Skip to content

Commit

Permalink
Don't always send query options
Browse files Browse the repository at this point in the history
  • Loading branch information
albert-wang committed Apr 12, 2016
1 parent 6547ade commit 3c2d3e1
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions twitch.go
Expand Up @@ -5,11 +5,10 @@ import (
"fmt"
"io/ioutil"
"net/http"

"github.com/google/go-querystring/query"
"net/url"
)

const baseUrl = "https://api.twitch.tv/kraken/"
const baseUrl = "https://api.twitch.tv/kraken"

type TwitchClient struct {
httpClient *http.Client
Expand All @@ -19,6 +18,7 @@ type RequestOptions struct {
Limit int64 `url:"limit"`
Offset int64 `url:"offset"`
Direction string `url:"direction"`
Nonce int64 `url:"_"`
}

func NewTwitchClient(httpClient *http.Client) TwitchClient {
Expand All @@ -27,18 +27,34 @@ func NewTwitchClient(httpClient *http.Client) TwitchClient {
}
}

func (client *TwitchClient) getRequest(endpoint string, options *RequestOptions, v interface{}) error {
url := baseUrl + endpoint
func (client *TwitchClient) getRequest(endpoint string, options *RequestOptions, out interface{}) error {
targetUrl := baseUrl + endpoint

if options != nil {
v, err := query.Values(options)
if err != nil {
return err
v := url.Values{}

if options.Direction != "" {
v.Add("direction", options.Direction)
}

if options.Limit != 0 {
v.Add("limit", fmt.Sprintf("%d", options.Limit))
}

if options.Offset != 0 {
v.Add("offset", fmt.Sprintf("%d", options.Offset))
}

if options.Nonce != 0 {
v.Add("_", fmt.Sprintf("%d", options.Nonce))
}
url += "?" + v.Encode()

targetUrl += "?" + v.Encode()
}

req, _ := http.NewRequest("GET", url, nil)
fmt.Println(targetUrl)

req, _ := http.NewRequest("GET", targetUrl, nil)
req.Header.Set("Accept", "application/vnd.twitchtv.v3+json")
res, err := client.httpClient.Do(req)
if err != nil {
Expand All @@ -51,7 +67,7 @@ func (client *TwitchClient) getRequest(endpoint string, options *RequestOptions,
}

body, _ := ioutil.ReadAll(res.Body)
err = json.Unmarshal(body, v)
err = json.Unmarshal(body, out)
if err != nil {
return err
}
Expand Down

0 comments on commit 3c2d3e1

Please sign in to comment.