paapi5

package module
v0.12.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2024 License: Apache-2.0 Imports: 15 Imported by: 1

README

pa-api -- APIs for Amazon Product Advertising API v5 by Golang

check vulns lint status GitHub license GitHub release

This package is required Go 1.16 or later.

Migrated repository to github.com/goark/pa-api

Usage

Create PA-API Information

Default PA-API information.

sv := paapi5.New() //Create default server
fmt.Println("Marketplace:", sv.Marketplace())
fmt.Println("Region:", sv.Region())
fmt.Println("AcceptLanguage:", sv.AcceptLanguage())
fmt.Println("URL:", sv.URL(paapi5.GetItems.Path()))
// Output:
// Marketplace: www.amazon.com
// Region: us-east-1
// AcceptLanguage: en_US
// URL: https://webservices.amazon.com/paapi5/getitems

PA-API information for Japan region.

sv := paapi5.New(paapi5.WithMarketplace(paapi5.LocaleJapan)) //Create server in Japan region
fmt.Println("Marketplace:", sv.Marketplace())
fmt.Println("Region:", sv.Region())
fmt.Println("AcceptLanguage:", sv.AcceptLanguage())
fmt.Println("URL:", sv.URL(paapi5.GetItems.Path()))
// Output:
// Marketplace: www.amazon.co.jp
// Region: us-west-2
// AcceptLanguage: ja_JP
// URL: https://webservices.amazon.co.jp/paapi5/getitems
Create Client Instance

Create default client instance.

client := paapi5.DefaultClient("mytag-20", "AKIAIOSFODNN7EXAMPLE", "1234567890") //Create default client
fmt.Println("Marketplace:", client.Marketplace())
// Output:
// Marketplace: www.amazon.com

Create client instance for Japan region.

//Create client for Janan region
client := paapi5.New(
    paapi5.WithMarketplace(paapi5.LocaleJapan),
).CreateClient(
    "mytag-20",
    "AKIAIOSFODNN7EXAMPLE",
    "1234567890",
    paapi5.WithHttpClient(http.DefaultClient),
)
fmt.Println("Marketplace:", client.Marketplace())
// Output:
// Marketplace: www.amazon.co.jp

Sample code

Operation GetItems
package main

import (
    "context"
    "fmt"

    paapi5 "github.com/goark/pa-api"
    "github.com/goark/pa-api/entity"
    "github.com/goark/pa-api/query"
)

func main() {
    //Create client
    client := paapi5.New(
        paapi5.WithMarketplace(paapi5.LocaleJapan),
    ).CreateClient(
        "mytag-20",
        "AKIAIOSFODNN7EXAMPLE",
        "1234567890",
    )

    //Make query
    q := query.NewGetItems(
        client.Marketplace(),
        client.PartnerTag(),
        client.PartnerType(),
    ).ASINs([]string{"B07YCM5K55"}).EnableImages().EnableItemInfo().EnableParentASIN()

    //Requet and response
    body, err := client.RequestContext(context.Background(), q)
    if err != nil {
        fmt.Printf("%+v\n", err)
        return
    }
    //io.Copy(os.Stdout, bytes.NewReader(body))

    //Decode JSON
    res, err := entity.DecodeResponse(body)
    if err != nil {
        fmt.Printf("%+v\n", err)
        return
    }
    fmt.Println(res.String())
}
Operation GetVariations
package main

import (
    "context"
    "fmt"

    paapi5 "github.com/goark/pa-api"
    "github.com/goark/pa-api/entity"
    "github.com/goark/pa-api/query"
)

func main() {
    //Create client
    client := paapi5.New(
        paapi5.WithMarketplace(paapi5.LocaleJapan),
    ).CreateClient(
        "mytag-20",
        "AKIAIOSFODNN7EXAMPLE",
        "1234567890",
    )

    //Make query
    q := query.NewGetVariations(
        client.Marketplace(),
        client.PartnerTag(),
        client.PartnerType(),
    ).ASIN("B07YCM5K55").EnableImages().EnableItemInfo().EnableParentASIN()

    //Request and response
    body, err := client.RequestContext(context.Background(), q)
    if err != nil {
        fmt.Printf("%+v\n", err)
        return
    }
    //io.Copy(os.Stdout, bytes.NewReader(body))

    //Decode JSON
    res, err := entity.DecodeResponse(body)
    if err != nil {
        fmt.Printf("%+v\n", err)
        return
    }
    fmt.Println(res.String())
}
Operation SearchItems
package main

import (
    "context"
    "fmt"

    paapi5 "github.com/goark/pa-api"
    "github.com/goark/pa-api/entity"
    "github.com/goark/pa-api/query"
)

func main() {
    //Create client
    client := paapi5.New(
        paapi5.WithMarketplace(paapi5.LocaleJapan),
    ).CreateClient(
        "mytag-20",
        "AKIAIOSFODNN7EXAMPLE",
        "1234567890",
    )

    //Make query
    q := query.NewSearchItems(
        client.Marketplace(),
        client.PartnerTag(),
        client.PartnerType(),
    ).Search(query.Keywords, "数学ガール").EnableImages().EnableItemInfo().EnableParentASIN()

    //Request and response
    body, err := client.RequestContext(context.Background(), q)
    if err != nil {
        fmt.Printf("%+v\n", err)
        return
    }
    //io.Copy(os.Stdout, bytes.NewReader(body))

    //Decode JSON
    res, err := entity.DecodeResponse(body)
    if err != nil {
        fmt.Printf("%+v\n", err)
        return
    }
    fmt.Println(res.String())
}
Operation GetBrowseNodes
package main

import (
    "context"
    "fmt"

    paapi5 "github.com/goark/pa-api"
    "github.com/goark/pa-api/entity"
    "github.com/goark/pa-api/query"
)

func main() {
    //Create client
    client := paapi5.New(
        paapi5.WithMarketplace(paapi5.LocaleJapan),
    ).CreateClient(
        "mytag-20",
        "AKIAIOSFODNN7EXAMPLE",
        "1234567890",
    )

    //Make query
    q := query.NewGetBrowseNodes(
        client.Marketplace(),
        client.PartnerTag(),
        client.PartnerType(),
    ).BrowseNodeIds([]string{"3040", "3045"}).EnableBrowseNodes()

    //Request and response
    body, err := client.RequestContext(context.Background(), q)
    if err != nil {
        fmt.Printf("%+v\n", err)
        return
    }

    //Decode JSON
    res, err := entity.DecodeResponse(body)
    if err != nil {
        fmt.Printf("%+v\n", err)
        return
    }
    fmt.Println(res.String())
}

Contributors

Many thanks for contributors

Documentation

Overview

Package paapi5 APIs for Amazon Product Advertising API v5 client

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Marketplace() string
	PartnerTag() string
	PartnerType() string
	Request(Query) ([]byte, error)
	RequestContext(context.Context, Query) ([]byte, error)
}

Client interface

Example
package main

import (
	"context"
	"fmt"
	"net/http"

	paapi5 "github.com/goark/pa-api"
)

func main() {
	//Create client for Janan region
	client := paapi5.New(
		paapi5.WithMarketplace(paapi5.LocaleJapan),
	).CreateClient(
		"mytag-20",
		"AKIAIOSFODNN7EXAMPLE",
		"1234567890",
		paapi5.WithContext(context.Background()),
		paapi5.WithHttpClient(http.DefaultClient),
	)
	fmt.Println("Marketplace:", client.Marketplace())
}
Output:

Marketplace: www.amazon.co.jp

func DefaultClient

func DefaultClient(associateTag, accessKey, secretKey string) Client

DefaultClient function returns an default Client instance with associate-tag, access-key, and secret-key parameters.

Example
package main

import (
	"fmt"

	paapi5 "github.com/goark/pa-api"
)

func main() {
	client := paapi5.DefaultClient("mytag-20", "AKIAIOSFODNN7EXAMPLE", "1234567890") //Create default client
	fmt.Println("Marketplace:", client.Marketplace())
}
Output:

Marketplace: www.amazon.com

type ClientOptFunc

type ClientOptFunc func(*client)

ClientOptFunc type is self-referential function type for Server.CreateClient method. (functional options pattern)

func WithContext

func WithContext(ctx context.Context) ClientOptFunc

WithContext is dummy function. Because this function is deprecated.

func WithHttpClient

func WithHttpClient(hc *http.Client) ClientOptFunc

WithHttpClient function returns ClientOptFunc function value. This function is used in Server.CreateClient method that represents http.Client.

type Error

type Error int

Error is error codes for paapi5 package

const (
	ErrNullPointer Error = iota + 1 //Null reference instance
	ErrHTTPStatus                   //Bad HTTP status
	ErrNoData                       //No response data
)

func (Error) Error

func (e Error) Error() string

Error method returns error message. This method is a implementation of error interface.

type Marketplace

type Marketplace interface {
	String() string
	HostName() string
	Region() string
	Language() string
}

Marketplace is interface class of locale information.

func MarketplaceOf

func MarketplaceOf(s string) Marketplace

MarketplaceOf function returns Marketplace instance from service domain.

type MarketplaceEnum

type MarketplaceEnum int

MarketplaceEnum is enumeration of locale information.

const (
	LocaleUnknown            MarketplaceEnum = iota //Unknown local
	LocaleAustralia                                 //Australia
	LocaleBrazil                                    //Brazil
	LocaleCanada                                    //Canada
	LocaleEgypt                                     //Egypt
	LocaleFrance                                    //France
	LocaleGermany                                   //Germany
	LocaleIndia                                     //India
	LocaleItaly                                     //Italy
	LocaleJapan                                     //Japan
	LocaleMexico                                    //Mexico
	LocaleNetherlands                               //Netherlands
	LocalePoland                                    //Poland
	LocaleSingapore                                 //Singapore
	LocaleSaudiArabia                               //SaudiArabia
	LocaleSpain                                     //Spain
	LocaleSweden                                    //Sweden
	LocaleTurkey                                    //Turkey
	LocaleUnitedArabEmirates                        //United Arab Emirates
	LocaleUnitedKingdom                             //United Kingdom
	LocaleUnitedStates                              //United States
	DefaultMarketplace       = LocaleUnitedStates
)

func (MarketplaceEnum) HostName

func (m MarketplaceEnum) HostName() string

HostName returns hostname of Marketplace.

func (MarketplaceEnum) Language

func (m MarketplaceEnum) Language() string

Language returns language name of Marketplace.

func (MarketplaceEnum) Region

func (m MarketplaceEnum) Region() string

Region returns region name of Marketplace.

func (MarketplaceEnum) String

func (m MarketplaceEnum) String() string

String returns marketplace name of Marketplace.

type Operation

type Operation int

Operation is enumeration of PA-API operation

const (
	NullOperation  Operation = iota //Unknown
	GetVariations                   //GetVariations
	GetItems                        //GetItems
	SearchItems                     //SearchItems
	GetBrowseNodes                  //GetBrowseNodes
)

func (Operation) MarshalJSON

func (c Operation) MarshalJSON() ([]byte, error)

MarshalJSON method implements the json.Marshaler interface.

func (Operation) Path

func (c Operation) Path() string

Path method returns URL path of PA-API operation

func (Operation) String

func (c Operation) String() string

String method is a implementation of fmt.Stringer interface.

func (Operation) Target

func (c Operation) Target() string

Target method returns taget name of PA-API operation

func (*Operation) UnmarshalJSON

func (c *Operation) UnmarshalJSON(b []byte) error

UnmarshalJSON method implements json.Unmarshaler interface.

type Query

type Query interface {
	Operation() Operation
	Payload() ([]byte, error)
}

Query interface for Client type

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server type is a implementation of PA-API service.

Example
package main

import (
	"fmt"

	paapi5 "github.com/goark/pa-api"
)

func main() {
	sv := paapi5.New() //Create default server
	fmt.Println("Marketplace:", sv.Marketplace())
	fmt.Println("Region:", sv.Region())
	fmt.Println("AcceptLanguage:", sv.AcceptLanguage())
	fmt.Println("URL:", sv.URL(paapi5.GetItems.Path()))
}
Output:

Marketplace: www.amazon.com
Region: us-east-1
AcceptLanguage: en_US
URL: https://webservices.amazon.com/paapi5/getitems

func New

func New(opts ...ServerOptFunc) *Server

New function returns an Server instance with options.

Example
package main

import (
	"fmt"

	paapi5 "github.com/goark/pa-api"
)

func main() {
	sv := paapi5.New(paapi5.WithMarketplace(paapi5.LocaleJapan)) //Create server in Japan region
	fmt.Println("Marketplace:", sv.Marketplace())
	fmt.Println("Region:", sv.Region())
	fmt.Println("AcceptLanguage:", sv.AcceptLanguage())
	fmt.Println("URL:", sv.URL(paapi5.GetItems.Path()))
}
Output:

Marketplace: www.amazon.co.jp
Region: us-west-2
AcceptLanguage: ja_JP
URL: https://webservices.amazon.co.jp/paapi5/getitems

func (*Server) AWS4Request

func (s *Server) AWS4Request() string

AWS4Request method returns AWS4Request parameter for PA-API v5

func (*Server) Accept

func (s *Server) Accept() string

Accept method returns Accept parameter for PA-API v5

func (*Server) AcceptLanguage

func (s *Server) AcceptLanguage() string

AcceptLanguage method returns Accept-Language parameter for PA-API v5

func (*Server) ContentEncoding

func (s *Server) ContentEncoding() string

ContentEncoding method returns Content-Encoding parameter for PA-API v5

func (*Server) ContentType

func (s *Server) ContentType() string

ContentType method returns Content-Type parameter for PA-API v5

func (*Server) CreateClient

func (s *Server) CreateClient(associateTag, accessKey, secretKey string, opts ...ClientOptFunc) Client

CreateClient method returns an Client instance with associate-tag, access-key, secret-key, and other options.

func (*Server) HMACAlgorithm

func (s *Server) HMACAlgorithm() string

HMACAlgorithm method returns HMAC-Algorithm parameter for PA-API v5

func (*Server) HostName

func (s *Server) HostName() string

HostName method returns hostname for PA-API v5.

func (*Server) Marketplace

func (s *Server) Marketplace() string

Marketplace method returns marketplace name for PA-API v5.

func (*Server) Region

func (s *Server) Region() string

Region method returns region name for PA-API v5

func (*Server) ServiceName

func (s *Server) ServiceName() string

ServiceName method returns ServiceName parameter for PA-API v5

func (*Server) URL

func (s *Server) URL(path string) *url.URL

URL method returns url of service server information for PA-API v5.

type ServerOptFunc

type ServerOptFunc func(*Server)

ServerOptFunc type is self-referential function type for New functions. (functional options pattern)

func WithLanguage

func WithLanguage(language string) ServerOptFunc

WithLanguage function returns ServerOptFunc function value. This function is used in New functions that represents Accept-Language parameter.

func WithMarketplace

func WithMarketplace(marketplace Marketplace) ServerOptFunc

WithMarketplace function returns ServerOptFunc function value. This function is used in New functions that represents Marketplace data.

type TimeStamp

type TimeStamp struct {
	time.Time
}

TimeStamp is wrapper class of time.Time

func NewTimeStamp

func NewTimeStamp(tm time.Time) TimeStamp

NewTimeStamp returns TimeStamp instance

func (TimeStamp) String

func (t TimeStamp) String() string

func (TimeStamp) StringDate

func (t TimeStamp) StringDate() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL