Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
breaking change: rename API.DoAPIRequest to API.Call
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Apr 1, 2020
1 parent aff2bb2 commit d9ec2a3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
log.Printf("[INFO] Initialising Prisma connection with API key %s", opts.PrismAPIKey)

p := prisma.NewClient(opts.PrismAPIKey, opts.PrismAPIPassword, opts.PrismAPIUrl)
healthCheckResult, err := p.DoAPIRequest("GET", "/check", nil)
healthCheckResult, err := p.Call("GET", "/check", nil)
if err != nil {
log.Printf("[ERROR] Can't check Prisma health, %s", err)
return
Expand Down
14 changes: 7 additions & 7 deletions prisma.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func NewClient(username, password, apiURL string) *API {
return &API{username: username, password: password, apiURL: apiURL, httpClientTimeout: defaultHTTPClientTimeout}
}

// DoAPIRequest does request to API with specified method
// Call does request to API with specified method
// and returns response body on success.
// Thread safe.
func (p *API) DoAPIRequest(method, url string, body io.Reader) ([]byte, error) {
func (p *API) Call(method, url string, body io.Reader) ([]byte, error) {
p.tokenLock.Lock()
if time.Since(p.tokenRenewTime) > prismaRenewTimeout {
if err := p.authenticate(); err != nil {
Expand All @@ -70,7 +70,7 @@ func (p *API) DoAPIRequest(method, url string, body io.Reader) ([]byte, error) {
}
token := p.token
p.tokenLock.Unlock()
return doAPIRequestWithToken(method, p.apiURL+url, token, body)
return callWithToken(method, p.apiURL+url, token, body)
}

// SetTimeout sets http timeout for Prisma requests to specified value.
Expand All @@ -94,7 +94,7 @@ func (p *API) authenticate() error {
if err != nil {
return errors.Wrap(err, "error marshaling login data")
}
data, err := doAPIRequestWithToken("POST", p.apiURL+"/login", "", bytes.NewBuffer(jsonValue))
data, err := callWithToken("POST", p.apiURL+"/login", "", bytes.NewBuffer(jsonValue))
if err != nil {
return errors.Wrapf(err, "error logging in with user %q", p.username)
}
Expand All @@ -104,7 +104,7 @@ func (p *API) authenticate() error {
p.token = res.Token
default:
// token is set and we will try to renew it
data, err := doAPIRequestWithToken("GET", p.apiURL+"/auth_token/extend", p.token, nil)
data, err := callWithToken("GET", p.apiURL+"/auth_token/extend", p.token, nil)
if err != nil {
log.Printf("[INFO] Error extending token, will re-login, %v", err)
p.token = ""
Expand All @@ -120,10 +120,10 @@ func (p *API) authenticate() error {
return nil
}

// doAPIRequestWithToken does request to API with specified method and
// callWithToken does request to Prisma API with specified method and
// provided token and returns response body on success.
// Thread safe.
func doAPIRequestWithToken(method, fullURL, token string, body io.Reader) ([]byte, error) {
func callWithToken(method, fullURL, token string, body io.Reader) ([]byte, error) {
req, err := http.NewRequest(method, fullURL, body)
if err != nil {
return nil, errors.Wrap(err, "error creating request")
Expand Down
4 changes: 2 additions & 2 deletions prisma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestApiRequest(t *testing.T) {
x := x
t.Run(x.description, func(t *testing.T) {
p.apiURL = x.serverURL
data, err := p.DoAPIRequest(x.method, x.url, x.body)
data, err := p.Call(x.method, x.url, x.body)
if x.error != "" {
assert.EqualError(t, err, x.error, "Test case %d error check failed", i)
} else {
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestApiRequestParallel(t *testing.T) {
wg.Add(1) // nolint:gomnd
go func(wg *sync.WaitGroup, i int) {
<-start
resp, err := p.DoAPIRequest("GET", "/check", nil)
resp, err := p.Call("GET", "/check", nil)
assert.NoError(t, err, "Test case %d error check failed", i)
assert.Equal(t, "pong", string(resp), "Test case %d API object token check failed", i)
wg.Done()
Expand Down

0 comments on commit d9ec2a3

Please sign in to comment.