Skip to content

Commit

Permalink
add more options
Browse files Browse the repository at this point in the history
  • Loading branch information
Алексей Волегов committed Sep 19, 2022
1 parent 08c7076 commit 84215f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/badkaktus/gorocket

go 1.17
go 1.13
31 changes: 26 additions & 5 deletions gorocket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gorocket

import (
"context"
"encoding/json"
"log"
"net/http"
Expand All @@ -13,17 +14,30 @@ type Client struct {
xToken string
apiVersion string
HTTPClient *http.Client

timeout time.Duration
}

// NewClient creates new Facest.io client with given API key
func NewClient(opts ...Option) *Client {
c := &Client{
func NewClient(url string) *Client {
return &Client{
//userID: user,
HTTPClient: &http.Client{
Timeout: 5 * time.Minute,
},
//xToken: token,
// baseURL: url,
baseURL: url,
apiVersion: "api/v1",
}
}

// NewClient creates new Facest.io client with given API key
func NewWithOptions(url string, opts ...Option) *Client {
c := &Client{
HTTPClient: &http.Client{
Timeout: 5 * time.Minute,
},
baseURL: url,
apiVersion: "api/v1",
}

Expand All @@ -36,9 +50,9 @@ func NewClient(opts ...Option) *Client {

type Option func(*Client)

func WithUrl(url string) Option {
func WithTimeout(d time.Duration) Option {
return func(c *Client) {
c.baseURL = url
c.timeout = d
}
}

Expand All @@ -60,6 +74,13 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error {
req.Header.Add("X-Auth-Token", c.xToken)
req.Header.Add("X-User-Id", c.userID)

if c.timeout > 0 {
ctx, cancel := context.WithTimeout(req.Context(), c.timeout)
defer cancel()

req = req.WithContext(ctx)
}

res, err := c.HTTPClient.Do(req)
if err != nil {
log.Println(err)
Expand Down

0 comments on commit 84215f0

Please sign in to comment.