-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added balance client and invalidated balance for legacy re claiming (#…
…202)
- Loading branch information
1 parent
877449c
commit a6bd304
Showing
6 changed files
with
234 additions
and
21 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package balance | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"os" | ||
"time" | ||
|
||
"github.com/brave-intl/bat-go/utils/closers" | ||
"github.com/rs/zerolog/log" | ||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
// Client abstracts over the underlying client | ||
type Client interface { | ||
InvalidateBalance(ctx context.Context, id uuid.UUID) error | ||
} | ||
|
||
// HTTPClient wraps http.Client for interacting with the ledger server | ||
type HTTPClient struct { | ||
BaseURL *url.URL | ||
AuthToken string | ||
|
||
client *http.Client | ||
} | ||
|
||
// New returns a new HTTPClient, retrieving the base URL from the environment | ||
func New() (*HTTPClient, error) { | ||
serverURL := os.Getenv("BALANCE_SERVER") | ||
|
||
if len(serverURL) == 0 { | ||
return nil, errors.New("BALANCE_SERVER was empty") | ||
} | ||
|
||
baseURL, err := url.Parse(serverURL) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &HTTPClient{ | ||
BaseURL: baseURL, | ||
AuthToken: os.Getenv("BALANCE_TOKEN"), | ||
client: &http.Client{ | ||
Timeout: time.Second * 10, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (c *HTTPClient) newRequest(ctx context.Context, method, path string, body interface{}) (*http.Request, error) { | ||
url := c.BaseURL.ResolveReference(&url.URL{Path: path}) | ||
|
||
var buf io.ReadWriter | ||
if body != nil { | ||
buf = new(bytes.Buffer) | ||
err := json.NewEncoder(buf).Encode(body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
req, err := http.NewRequest(method, url.String(), buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("accept", "application/json") | ||
|
||
if body != nil { | ||
req.Header.Add("content-type", "application/json") | ||
} | ||
|
||
logger := log.Ctx(ctx) | ||
|
||
dump, err := httputil.DumpRequestOut(req, true) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
logger.Debug().Str("type", "http.Request").Msg(string(dump)) | ||
|
||
req.Header.Set("authorization", "Bearer "+c.AuthToken) | ||
|
||
return req, err | ||
} | ||
|
||
func (c *HTTPClient) do(req *http.Request, v interface{}) (*http.Response, error) { | ||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer closers.Panic(resp.Body) | ||
|
||
if resp.StatusCode >= 200 && resp.StatusCode <= 299 { | ||
if v != nil { | ||
err = json.NewDecoder(resp.Body).Decode(v) | ||
if err != nil { | ||
return resp, err | ||
} | ||
} | ||
return resp, nil | ||
} | ||
return resp, fmt.Errorf("Request error: %d", resp.StatusCode) | ||
} | ||
|
||
// InvalidateBalance invalidates the cached value on balance | ||
func (c *HTTPClient) InvalidateBalance(ctx context.Context, id uuid.UUID) error { | ||
req, err := c.newRequest(ctx, "DELETE", "v2/wallet/"+id.String()+"/balance", nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = c.do(req, nil) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.