Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Commit 9f1d995

Browse files
authored
Merge pull request #75 from shuheiktgw/remove_authentication_service
Remove Authentication.UsingGithubToken method
2 parents d9c12e9 + 21f676d commit 9f1d995

6 files changed

+21
-201
lines changed

README.md

+2-30
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ Travis CI is migrating projects in `https://api.travis-ci.org/` to `https://api.
4040

4141
### Authentication
4242

43-
There two ways to authenticator your Travis CI client.
44-
45-
- Authentication with Travis API token
46-
- Authentication with GitHub personal access token
47-
48-
##### Authentication with Travis API token
49-
5043
```go
5144
client := travis.NewClient(travis.ApiOrgUrl, "TravisApiToken")
5245

@@ -58,23 +51,9 @@ You can issue Travis API token and hand it in to `NewClient` method directly. Yo
5851

5952
For more information on how to issue Travis CI API token, please visit [their documentation](https://docs.travis-ci.com/user/triggering-builds/).
6053

54+
### Unauthenticated client
6155

62-
63-
##### Authentication with GitHub personal access token
64-
Authentication with a Github personal access token will require some extra steps. [This GitHub help page](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) guides you thorough how to create one.
65-
66-
```go
67-
client := travis.NewClient(travis.ApiOrgUrl, "")
68-
69-
_, _, err := client.Authentication.UsingGithubToken(context.Background(), "GitHubToken")
70-
71-
// Jobs.Cancel will success
72-
_, _, err = client.Jobs.Cancel(context.Background(), 12345)
73-
```
74-
75-
#### Unauthenticated client
76-
77-
It is possible to interact with the API without authentication. However some resources are not accessible.
56+
It is possible to interact with the API without authentication. However, most resources are not accessible.
7857

7958
```go
8059
client := travis.NewClient(travis.ApiOrgUrl, "")
@@ -100,13 +79,6 @@ opt := BuildOption{Include: []string{"build.repository", "build.commits"}}
10079
build, _, err := client.Builds.Find(context.Background(), 123, &opt)
10180
```
10281

103-
## Important Note
104-
105-
There are several breaking changes between v0.1.9 and v0.2.0 mainly to support eager loading. If you have never used go-travis, please select v0.2.0 or above. If you have used go-travis v0.1.9 or below, and consider updating it, please be aware those two changes:
106-
107-
- Almost all the struct fields are pointer.
108-
- Almost all the methods interacting with the API takes an option parameter to support eager loading.
109-
11082
## Contribution
11183
Contributions are of course always welcome!
11284

authentication.go

-69
This file was deleted.

authentication_integration_test.go

-29
This file was deleted.

authentication_test.go

-70
This file was deleted.

travis.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ type Client struct {
5252

5353
// Services used to manipulate API entities
5454
Active *ActiveService
55-
Authentication *AuthenticationService
5655
BetaFeatures *BetaFeaturesService
5756
BetaMigrationRequests *BetaMigrationRequestsService
5857
Branches *BranchesService
@@ -99,7 +98,6 @@ func NewClient(baseUrl string, travisToken string) *Client {
9998
}
10099

101100
c.Active = &ActiveService{client: c}
102-
c.Authentication = &AuthenticationService{client: c}
103101
c.BetaFeatures = &BetaFeaturesService{client: c}
104102
c.BetaMigrationRequests = &BetaMigrationRequestsService{client: c}
105103
c.Branches = &BranchesService{client: c}
@@ -126,7 +124,7 @@ func NewClient(baseUrl string, travisToken string) *Client {
126124
c.User = &UserService{client: c}
127125

128126
if travisToken != "" {
129-
c.Authentication.UsingTravisToken(travisToken)
127+
c.SetToken(travisToken)
130128
}
131129

132130
return c
@@ -226,6 +224,12 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt
226224
return resp, err
227225
}
228226

227+
// SetToken formats and writes provided
228+
// Travis API token in the client's headers.
229+
func (c *Client) SetToken(token string) {
230+
c.Headers["Authorization"] = "token " + token
231+
}
232+
229233
// IsAuthenticated indicates if Authorization headers were
230234
// found in Client.Headers mapping.
231235
func (c *Client) IsAuthenticated() bool {

travis_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package travis
77

88
import (
9+
"fmt"
910
"io/ioutil"
1011
"net/http"
1112
"net/http/httptest"
@@ -212,3 +213,14 @@ func TestClient_NewRequest_with_overriding_userAgent(t *testing.T) {
212213
t.Fatalf("Wrong User-Agent: got: %s, want: %s", got, want)
213214
}
214215
}
216+
217+
func TestClient_SetToken(t *testing.T) {
218+
token := "abc123"
219+
220+
c := NewClient(ApiOrgUrl, "")
221+
c.SetToken(token)
222+
223+
if h := c.Headers["Authorization"]; h != fmt.Sprintf("token %s", token) {
224+
t.Fatalf("Client.SetToken: unexpected Authorization %s; expected %s", h, token)
225+
}
226+
}

0 commit comments

Comments
 (0)