77 "io"
88 "io/ioutil"
99 "net/http"
10- "net/url"
1110 "regexp"
1211 "strings"
1312
@@ -17,14 +16,18 @@ import (
1716// ClientOption represents an argument to NewClient
1817type ClientOption = func (http.RoundTripper ) http.RoundTripper
1918
20- // NewClient initializes a Client
21- func NewClient (opts ... ClientOption ) * Client {
19+ // NewHTTPClient initializes an http. Client
20+ func NewHTTPClient (opts ... ClientOption ) * http. Client {
2221 tr := http .DefaultTransport
2322 for _ , opt := range opts {
2423 tr = opt (tr )
2524 }
26- http := & http.Client {Transport : tr }
27- client := & Client {http : http }
25+ return & http.Client {Transport : tr }
26+ }
27+
28+ // NewClient initializes a Client
29+ func NewClient (opts ... ClientOption ) * Client {
30+ client := & Client {http : NewHTTPClient (opts ... )}
2831 return client
2932}
3033
@@ -208,102 +211,6 @@ func (c Client) REST(method string, p string, body io.Reader, data interface{})
208211 return nil
209212}
210213
211- // DirectRequest is a low-level interface to making generic API requests
212- func (c Client ) DirectRequest (method string , p string , params interface {}, headers []string ) (* http.Response , error ) {
213- url := "https://api.github.com/" + p
214- var body io.Reader
215- var bodyIsJSON bool
216- isGraphQL := p == "graphql"
217-
218- switch pp := params .(type ) {
219- case map [string ]interface {}:
220- if strings .EqualFold (method , "GET" ) {
221- url = addQuery (url , pp )
222- } else {
223- if isGraphQL {
224- pp = groupGraphQLVariables (pp )
225- }
226- b , err := json .Marshal (pp )
227- if err != nil {
228- return nil , fmt .Errorf ("error serializing parameters: %w" , err )
229- }
230- body = bytes .NewBuffer (b )
231- bodyIsJSON = true
232- }
233- case io.Reader :
234- body = pp
235- default :
236- return nil , fmt .Errorf ("unrecognized parameters type: %v" , params )
237- }
238-
239- req , err := http .NewRequest (method , url , body )
240- if err != nil {
241- return nil , err
242- }
243-
244- if bodyIsJSON {
245- req .Header .Set ("Content-Type" , "application/json; charset=utf-8" )
246- }
247- for _ , h := range headers {
248- idx := strings .IndexRune (h , ':' )
249- if idx == - 1 {
250- return nil , fmt .Errorf ("header %q requires a value separated by ':'" , h )
251- }
252- req .Header .Set (h [0 :idx ], strings .TrimSpace (h [idx + 1 :]))
253- }
254-
255- return c .http .Do (req )
256- }
257-
258- func groupGraphQLVariables (params map [string ]interface {}) map [string ]interface {} {
259- topLevel := make (map [string ]interface {})
260- variables := make (map [string ]interface {})
261-
262- for key , val := range params {
263- switch key {
264- case "query" :
265- topLevel [key ] = val
266- default :
267- variables [key ] = val
268- }
269- }
270-
271- if len (variables ) > 0 {
272- topLevel ["variables" ] = variables
273- }
274- return topLevel
275- }
276-
277- func addQuery (path string , params map [string ]interface {}) string {
278- if len (params ) == 0 {
279- return path
280- }
281-
282- query := url.Values {}
283- for key , value := range params {
284- switch v := value .(type ) {
285- case string :
286- query .Add (key , v )
287- case []byte :
288- query .Add (key , string (v ))
289- case nil :
290- query .Add (key , "" )
291- case int :
292- query .Add (key , fmt .Sprintf ("%d" , v ))
293- case bool :
294- query .Add (key , fmt .Sprintf ("%v" , v ))
295- default :
296- panic (fmt .Sprintf ("unknown type %v" , v ))
297- }
298- }
299-
300- sep := "?"
301- if strings .ContainsRune (path , '?' ) {
302- sep = "&"
303- }
304- return path + sep + query .Encode ()
305- }
306-
307214func handleResponse (resp * http.Response , data interface {}) error {
308215 success := resp .StatusCode >= 200 && resp .StatusCode < 300
309216
0 commit comments