|
| 1 | +package qiita |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + // "io/ioutil" |
| 8 | + "fmt" |
| 9 | + "github.com/google/go-querystring/query" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "reflect" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + defaultBaseURL = "https://qiita.com/api/v2/" |
| 17 | + userAgent = "go-qiita" |
| 18 | +) |
| 19 | + |
| 20 | +type ListOptions struct { |
| 21 | + Page int `url:"page,omitempty"` |
| 22 | + PerPage int `url:"per_page,omitempty"` |
| 23 | +} |
| 24 | + |
| 25 | +type Response struct { |
| 26 | + *http.Response |
| 27 | + |
| 28 | + NextPage int |
| 29 | + PrevPage int |
| 30 | + FirstPage int |
| 31 | + LastPage int |
| 32 | +} |
| 33 | + |
| 34 | +type Client struct { |
| 35 | + client *http.Client |
| 36 | + BaseURL *url.URL |
| 37 | + UserAgent string |
| 38 | + |
| 39 | + Items *ItemsService |
| 40 | +} |
| 41 | + |
| 42 | +func addOptions(s string, opt interface{}) (string, error) { |
| 43 | + v := reflect.ValueOf(opt) |
| 44 | + if v.Kind() == reflect.Ptr && v.IsNil() { |
| 45 | + return s, nil |
| 46 | + } |
| 47 | + |
| 48 | + u, err := url.Parse(s) |
| 49 | + if err != nil { |
| 50 | + return s, err |
| 51 | + } |
| 52 | + |
| 53 | + qs, err := query.Values(opt) |
| 54 | + if err != nil { |
| 55 | + return s, err |
| 56 | + } |
| 57 | + |
| 58 | + u.RawQuery = qs.Encode() |
| 59 | + return u.String(), nil |
| 60 | +} |
| 61 | + |
| 62 | +func NewClient(httpClient *http.Client) *Client { |
| 63 | + if httpClient == nil { |
| 64 | + httpClient = http.DefaultClient |
| 65 | + } |
| 66 | + baseURL, _ := url.Parse(defaultBaseURL) |
| 67 | + fmt.Println(baseURL) |
| 68 | + |
| 69 | + c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent} |
| 70 | + c.Items = &ItemsService{client: c} |
| 71 | + return c |
| 72 | +} |
| 73 | + |
| 74 | +func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) { |
| 75 | + rel, err := url.Parse(urlStr) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + u := c.BaseURL.ResolveReference(rel) |
| 81 | + |
| 82 | + var buf io.ReadWriter |
| 83 | + if body != nil { |
| 84 | + buf = new(bytes.Buffer) |
| 85 | + err := json.NewEncoder(buf).Encode(body) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + req, err := http.NewRequest(method, u.String(), buf) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + req.Header.Add("Accept", "application/json") |
| 97 | + if c.UserAgent != "" { |
| 98 | + req.Header.Add("User-Agent", c.UserAgent) |
| 99 | + } |
| 100 | + return req, nil |
| 101 | +} |
| 102 | + |
| 103 | +func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) { |
| 104 | + resp, err := c.client.Do(req) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + defer resp.Body.Close() |
| 110 | + |
| 111 | + // err = CheckResponse(resp) |
| 112 | + // if err != nil { |
| 113 | + // // even though there was an error, we still return the response |
| 114 | + // // in case the caller wants to inspect it further |
| 115 | + // return resp, err |
| 116 | + // } |
| 117 | + |
| 118 | + if v != nil { |
| 119 | + if w, ok := v.(io.Writer); ok { |
| 120 | + io.Copy(w, resp.Body) |
| 121 | + } else { |
| 122 | + err = json.NewDecoder(resp.Body).Decode(v) |
| 123 | + } |
| 124 | + } |
| 125 | + return resp, err |
| 126 | +} |
0 commit comments