diff --git a/path/go.mod b/path/go.mod index 99193ac..bc27246 100644 --- a/path/go.mod +++ b/path/go.mod @@ -1,3 +1,3 @@ -module github.com/path-network/go-path +module github.com/path-network/go-path/path go 1.13 diff --git a/path/path.go b/path/path.go index bfa6dd4..726356f 100644 --- a/path/path.go +++ b/path/path.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -92,6 +92,15 @@ func (client *Client) ChangePassword(oldPassword, newPassword string) error { return err } +// AuthorizationTokenType represents the type of authorization token. +type AuthorizationTokenType string + +// AuthorizationToken types +const ( + // Default token type Bearer + AuthorizationTokenBearer AuthorizationTokenType = "Bearer" +) + // handleRequest executes the provided request and does all of the error processing. If a successful HTTP status code was received, // it returns the clean request body. func (client *Client) handleRequest(req *http.Request) ([]byte, error) { @@ -106,7 +115,7 @@ func (client *Client) handleRequest(req *http.Request) ([]byte, error) { return nil, err } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return body, err } @@ -146,6 +155,10 @@ func (client *Client) handleRequest(req *http.Request) ([]byte, error) { return body, errors.New(errMsg.String()) } + case http.StatusCreated: + { + return body, nil + } default: { return body, errors.New(fmt.Sprintf("Received unexpected status code: %d", resp.StatusCode)) @@ -478,5 +491,11 @@ func NewClient(tokenRequest AccessTokenRequest) (Client, error) { err := client.GetToken(tokenRequest) + switch client.token.TokenType { + case "bearer": + client.token.TokenType = AuthorizationTokenBearer + default: + client.token.TokenType = AuthorizationTokenBearer + } return client, err } diff --git a/path/token.go b/path/token.go index 67026e0..d3d4ea6 100644 --- a/path/token.go +++ b/path/token.go @@ -2,17 +2,17 @@ package path // Token holds the data returned from the /token endpoint after successful authentication type Token struct { - AccessToken string `json:"access_token"` - TokenType string `json:"token_type"` + AccessToken string `json:"access_token"` + TokenType AuthorizationTokenType `json:"token_type"` } // AccessTokenRequest holds the necessary data that the /token endpoint expects type AccessTokenRequest struct { - GrantType string `json:"grant_type"` + GrantType string `json:"grant_type"` // Required - Username string `json:"username"` - Password string `json:"password"` + Username string `json:"username"` + Password string `json:"password"` Scope string `json:"scope"` ClientID string `json:"client_id"`