@@ -19,6 +19,24 @@ const DefaultBaseURL = "https://api.github.com"
1919// Set to values > 0 to control verbosity, for debugging.
2020var VERBOSITY = 0
2121
22+ // DoAuthRequest ...
23+ //
24+ // TODO: This function is amazingly ugly (separate headers, token, no API
25+ // URL constructions, et cetera).
26+ func DoAuthRequest (method , url , mime , token string , headers map [string ]string , body io.Reader ) (* http.Response , error ) {
27+ req , err := newAuthRequest (method , url , mime , token , headers , body )
28+ if err != nil {
29+ return nil , err
30+ }
31+
32+ resp , err := http .DefaultClient .Do (req )
33+ if err != nil {
34+ return nil , err
35+ }
36+
37+ return resp , nil
38+ }
39+
2240// Client collects a few options that can be set when contacting the GitHub
2341// API, such as authorization tokens. Methods called on Client will supply
2442// these options when calling the API.
@@ -209,6 +227,45 @@ func (c Client) getPaginated(uri string) (io.ReadCloser, error) {
209227 return r , nil
210228}
211229
230+ // Create a new request that sends the auth token.
231+ func newAuthRequest (method , url , mime , token string , headers map [string ]string , body io.Reader ) (* http.Request , error ) {
232+ vprintln ("creating request:" , method , url , mime , token )
233+
234+ var n int64 // content length
235+ var err error
236+ if f , ok := body .(* os.File ); ok {
237+ // Retrieve the content-length and buffer up if necessary.
238+ body , n , err = materializeFile (f )
239+ if err != nil {
240+ return nil , err
241+ }
242+ }
243+
244+ req , err := http .NewRequest (method , url , body )
245+ if err != nil {
246+ return nil , err
247+ }
248+
249+ // net/http automatically does this if req.Body is of type
250+ // (bytes.Reader|bytes.Buffer|strings.Reader). Sadly, we also need to
251+ // handle *os.File.
252+ if n != 0 {
253+ vprintln ("setting content-length to" , n )
254+ req .ContentLength = n
255+ }
256+
257+ if mime != "" {
258+ req .Header .Set ("Content-Type" , mime )
259+ }
260+ req .Header .Set ("Authorization" , "token " + token )
261+
262+ for k , v := range headers {
263+ req .Header .Set (k , v )
264+ }
265+
266+ return req , nil
267+ }
268+
212269// nextLink returns the HTTP header Link annotated with 'next', "" otherwise.
213270func nextLink (links linkheader.Links ) string {
214271 for _ , link := range links {
0 commit comments