From 675224526cd27114e79cb1da057a8532ec29bfe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alican=20Akku=C5=9F?= Date: Tue, 2 Jan 2024 17:17:01 +0300 Subject: [PATCH] adds localization (#64) --- README.md | 17 +++++++++++++++++ adapter/craftgate.go | 15 ++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e20bb8c..d4e8fa2 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,23 @@ if err != nil { } ``` +Also, you can pass the options to make localization for API responses. You can use `tr` or `en` right now. +```go +client, _ := craftgate.New("", "", "https://api.craftgate.io", craftgate.WithLocalization("en")) + +request := craftgate.SearchInstallmentsRequest{ + BinNumber: "487074", + Price: 100, + Currency: craftgate.Currency_TRY, +} + +res, err := client.Installment.SearchInstallments(context.Background(), request) + +if err != nil { + t.Errorf("Error %s", err) +} +``` + You should use production API servers at `https://api.craftgate.io` for real world. For testing purposes, please use the sandbox URL `https://sandbox-api.craftgate.io`. ## Examples diff --git a/adapter/craftgate.go b/adapter/craftgate.go index 06a5fb4..533cc18 100644 --- a/adapter/craftgate.go +++ b/adapter/craftgate.go @@ -65,6 +65,17 @@ func init() { type ClientOption func(*Client) error +func WithLocalization(lang string) ClientOption { + return func(client *Client) error { + if len(lang) > 0 { + client.headers["lang"] = strings.ToLower(lang) + } else { + client.headers["lang"] = "tr" + } + return nil + } +} + type Client struct { httpClient *http.Client baseURL *url.URL @@ -90,6 +101,8 @@ type Client struct { func New(apiKey, apiSecret, baseURL string, opts ...ClientOption) (*Client, error) { client := newClient(apiKey, apiSecret) + client.headers = make(map[string]string) + for _, option := range opts { if err := option(client); err != nil { return nil, err @@ -102,7 +115,7 @@ func New(apiKey, apiSecret, baseURL string, opts ...ClientOption) (*Client, erro if client.baseURL == nil { client.baseURL, _ = url.Parse(baseURL) } - client.headers = make(map[string]string) + return client, nil }