Skip to content

Updated mod + deprecated ioutil, success return #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion path/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/path-network/go-path
module github.com/path-network/go-path/path

go 1.13
23 changes: 21 additions & 2 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions path/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down