Skip to content

Latest commit

 

History

History
191 lines (168 loc) · 10.6 KB

README.md

File metadata and controls

191 lines (168 loc) · 10.6 KB

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

Go Reference

Installation

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

Progress

Example

package main

import (
	"context"
	"log"
	"net/http"
	"net/http/httputil"
	
	sp "github.com/amzapi/selling-partner-api-sdk/pkg/selling-partner"
	"github.com/amzapi/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>",
	})

	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.AuthorizeRequest(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: "***",
	})

	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.AuthorizeRequest(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)
}