Created
September 7, 2020 11:09
-
-
Save aleksen/1ee7edf57a940be8fc1c0c361d3c7126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/http/httptest" | |
"sync" | |
"time" | |
"github.com/adyen/adyen-go-api-library/v2/src/adyen" | |
"github.com/adyen/adyen-go-api-library/v2/src/checkout" | |
"github.com/adyen/adyen-go-api-library/v2/src/common" | |
"github.com/google/uuid" | |
) | |
type requestLogger struct{} | |
func (rl *requestLogger) RoundTrip(req *http.Request) (*http.Response, error) { | |
body, _ := ioutil.ReadAll(req.Body) | |
data := map[string]interface{}{} | |
json.Unmarshal(body, &data) | |
reference := data["reference"] | |
idempotencyKey := req.Header.Get("Idempotency-Key") | |
if reference != idempotencyKey { | |
fmt.Printf("Request body %s had idempotencyKey %s", reference, idempotencyKey) | |
} | |
resp := httptest.NewRecorder() | |
resp.WriteHeader(http.StatusOK) | |
return resp.Result(), nil | |
} | |
func main() { | |
client := adyen.NewClient(&common.Config{ | |
HTTPClient: &http.Client{ | |
Transport: &requestLogger{}, | |
}, | |
}) | |
s := &http.Server{ | |
Addr: ":8080", | |
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
idempotencyKey := uuid.Must(uuid.NewRandom()).String() | |
client.SetIdempotencyKey(idempotencyKey) | |
client.Checkout.Payments(&checkout.PaymentRequest{ | |
Reference: idempotencyKey, | |
}) | |
w.WriteHeader(http.StatusOK) | |
}), | |
ReadTimeout: 10 * time.Second, | |
WriteTimeout: 10 * time.Second, | |
MaxHeaderBytes: 1 << 20, | |
} | |
go s.ListenAndServe() | |
wg := &sync.WaitGroup{} | |
wg.Add(10) | |
for r := 0; r < 10; r++ { | |
fmt.Printf("Routine #%d", r) | |
go func() { | |
for i := 0; i < 100; i++ { | |
fmt.Printf("Req#%d.%d", r, i) | |
http.Get("http://localhost:8080") | |
} | |
wg.Done() | |
}() | |
} | |
wg.Wait() | |
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | |
defer cancel() | |
s.Shutdown(ctx) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment