selling-partner-api-sdk

module
v0.0.0-...-1bcf55c Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2023 License: MIT

README

Amazon's Selling Partner API (SP-API) Golang SDK

Go Reference

Installation

go get -u github.com/arrodriguez/selling-partner-api-sdk

Progress

Example

package main

import (
	"context"
	"log"
	"net/http"
	"net/http/httputil"

	sp "github.com/arrodriguez/selling-partner-api-sdk/pkg/selling-partner"
	"github.com/arrodriguez/selling-partner-api-sdk/sellers"

	"github.com/google/uuid"
	"github.com/pkg/errors"
)

func main() {

	sellingPartner, err := sp.NewSellingPartner(&sp.Config{
		ClientID:     "<ClientID>",
		ClientSecret: "<ClientSecret>",
		RefreshToken: "<RefreshToken>",
		AccessKeyID: "<AWS IAM User Access Key Id>",
		SecretKey:   "<AWS IAM User Secret Key>",
		Region:      "<AWS Region>",
		RoleArn:     "<AWS IAM Role ARN>",
	})

	if err != nil {
		panic(err)
	}

	endpoint := "https://sellingpartnerapi-fe.amazon.com"

	seller, err := sellers.NewClientWithResponses(endpoint,
		sellers.WithRequestBefore(func(ctx context.Context, req *http.Request) error {
			req.Header.Add("X-Amzn-Requestid", uuid.New().String()) //tracking requests
			err = sellingPartner.SignRequest(req)
			if err != nil {
				return errors.Wrap(err, "sign error")
			}
			dump, err := httputil.DumpRequest(req, true)
			if err != nil {
				return errors.Wrap(err, "DumpRequest Error")
			}
			log.Printf("DumpRequest = %s", dump)
			return nil
		}),
		sellers.WithResponseAfter(func(ctx context.Context, rsp *http.Response) error {
			dump, err := httputil.DumpResponse(rsp, true)
			if err != nil {
				return errors.Wrap(err, "DumpResponse Error")
			}
			log.Printf("DumpResponse = %s", dump)
			return nil
		}),
	)

	if err != nil {
		panic(err)
	}

	ctx := context.Background()
	_, err = seller.GetMarketplaceParticipationsWithResponse(ctx)

	if err != nil {
		panic(err)
	}
}

#Report Decryption Amazon specification of version 2020-09-04 returns encrypted reports. To decrypt the reports you could use the Decrypt function of the decryption package.

Test example

The test example uses amzn.GetReportDocumentResponse from Amazon models and the Decrypt function to download, decrypt and dump report.

func TestSellingPartnerGetReportDocumentThirdParty(t *testing.T) {
	sellingPartner, err := sp.NewSellingPartner(&sp.Config{
		ClientID:     "***",
		ClientSecret: "***",
		RefreshToken: "***",
		AccessKeyID:  "***",
		SecretKey:    "***",
		Region:       "***",
		RoleArn:      "***",
	})

	if err != nil {
		t.Fatal("Failed to create NewSellingPartner: ", err)
	}

	report, err := reports.NewClientWithResponses(spiHost,
		reports.WithRequestBefore(func(ctx context.Context, req *http.Request) error {
			err = sellingPartner.SignRequest(req)
			if err != nil {
				return errors.Wrap(err, "sign error")
			}
			dump, err := httputil.DumpRequest(req, true)
			if err != nil {
				return errors.Wrap(err, "DumpRequest Error")
			}
			ioutil.WriteFile("test-samples/3dumpedThirdPartyReqGetReports.txt", dump, 0777)
			return nil
		}),
		reports.WithResponseAfter(func(ctx context.Context, rsp *http.Response) error {
			dump, err := httputil.DumpResponse(rsp, true)
			if err != nil {
				return errors.Wrap(err, "DumpResponse Error")
			}
			ioutil.WriteFile("test-samples/3dumpedThirdPartyRespGetReports.txt", dump, 0777)
			return nil
		}),
	)
	if err != nil {
		t.Fatal("Failed to create NewClientWithResponses: ", err)
	}
	ctx := context.Background()
	reportDocumentId := "***"
	resp, err := report.GetReportDocument(ctx, reportDocumentId)
	if err != nil {
		t.Fatal("Failed to make GetReportDocument request: ", err)
	}
	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		t.Fatal("Service returned a status that isn't 2xx: ", resp.StatusCode)
	}
	defer resp.Body.Close()
	var bodyData []byte
	resp.Body.Read(bodyData)
	content, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatal("Failed to read GetReportDocument response body: ", err)
	}
	var reportDocumentResp amzn.GetReportDocumentResponse
	err = json.Unmarshal(content, &reportDocumentResp)
	if err != nil {
		t.Fatal("Failed to unmarshall GetReportDocument response: ", err)
	}
	if reportDocumentResp.Errors != nil {
		t.Fatal("Got errors in the GetReportDocument response: ", reportDocumentResp.Errors)
	}
	respWithDocContent, err := http.Get(reportDocumentResp.Payload.Url)
	if err != nil {
		t.Fatal("failed to make request to "+reportDocumentResp.Payload.Url+" : ", err)
	}
	if respWithDocContent.StatusCode < 200 || respWithDocContent.StatusCode > 299 {
		t.Fatal("Service returned a status that isn't 2xx: ", respWithDocContent.StatusCode)
	}
	defer respWithDocContent.Body.Close()

	reportContentBytes, err := ioutil.ReadAll(respWithDocContent.Body)
	if err != nil {
		t.Fatal("Failed to read Report content body: ", err)
	}
	ioutil.WriteFile("test-samples/3_encrypted_"+reportDocumentId+"_"+reportType+"_report.txt", reportContentBytes, 0777)
	decryptedFilecontent, err := decryption.Decrypt(reportDocumentResp.Payload.EncryptionDetails.Key, reportDocumentResp.Payload.EncryptionDetails.InitializationVector, reportContentBytes)
	if err != nil {
		t.Fatal("Failed to decrypt file content: ", err)
	}
	ioutil.WriteFile("test-samples/3_"+reportDocumentId+"_"+reportType+"_report.txt", decryptedFilecontent, 0777)
}

Directories

Path Synopsis
Package authorization provides primitives to interact the openapi HTTP API.
Package authorization provides primitives to interact the openapi HTTP API.
Package catalog provides primitives to interact the openapi HTTP API.
Package catalog provides primitives to interact the openapi HTTP API.
Package fbaInbound provides primitives to interact the openapi HTTP API.
Package fbaInbound provides primitives to interact the openapi HTTP API.
Package fbaInventory provides primitives to interact the openapi HTTP API.
Package fbaInventory provides primitives to interact the openapi HTTP API.
Package fbaOutbound provides primitives to interact the openapi HTTP API.
Package fbaOutbound provides primitives to interact the openapi HTTP API.
Package feeds provides primitives to interact the openapi HTTP API.
Package feeds provides primitives to interact the openapi HTTP API.
Package fees provides primitives to interact the openapi HTTP API.
Package fees provides primitives to interact the openapi HTTP API.
Package finances provides primitives to interact the openapi HTTP API.
Package finances provides primitives to interact the openapi HTTP API.
Package merchantFulfillment provides primitives to interact the openapi HTTP API.
Package merchantFulfillment provides primitives to interact the openapi HTTP API.
Package messaging provides primitives to interact the openapi HTTP API.
Package messaging provides primitives to interact the openapi HTTP API.
Package notifications provides primitives to interact the openapi HTTP API.
Package notifications provides primitives to interact the openapi HTTP API.
Package ordersV0 provides primitives to interact the openapi HTTP API.
Package ordersV0 provides primitives to interact the openapi HTTP API.
pkg
Package productPricing provides primitives to interact the openapi HTTP API.
Package productPricing provides primitives to interact the openapi HTTP API.
Package reports provides primitives to interact the openapi HTTP API.
Package reports provides primitives to interact the openapi HTTP API.
Package sales provides primitives to interact the openapi HTTP API.
Package sales provides primitives to interact the openapi HTTP API.
Package sellers provides primitives to interact the openapi HTTP API.
Package sellers provides primitives to interact the openapi HTTP API.
Package service provides primitives to interact the openapi HTTP API.
Package service provides primitives to interact the openapi HTTP API.
Package shipping provides primitives to interact the openapi HTTP API.
Package shipping provides primitives to interact the openapi HTTP API.
Package smallAndLight provides primitives to interact the openapi HTTP API.
Package smallAndLight provides primitives to interact the openapi HTTP API.
Package solicitations provides primitives to interact the openapi HTTP API.
Package solicitations provides primitives to interact the openapi HTTP API.
Package uploads provides primitives to interact the openapi HTTP API.
Package uploads provides primitives to interact the openapi HTTP API.

Jump to

Keyboard shortcuts

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