Skip to content

Commit

Permalink
Pass golang-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
bloveless committed May 4, 2020
1 parent 2b37fb0 commit bc469db
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 44 deletions.
16 changes: 14 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func (c Client) OAuthRequestTokenGet(input OAuthRequestTokenInput) (OAuthRequest
var output = OAuthRequestTokenOutput{}
var decoder = schema.NewDecoder()
err = decoder.Decode(&output, values)
if err != nil {
return OAuthRequestTokenOutput{}, err
}

return output, nil
}
Expand All @@ -97,6 +100,9 @@ func (c Client) OAuthAccessTokenGet(input OAuthAccessTokenInput) (OAuthAccessTok
var output = OAuthAccessTokenOutput{}
var decoder = schema.NewDecoder()
err = decoder.Decode(&output, values)
if err != nil {
return OAuthAccessTokenOutput{}, err
}

return output, nil
}
Expand All @@ -118,7 +124,10 @@ func (c Client) StatusesUpdatePost(input StatusesUpdateInput) (StatusesUpdateOut
}

output := StatusesUpdateOutput{}
json.Unmarshal(resBytes, &output)
err = json.Unmarshal(resBytes, &output)
if err != nil {
return StatusesUpdateOutput{}, err
}

return output, nil
}
Expand Down Expand Up @@ -153,7 +162,10 @@ func (c Client) StatusesUserTimelineGet(input StatusesUserTimelineInput) ([]Stat
}

var output []StatusesUserTimelineOutput
json.Unmarshal(resBytes, &output)
err = json.Unmarshal(resBytes, &output)
if err != nil {
return []StatusesUserTimelineOutput{}, err
}

return output, nil
}
31 changes: 21 additions & 10 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ func processParams(input interface{}) url.Values {

if value != nil {
// convert to string based on underlying type
switch value.(type) {
switch value := value.(type) {
case string:
params.Add(name, value.(string))
params.Add(name, value)
case bool:
params.Add(name, strconv.FormatBool(value.(bool)))
params.Add(name, strconv.FormatBool(value))
case int:
params.Add(name, strconv.FormatInt(int64(value.(int)), 10))
params.Add(name, strconv.FormatInt(int64(value), 10))
case int64:
params.Add(name, strconv.FormatInt(value.(int64), 10))
params.Add(name, strconv.FormatInt(value, 10))
case float64:
params.Add(name, strconv.FormatFloat(value.(float64), 'f', -1, 64))
params.Add(name, strconv.FormatFloat(value, 'f', -1, 64))
}
}
}
Expand Down Expand Up @@ -141,6 +141,9 @@ func (c Client) getSignedRequest(method, uri string, params url.Values) (*http.R
u.RawQuery = sr.params.Encode()

req, err = http.NewRequest(sr.method, u.String(), nil)
if err != nil {
return nil, err
}
}

if err != nil {
Expand Down Expand Up @@ -191,14 +194,22 @@ func (c Client) signature(sr signatureRequest) (string, error) {

signingKey := url.QueryEscape(c.OAuthConsumerSecret) + "&" + url.QueryEscape(c.OAuthAccessTokenSecret)

return calculateSignature(signatureBaseString, signingKey), nil
sig, err := calculateSignature(signatureBaseString, signingKey)
if err != nil {
return "", err
}

return sig, nil
}

func calculateSignature(base, key string) string {
func calculateSignature(base, key string) (string, error) {
hash := hmac.New(sha1.New, []byte(key))
hash.Write([]byte(base))
_, err := hash.Write([]byte(base))
if err != nil {
return "", err
}
signature := hash.Sum(nil)
return base64.StdEncoding.EncodeToString(signature)
return base64.StdEncoding.EncodeToString(signature), nil
}

type headerParameters struct {
Expand Down
37 changes: 5 additions & 32 deletions request_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
package tweetgo

import (
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
)

type mockClient struct {
responses []*http.Response
}

func (c *mockClient) Do(req *http.Request) (*http.Response, error) {
if len(c.responses) > 0 {
response := c.responses[0]
c.responses = c.responses[1:]
return response, nil
}

return nil, errors.New("no response found to return")
}

type mockNonce struct{}

func (n mockNonce) Generate() string {
return "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg\n"
}

type mockCurrentTimer struct{}

func (t mockCurrentTimer) GetCurrentTime() int64 {
return 1588432148
}

func TestCanProcessParamsAndOmitNilValues(t *testing.T) {
type testStruct struct {
TestString *string `schema:"test_string"`
Expand Down Expand Up @@ -128,8 +101,8 @@ func TestCorrectlyCalculatesSignatureForStatusesUpdate(t *testing.T) {
uri: "https://api.twitter.com/1.1/statuses/update.json",
nonce: "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg",
timestamp: "1318622958",
params: url.Values{
"status": {"Hello Ladies + Gentlemen, a signed OAuth request!"},
params: url.Values{
"status": {"Hello Ladies + Gentlemen, a signed OAuth request!"},
"include_entities": {"true"},
},
}
Expand Down Expand Up @@ -159,7 +132,7 @@ func TestCorrectlyCalculatesSignatureForStatusesUpdateWithGetParameters(t *testi
uri: "https://api.twitter.com/1.1/statuses/update.json?include_entities=true",
nonce: "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg",
timestamp: "1318622958",
params: url.Values{
params: url.Values{
"status": {"Hello Ladies + Gentlemen, a signed OAuth request!"},
},
}
Expand Down Expand Up @@ -190,8 +163,8 @@ func TestCorrectlyCalculatesSignatureForStatusesUpdateWithDuplicatedGetParameter
uri: "https://api.twitter.com/1.1/statuses/update.json?include_entities=true",
nonce: "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg",
timestamp: "1318622958",
params: url.Values{
"status": {"Hello Ladies + Gentlemen, a signed OAuth request!"},
params: url.Values{
"status": {"Hello Ladies + Gentlemen, a signed OAuth request!"},
"include_entities": {"true"},
},
}
Expand Down

0 comments on commit bc469db

Please sign in to comment.