* Add NewFromToken convenience method to init client
Fixes#296
Adds a new convenience method for creating a new instance of `godo` from
just an API token.
Usage
Right now, it takes quite a few steps to create a `godo` instance:
```go
type TokenSource struct {
AccessToken string
}
func (t *TokenSource) Token() (*oauth2.Token, error) {
token := &oauth2.Token{
AccessToken: t.AccessToken,
}
return token, nil
}
func main() {
tokenSource := &TokenSource{
AccessToken: pat,
}
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
}
```
Now, it takes just a single line:
```go
func main() {
client := godo.NewFromToken("my-token")
}
```
The intention of this is to make initializing new clients easier. It
also has an additional benefit of not forcing users to have to import
the `golang.org/x/oauth2` dependency just to initialize new `godo`
instances.
* Add NewFromToken convenience method to init client
Fixes#296
Adds a new convenience method for creating a new instance of `godo` from
just an API token.
Usage
Right now, it takes quite a few steps to create a `godo` instance:
```go
type TokenSource struct {
AccessToken string
}
func (t *TokenSource) Token() (*oauth2.Token, error) {
token := &oauth2.Token{
AccessToken: t.AccessToken,
}
return token, nil
}
func main() {
tokenSource := &TokenSource{
AccessToken: pat,
}
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
}
```
Now, it takes just a single line:
```go
func main() {
client := godo.NewFromToken("my-token")
}
```
The intention of this is to make initializing new clients easier. It
also has an additional benefit of not forcing users to have to import
the `golang.org/x/oauth2` dependency just to initialize new `godo`
instances.
* Remove our own tokenSource type
It turns out you can use an `oauth2.Config` as a token source, so this
removes our own tokenSource type in favour of that one.
Co-authored-by: Zach Gershman <zachgersh@users.noreply.github.com>
* Move DoRequest* to godo package with stdlib context
* Use stdlib context package in place of godo context.
* Remove Go 1.6 from TravisCI configuration.
* Use stdlib context in util and tests instead of godo context.
* Add in Go 1.8, 1.9, and 1.10 to TravisCI configuration.
* Put 1.10 in quotes.
This patch updates the godo package to use a portable Go context -- a
Context that works with Go versions prior to Go 1.7 as well as Go 1.7
and onwards.
> Go 1.7 adds support for maps using keys with integer types: the
> encoding uses a quoted decimal representation as the JSON key.
in: https://tip.golang.org/doc/go1.7, under encoding/json
When adding options to a path, don't drop existing keys. This will enable image pagination when there is a type key to work. The bug was trying to addOptions to an existing path with query values. Previously if a path `/path?type=alpha` was passed, key `page=2` would be appeneded, but `type=alpha` would be dropped.
This makes it possible for consumers of the package to debug API calls made to the DO APIs.
Usage:
client.OnRequestCompleted(func(req *http.Request, resp *http.Response) {
// ... e.g. use httputil.DumpRequest / httputil.DumpResponse
})