Skip to content

Commit

Permalink
Merge pull request #3 from coinbase-samples/jc-dev
Browse files Browse the repository at this point in the history
add core.go support
  • Loading branch information
jc-cb authored Jun 14, 2024
2 parents 7dc342f + 1b4f758 commit d5a7f50
Show file tree
Hide file tree
Showing 40 changed files with 159 additions and 319 deletions.
216 changes: 0 additions & 216 deletions call.go

This file was deleted.

5 changes: 3 additions & 2 deletions cancel_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package intx
import (
"context"
"fmt"
"github.com/coinbase-samples/core-go"
)

type CancelOrderRequest struct {
Expand All @@ -31,7 +32,7 @@ type CancelOrderResponse struct {
Request *CancelOrderRequest
}

func (c Client) CancelOrder(
func (c *Client) CancelOrder(
ctx context.Context,
request *CancelOrderRequest,
) (*CancelOrderResponse, error) {
Expand All @@ -42,7 +43,7 @@ func (c Client) CancelOrder(

response := &CancelOrderResponse{Request: request}

if err := del(ctx, c, path, queryParams, nil, &response.Order); err != nil {
if err := core.Delete(ctx, c, path, queryParams, nil, &response.Order, addIntxHeaders); err != nil {
return nil, err
}

Expand Down
5 changes: 3 additions & 2 deletions cancel_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package intx

import (
"context"
"github.com/coinbase-samples/core-go"
)

type CancelOrdersRequest struct {
Expand All @@ -30,7 +31,7 @@ type CancelOrdersResponse struct {
Request *CancelOrdersRequest
}

func (c Client) CancelOrders(
func (c *Client) CancelOrders(
ctx context.Context,
request *CancelOrdersRequest,
) (*CancelOrdersResponse, error) {
Expand All @@ -45,7 +46,7 @@ func (c Client) CancelOrders(

response := &CancelOrdersResponse{Request: request}

if err := del(ctx, c, path, queryParams, nil, &response.Order); err != nil {
if err := core.Delete(ctx, c, path, queryParams, nil, &response.Order, addIntxHeaders); err != nil {
return nil, err
}

Expand Down
61 changes: 51 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,73 @@
package intx

import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/coinbase-samples/core-go"
"log"
"net/http"
"os"
"strconv"
"time"
)

var defaultV1ApiBaseUrl = "https://api.international.coinbase.com/api/v1"

type Client struct {
HttpClient http.Client
httpClient http.Client
httpBaseUrl string
Credentials *Credentials
HttpBaseUrl string
}

func (c *Client) BaseUrl(u string) *Client {
c.HttpBaseUrl = u
func (c *Client) HttpBaseUrl() string {
return c.httpBaseUrl
}

func (c *Client) HttpClient() *http.Client {
return &c.httpClient
}

func (c *Client) SetBaseUrl(u string) *Client {
c.httpBaseUrl = u
return c
}

func NewClient(credentials *Credentials, httpClient http.Client) *Client {
baseUrl := os.Getenv("INTX_BASE_URL")
if baseUrl == "" {
baseUrl = defaultV1ApiBaseUrl
httpBaseUrl := os.Getenv("INTX_BASE_URL")
if httpBaseUrl == "" {
httpBaseUrl = defaultV1ApiBaseUrl
}

return &Client{
httpBaseUrl: httpBaseUrl,
Credentials: credentials,
HttpClient: httpClient,
HttpBaseUrl: baseUrl,
httpClient: httpClient,
}
}

func addIntxHeaders(req *http.Request, path string, body []byte, client core.Client, t time.Time) {
c := client.(*Client)
signature := sign(req.Method, path, t.Unix(), c.Credentials.SigningKey, body)
req.Header.Add("Accept", "application/json")
req.Header.Add("CB-ACCESS-KEY", c.Credentials.AccessKey)
req.Header.Add("CB-ACCESS-PASSPHRASE", c.Credentials.Passphrase)
req.Header.Add("CB-ACCESS-SIGN", signature)
req.Header.Add("CB-ACCESS-TIMESTAMP", strconv.FormatInt(t.Unix(), 10))
}

func sign(method, path string, t int64, signingKey string, body []byte) string {
key, err := base64.StdEncoding.DecodeString(signingKey)
if err != nil {
log.Fatalf("Error decoding signing key: %v", err)
}

message := fmt.Sprintf("%d%s%s", t, method, path)
if len(body) > 0 {
message += string(body)
}

h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
5 changes: 3 additions & 2 deletions create_counterparty_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package intx

import (
"context"
"github.com/coinbase-samples/core-go"
)

type CreateCounterpartyIdRequest struct {
Expand All @@ -29,7 +30,7 @@ type CreateCounterpartyIdResponse struct {
Request *CreateCounterpartyIdRequest `json:"request"`
}

func (c Client) CreateCounterpartyId(
func (c *Client) CreateCounterpartyId(
ctx context.Context,
request *CreateCounterpartyIdRequest,
) (*CreateCounterpartyIdResponse, error) {
Expand All @@ -40,7 +41,7 @@ func (c Client) CreateCounterpartyId(

response := &CreateCounterpartyIdResponse{Request: request}

if err := post(ctx, c, path, queryParams, request, &response); err != nil {
if err := core.Post(ctx, c, path, queryParams, request, &response, addIntxHeaders); err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit d5a7f50

Please sign in to comment.