Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
reywyn committed Jul 26, 2022
1 parent 15fda61 commit 3c98135
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
15 changes: 7 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

const (
BaseURL = "https://sandbox-api.craftgate.io"
ApiKeyHeaderName = "x-api-key"
RandomHeaderName = "x-rnd-key"
AuthVersionHeaderName = "x-auth-version"
Expand All @@ -25,17 +24,17 @@ type DataResponse[T any] struct {
}

type Client struct {
InstallmentApi InstallmentApi
Installment InstallmentApi
}

func CraftgateClient(apiKey, secretKey string) *Client {
func CraftgateClient(apiKey, secretKey, baseURL string) *Client {

return &Client{
InstallmentApi: InstallmentApi{
opts: RequestOptions{
apiKey: apiKey,
secretKey: secretKey,
baseURL: BaseURL,
Installment: InstallmentApi{
Opts: RequestOptions{
ApiKey: apiKey,
SecretKey: secretKey,
BaseURL: baseURL,
},
},
}
Expand Down
33 changes: 13 additions & 20 deletions installment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

type InstallmentApi struct {
opts RequestOptions
Opts RequestOptions
}

type InstallmentParams struct {
binNumber string
price float64
currency string
distinctCardBrandsWithLowestCommissions bool
type SearchInstallmentRequest struct {
BinNumber string
Price float64
Currency string
DistinctCardBrandsWithLowestCommissions bool
}

type InstallmentPrice struct {
Expand All @@ -23,7 +23,7 @@ type InstallmentPrice struct {
InstallmentLabel string `json:"installmentLabel"`
}

type Installment struct {
type InstallmentResponse struct {
BinNumber string `json:"binNumber"`
Price float64 `json:"price"`
CardType string `json:"cardType"`
Expand All @@ -38,22 +38,15 @@ type Installment struct {
InstallmentPrices []InstallmentPrice `json:"installmentPrices"`
}

func (c *InstallmentApi) Search(installmentParams InstallmentParams) (*Response[Installment], error) {

req, err := http.NewRequest("GET", fmt.Sprintf("%s/installment/v1/installments", c.opts.baseURL), nil)
if err != nil {
panic(err)
return nil, err
}
func (api *InstallmentApi) SearchInstallments(request SearchInstallmentRequest) (*Response[InstallmentResponse], error) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/installment/v1/installments", api.Opts.BaseURL), nil)

q := req.URL.Query()
q.Add("binNumber", installmentParams.binNumber)
q.Add("price", fmt.Sprintf("%f", installmentParams.price))
q.Add("binNumber", request.BinNumber)
q.Add("price", fmt.Sprintf("%f", request.Price))
req.URL.RawQuery = q.Encode()

//req = req.WithContext(ctx)

res := Response[Installment]{}
resErr := sendRequest(req, &res, c.opts)
res := Response[InstallmentResponse]{}
resErr := sendRequest(req, &res, api.Opts)
return &res, resErr
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import "fmt"

func main() {
baseURL := "https://sandbox-api.craftgate.io"
apiKey := "sandbox-YEhueLgomBjqsnvBlWVVuFsVhlvJlMHE"
secretKey := "sandbox-tBdcdKVGmGupzfaWcULcwDLMoglZZvTz"
Craftgate := CraftgateClient(apiKey, secretKey)
res, _ := Craftgate.InstallmentApi.Search(InstallmentParams{binNumber: "487074", price: 100.00})
Craftgate := CraftgateClient(apiKey, secretKey, baseURL)
res, _ := Craftgate.Installment.SearchInstallments(SearchInstallmentRequest{BinNumber: "487074", Price: 100.00})
fmt.Println(res)
}
14 changes: 5 additions & 9 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

type RequestOptions struct {
baseURL string
apiKey string
secretKey string
BaseURL string
ApiKey string
SecretKey string
}

func sendRequest(req *http.Request, v interface{}, opts RequestOptions) error {
Expand All @@ -23,9 +23,9 @@ func sendRequest(req *http.Request, v interface{}, opts RequestOptions) error {
}

randomStr := GenerateRandomString()
hashStr := GenerateHash(req.URL.String(), opts.apiKey, opts.secretKey, randomStr, "")
hashStr := GenerateHash(req.URL.String(), opts.ApiKey, opts.SecretKey, randomStr, "")
fmt.Println(hashStr)
req.Header.Set(ApiKeyHeaderName, opts.apiKey)
req.Header.Set(ApiKeyHeaderName, opts.ApiKey)
req.Header.Set(RandomHeaderName, randomStr)
req.Header.Set(AuthVersionHeaderName, "1")
req.Header.Set(ClientVersionHeaderName, "craftgate-go-client:1.0.0")
Expand All @@ -34,10 +34,6 @@ func sendRequest(req *http.Request, v interface{}, opts RequestOptions) error {
req.Header.Set("Accept", "application/json; charset=utf-8")

res, err := client.Do(req)
if err != nil {
return err
}

defer res.Body.Close()

if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
Expand Down

0 comments on commit 3c98135

Please sign in to comment.