Giter VIP home page Giter VIP logo

selling-partner-api-sdk's Introduction

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

Go Reference

Installation

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

Progress

Example

package main

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.