1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Curl extention to access the Github API
16 # See https://developer.github.com/v3/ for details
22 # Specific Curl that know hot to talk to the github API
26 # Headers to use on all requests
27 var header
: HeaderMap is noinit
32 # User agent (is used by github to contact devs in case of problems)
33 # Eg. "Awesome-Octocat-App"
34 var user_agent
: String
37 header
= new HeaderMap
38 header
["Authorization"] = "token {auth}"
41 # Get the requested URI, and check the HTTP response. Then convert to JSON
42 # and check for Github errors.
43 fun get_and_check
(uri
: String): nullable Jsonable
45 var request
= new CurlHTTPRequest(uri
, self)
46 request
.user_agent
= user_agent
47 request
.headers
= header
48 var response
= request
.execute
50 if response
isa CurlResponseSuccess then
51 var obj
= response
.body_str
.parse_json
52 if obj
isa JsonObject then
53 if obj
.keys
.has
("message") then
54 print
"Message from Github API: {obj["message"] or else ""}"
55 print
"Requested URI: {uri}"
61 else if response
isa CurlResponseFailed then
62 print
"Request to Github API failed"
63 print
"Requested URI: {uri}"
64 print
"Error code: {response.error_code}"
65 print
"Error msg: {response.error_msg}"
71 # Return the value of `git config --get github.oauthtoken`
72 # return "" if no such a key
73 fun get_github_oauth
: String
75 var p
= new IProcess("git", "config", "--get", "github.oauthtoken")
76 var token
= p
.read_line