@@ -16,14 +16,18 @@ import (
1616// ClientOption represents an argument to NewClient
1717type ClientOption = func (http.RoundTripper ) http.RoundTripper
1818
19- // NewClient initializes a Client
20- func NewClient (opts ... ClientOption ) * Client {
19+ // NewHTTPClient initializes an http. Client
20+ func NewHTTPClient (opts ... ClientOption ) * http. Client {
2121 tr := http .DefaultTransport
2222 for _ , opt := range opts {
2323 tr = opt (tr )
2424 }
25- http := & http.Client {Transport : tr }
26- 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 ... )}
2731 return client
2832}
2933
@@ -145,6 +149,45 @@ func (gr GraphQLErrorResponse) Error() string {
145149 return fmt .Sprintf ("graphql error: '%s'" , strings .Join (errorMessages , ", " ))
146150}
147151
152+ // Returns whether or not scopes are present, appID, and error
153+ func (c Client ) HasScopes (wantedScopes ... string ) (bool , string , error ) {
154+ url := "https://api.github.com/user"
155+ req , err := http .NewRequest ("GET" , url , nil )
156+ if err != nil {
157+ return false , "" , err
158+ }
159+
160+ req .Header .Set ("Content-Type" , "application/json; charset=utf-8" )
161+
162+ res , err := c .http .Do (req )
163+ if err != nil {
164+ return false , "" , err
165+ }
166+ defer res .Body .Close ()
167+
168+ if res .StatusCode != 200 {
169+ return false , "" , handleHTTPError (res )
170+ }
171+
172+ appID := res .Header .Get ("X-Oauth-Client-Id" )
173+ hasScopes := strings .Split (res .Header .Get ("X-Oauth-Scopes" ), "," )
174+
175+ found := 0
176+ for _ , s := range hasScopes {
177+ for _ , w := range wantedScopes {
178+ if w == strings .TrimSpace (s ) {
179+ found ++
180+ }
181+ }
182+ }
183+
184+ if found == len (wantedScopes ) {
185+ return true , appID , nil
186+ }
187+
188+ return false , appID , nil
189+ }
190+
148191// GraphQL performs a GraphQL request and parses the response
149192func (c Client ) GraphQL (query string , variables map [string ]interface {}, data interface {}) error {
150193 url := "https://api.github.com/graphql"
0 commit comments