Skip to content
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

Work with context.Context & deprecates io/ioutil #79

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func main() {
path := "https://api.twitter.com/1.1/statuses/home_timeline.json?count=2"
resp, _ := httpClient.Get(path)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Raw Response Body:\n%v\n", string(body))
}
```
Expand Down
6 changes: 3 additions & 3 deletions auther.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package oauth1
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"sort"
Expand Down Expand Up @@ -207,7 +207,7 @@ func collectParameters(req *http.Request, oauthParams map[string]string) (map[st
}
if req.Body != nil && req.Header.Get(contentType) == formContentType {
// reads data to a []byte, draining req.Body
b, err := ioutil.ReadAll(req.Body)
b, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
Expand All @@ -220,7 +220,7 @@ func collectParameters(req *http.Request, oauthParams map[string]string) (map[st
params[key] = value[0]
}
// reinitialize Body with ReadCloser over the []byte
req.Body = ioutil.NopCloser(bytes.NewReader(b))
req.Body = io.NopCloser(bytes.NewReader(b))
}
for key, value := range oauthParams {
// according to 3.4.1.3.1. the realm parameter is excluded
Expand Down
14 changes: 7 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -65,8 +65,8 @@ func NewClient(ctx context.Context, config *Config, token *Token) *http.Client {
// oauth_callback_confirmed is true. Returns the request token and secret
// (temporary credentials).
// See RFC 5849 2.1 Temporary Credentials.
func (c *Config) RequestToken() (requestToken, requestSecret string, err error) {
req, err := http.NewRequest("POST", c.Endpoint.RequestTokenURL, nil)
func (c *Config) RequestToken(ctx context.Context) (requestToken, requestSecret string, err error) {
req, err := http.NewRequestWithContext(ctx, "POST", c.Endpoint.RequestTokenURL, nil)
if err != nil {
return "", "", err
}
Expand All @@ -81,7 +81,7 @@ func (c *Config) RequestToken() (requestToken, requestSecret string, err error)
// when err is nil, resp contains a non-nil resp.Body which must be closed
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", "", fmt.Errorf("oauth1: error reading Body: %v", err)
}
Expand Down Expand Up @@ -144,8 +144,8 @@ func ParseAuthorizationCallback(req *http.Request) (requestToken, verifier strin
// Endpoint AccessTokenURL. Returns the access token and secret (token
// credentials).
// See RFC 5849 2.3 Token Credentials.
func (c *Config) AccessToken(requestToken, requestSecret, verifier string) (accessToken, accessSecret string, err error) {
req, err := http.NewRequest("POST", c.Endpoint.AccessTokenURL, nil)
func (c *Config) AccessToken(ctx context.Context, requestToken, requestSecret, verifier string) (accessToken, accessSecret string, err error) {
req, err := http.NewRequestWithContext(ctx, "POST", c.Endpoint.AccessTokenURL, nil)
if err != nil {
return "", "", err
}
Expand All @@ -160,7 +160,7 @@ func (c *Config) AccessToken(requestToken, requestSecret, verifier string) (acce
// when err is nil, resp contains a non-nil resp.Body which must be closed
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", "", fmt.Errorf("oauth1: error reading Body: %v", err)
}
Expand Down
18 changes: 9 additions & 9 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestConfigRequestToken(t *testing.T) {
RequestTokenURL: server.URL,
},
}
requestToken, requestSecret, err := config.RequestToken()
requestToken, requestSecret, err := config.RequestToken(context.TODO())
assert.Nil(t, err)
assert.Equal(t, expectedToken, requestToken)
assert.Equal(t, expectedSecret, requestSecret)
Expand All @@ -118,7 +118,7 @@ func TestConfigRequestToken_InvalidRequestTokenURL(t *testing.T) {
RequestTokenURL: "http://wrong.com/oauth/request_token",
},
}
requestToken, requestSecret, err := config.RequestToken()
requestToken, requestSecret, err := config.RequestToken(context.TODO())
assert.NotNil(t, err)
assert.Equal(t, "", requestToken)
assert.Equal(t, "", requestSecret)
Expand All @@ -139,7 +139,7 @@ func TestConfigRequestToken_CallbackNotConfirmed(t *testing.T) {
RequestTokenURL: server.URL,
},
}
requestToken, requestSecret, err := config.RequestToken()
requestToken, requestSecret, err := config.RequestToken(context.TODO())
if assert.Error(t, err) {
assert.Equal(t, "oauth1: oauth_callback_confirmed was not true", err.Error())
}
Expand All @@ -156,7 +156,7 @@ func TestConfigRequestToken_CannotParseBody(t *testing.T) {
RequestTokenURL: server.URL,
},
}
requestToken, requestSecret, err := config.RequestToken()
requestToken, requestSecret, err := config.RequestToken(context.TODO())
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "invalid URL escape")
}
Expand All @@ -176,7 +176,7 @@ func TestConfigRequestToken_MissingTokenOrSecret(t *testing.T) {
RequestTokenURL: server.URL,
},
}
requestToken, requestSecret, err := config.RequestToken()
requestToken, requestSecret, err := config.RequestToken(context.TODO())
if assert.Error(t, err) {
assert.Equal(t, "oauth1: Response missing oauth_token or oauth_token_secret", err.Error())
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestConfigAccessToken(t *testing.T) {
AccessTokenURL: server.URL,
},
}
accessToken, accessSecret, err := config.AccessToken("request_token", "request_secret", expectedVerifier)
accessToken, accessSecret, err := config.AccessToken(context.TODO(), "request_token", "request_secret", expectedVerifier)
assert.Nil(t, err)
assert.Equal(t, expectedToken, accessToken)
assert.Equal(t, expectedSecret, accessSecret)
Expand All @@ -238,7 +238,7 @@ func TestConfigAccessToken_InvalidAccessTokenURL(t *testing.T) {
AccessTokenURL: "http://wrong.com/oauth/access_token",
},
}
accessToken, accessSecret, err := config.AccessToken("any_token", "any_secret", "any_verifier")
accessToken, accessSecret, err := config.AccessToken(context.TODO(), "any_token", "any_secret", "any_verifier")
assert.NotNil(t, err)
assert.Equal(t, "", accessToken)
assert.Equal(t, "", accessSecret)
Expand All @@ -253,7 +253,7 @@ func TestConfigAccessToken_CannotParseBody(t *testing.T) {
AccessTokenURL: server.URL,
},
}
accessToken, accessSecret, err := config.AccessToken("any_token", "any_secret", "any_verifier")
accessToken, accessSecret, err := config.AccessToken(context.TODO(), "any_token", "any_secret", "any_verifier")
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "invalid URL escape")
}
Expand All @@ -272,7 +272,7 @@ func TestConfigAccessToken_MissingTokenOrSecret(t *testing.T) {
AccessTokenURL: server.URL,
},
}
accessToken, accessSecret, err := config.AccessToken("request_token", "request_secret", expectedVerifier)
accessToken, accessSecret, err := config.AccessToken(context.TODO(), "request_token", "request_secret", expectedVerifier)
if assert.Error(t, err) {
assert.Equal(t, "oauth1: Response missing oauth_token or oauth_token_secret", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Use an access Token to make authorized requests on behalf of a user.
path := "https://api.twitter.com/1.1/statuses/home_timeline.json?count=2"
resp, _ := httpClient.Get(path)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Raw Response Body:\n%v\n", string(body))
}

Expand Down
4 changes: 2 additions & 2 deletions examples/tumblr-request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"os"

"github.com/dghubble/oauth1"
Expand All @@ -29,7 +29,7 @@ func main() {
path := "https://api.tumblr.com/v2/user/info"
resp, _ := httpClient.Get(path)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Raw Response Body:\n%v\n", string(body))

// note: Tumblr requires OAuth signed requests for particular endpoints,
Expand Down
4 changes: 2 additions & 2 deletions examples/twitter-request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"os"

"github.com/dghubble/go-twitter/twitter"
Expand All @@ -29,7 +29,7 @@ func main() {
path := "https://api.twitter.com/1.1/statuses/home_timeline.json?count=2"
resp, _ := httpClient.Get(path)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Raw Response Body:\n%v\n", string(body))

// Nicer: Pass OAuth1 client to go-twitter API
Expand Down