Git Product home page Git Product logo

pa-api's People

Contributors

fennnec avatar galeone avatar giwiro avatar hackmac89 avatar isaka1022 avatar jleagle avatar keanpedersen avatar oakes6 avatar spiegel-im-spiegel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pa-api's Issues

Can't update the package because of wrong reference to errs package

Hi! I was looking for some update in a project, and when I typed

go get -u github.com/spiegel-im-spiegel/pa-api

here's what I got:

go get: github.com/spiegel-im-spiegel/[email protected]: parsing go.mod:
        module declares its path as: github.com/goark/errs
                but was required as: github.com/spiegel-im-spiegel/errs

I guess this line should be updated according to the new errs package location ๐Ÿ˜„

Enable to mock up the generation of "X-Amz-Date"

Hi, @isaka1022 san.

I would like to be able to mock time generation(time.Now()) when running unit tests.
I tried to write the code like above, but if you have a better way, please let me know.

Usecase

import (
	"github.com/dnaeon/go-vcr/recorder"

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


func Test(t *testing.T) {
	t.Run("tests", func(t *testing.T) {
		r, err := recorder.New(path)
		if err != nil {
			t.Fatal(err)
		}
		defer r.Stop()

		client := &http.Client{
			Transport: r,
		}

		amazon := paapi5.New().
			CreateClient(
				tag, access, secret,
				paapi5.WithHttpClient(client),
			)

		ctx := context.Background()
		q := query.NewSearchItems(...)

		// It would be nice to be able to mock up time generation when requesting here.
		content, err := amazon.RequestContext(ctx, q)
		if err != nil {
			t.Fatal(err)
		}
	})
}

Proposal

 func (c *client) post(ctx context.Context, cmd Operation, payload []byte) ([]byte, error) {
-       dt := NewTimeStamp(time.Now())
+       dt := NewTimeStamp(contextGetTime(ctx))
        u := c.server.URL(cmd.Path())
        hds := newHeaders(c.server, cmd, dt)
        sig := c.signiture(c.signedString(hds, payload), hds)
@@ -188,6 +188,20 @@ func (h *headers) values() string {
        return strings.Join(list, "\n")
 }
 
+type ctxTimeKey struct{}
+
+func ContextSetTime(ctx context.Context, t time.Time) context.Context {
+       return context.WithValue(ctx, ctxTimeKey{}, t)
+}
+
+func contextGetTime(ctx context.Context) time.Time {
+       val := ctx.Value(ctxTimeKey{})
+       if t, ok := val.(time.Time); !ok {
+               return t
+       }
+       return time.Now()
+}

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.