Skip to content

Commit

Permalink
add utils newRequest/doRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
maiyama18 committed Apr 1, 2019
1 parent 229211f commit cc25b9b
Showing 1 changed file with 62 additions and 25 deletions.
87 changes: 62 additions & 25 deletions miniqiita.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -46,52 +47,88 @@ type Item struct {
}

func (c *Client) GetUserItems(ctx context.Context, userID string, page, perPage int) ([]*Item, error) {
relativePath := path.Join("users", userID, "items")
queries := map[string]string{
"page": strconv.Itoa(page),
"per_page": strconv.Itoa(perPage),
}
req, err := c.newRequest(ctx, http.MethodGet, relativePath, queries, nil, nil)
if err != nil {
return nil, err
}

// send request
var items []*Item
code, err := c.doRequest(req, &items)

switch code {
case http.StatusOK:
return items, nil
case http.StatusBadRequest:
return nil, errors.New("bad request. some parameters may be invalid")
case http.StatusNotFound:
return nil, fmt.Errorf("not found. user with id '%s' may not exist", userID)
default:
return nil, errors.New("unexpected error")
}
}

func (c *Client) newRequest(ctx context.Context, method, relativePath string, queries, headers map[string]string, reqBody io.Reader) (*http.Request, error) {
reqURL := *c.BaseURL

// set path
reqURL.Path = path.Join(reqURL.Path, "users", userID, "items")
reqURL.Path = path.Join(reqURL.Path, relativePath)

// set query
q := reqURL.Query()
q.Add("page", strconv.Itoa(page))
q.Add("per_page", strconv.Itoa(perPage))
reqURL.RawQuery = q.Encode()
if queries != nil {
q := reqURL.Query()
for k, v := range queries {
q.Add(k, v)
}
reqURL.RawQuery = q.Encode()
}

// instantiate request
req, err := http.NewRequest(http.MethodGet, reqURL.String(), nil)
req, err := http.NewRequest(method, reqURL.String(), reqBody)
if err != nil {
return nil, err
}

// set header
req.Header.Set("User-Agent", "qiita-go-client")
req.Header.Set("Authorization", "Bearer "+c.Token)
req.Header.Set("Content-Type", "application/json")
if headers != nil {
for k, v := range headers {
req.Header.Set(k, v)
}
}

// set context
req = req.WithContext(ctx)

// send request
return req, nil
}

func (c *Client) doRequest(req *http.Request, respBody interface{}) (int, error) {
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
return 0, err
}
defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK:
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var items []*Item
if err := json.Unmarshal(bodyBytes, &items); err != nil {
return nil, err
}
return items, nil
case http.StatusBadRequest:
return nil, errors.New("bad request. some parameters may be invalid")
case http.StatusNotFound:
return nil, fmt.Errorf("not found. user with id '%s' may not exist", userID)
default:
return nil, errors.New("unexpected error")
if resp.StatusCode < 200 || 300 <= resp.StatusCode {
return resp.StatusCode, nil
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}

if err := json.Unmarshal(bodyBytes, respBody); err != nil {
return 0, err
}

return resp.StatusCode, nil
}

0 comments on commit cc25b9b

Please sign in to comment.