Skip to content

Commit

Permalink
client.go:
Browse files Browse the repository at this point in the history
  * Initialise() doc string updated
  * Auth methods now call Initialise() automatically

requests.go:
  * json.NewDecoder moved into successful block
  * rawResponseBody is now printed correctly

README.md
  * Removed manual call to Initialise()
  * Added error checking for authentication
  • Loading branch information
theriverman committed Sep 27, 2020
1 parent 9b013cc commit c56b315
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ venv.bak/
.vscode/settings.json

# Development files
dev
contribute/*
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,14 @@ func main() {
HTTPClient: &http.Client{},
}

// Initialise client
err := client.Initialise()
if err != nil {
log.Fatalln(err)
}

// Authenticate (get/set the JWT)
client.AuthByCredentials(&taiga.Credentials{
Type: "normal", // normal, ldap, gitlab, etc...
Username: "my_pretty_username",
// Authenticate (get/set Token)
if err := client.AuthByCredentials(&taiga.Credentials{
Type: "normal",
Username: "admin",
Password: "123123",
})
}); err != nil {
panic(err)
}

// Get /users/me
me, _ := client.User.Me()
Expand Down
10 changes: 8 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ func (c *Client) HasDefaultProject() bool {
}

// Initialise returns a new Taiga Client which is the entrypoint of the driver
// Initialise() is automatically called by the `AuthByCredentials` and `AuthByToken` methods.
// If you, for some reason, would like to manually set the Client.Token field, then Initialise() must be called manually!
func (c *Client) Initialise() error {
// Skip if already Initialised
if c.isInitialised {
return nil
}
// Taiga.Client safety guards
if len(c.BaseURL) < len("http://") { // compares for a minimum of len("http://")
return fmt.Errorf("BaseURL is not set or invalid")
Expand Down Expand Up @@ -146,7 +152,7 @@ func (c *Client) Initialise() error {
// AuthByCredentials authenticates to Taiga using the provided basic credentials
func (c *Client) AuthByCredentials(credentials *Credentials) error {
if !c.isInitialised {
return fmt.Errorf("Client not initialised")
return c.Initialise()
}

if len(credentials.Type) <= 1 {
Expand All @@ -165,7 +171,7 @@ func (c *Client) AuthByCredentials(credentials *Credentials) error {
// AuthByToken authenticates to Taiga using provided Token by requesting users/me
func (c *Client) AuthByToken(tokenType, token string) error {
if !c.isInitialised {
return fmt.Errorf("Client not initialised")
return c.Initialise()
}
c.TokenType = tokenType
c.Token = token
Expand Down
11 changes: 8 additions & 3 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func newfileUploadRequest(c *Client, url string, attachment *Attachment, tgObjec
return &responseBody, nil
}

return nil, fmt.Errorf("Request Failed. Returned body was:\n %s", rawResponseBody)
return nil, fmt.Errorf("Request Failed. Returned body was:\n %s", string(rawResponseBody))
}

func newRawRequest(RequestType string, c *Client, ResponseBody interface{}, URL string, Payload interface{}) (*http.Response, error) {
Expand Down Expand Up @@ -207,18 +207,23 @@ func newRawRequest(RequestType string, c *Client, ResponseBody interface{}, URL
defer resp.Body.Close()

// Evaluate response status code
decoder := json.NewDecoder(resp.Body)
if SuccessfulHTTPRequest(resp) {
if resp.StatusCode == http.StatusNoContent { // There's no body returned for 204 responses
return resp, nil
}
// We expect content so convert response JSON string to struct
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&ResponseBody)
if err != nil {
return nil, err
}
return resp, nil
}

return nil, fmt.Errorf("Request Failed. Returned body was:\n %s", resp.Body)
rawResponseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return nil, fmt.Errorf("Request Failed. Returned body was:\n %s", string(rawResponseBody))
}

0 comments on commit c56b315

Please sign in to comment.