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
32 # User agent (is used by github to contact devs in case of problems)
33 # Eg. "Awesome-Octocat-App"
34 var user_agent
: String
36 init(auth
: String, user_agent
: String)
40 self.user_agent
= user_agent
42 header
= new HeaderMap
43 header
["Authorization"] = "token {auth}"
46 # Get the requested URI, and check the HTTP response. Then convert to JSON
47 # and check for Github errors.
48 fun get_and_check
(uri
: String): nullable Object
50 var request
= new CurlHTTPRequest(uri
, self)
51 request
.user_agent
= user_agent
52 request
.headers
= header
53 var response
= request
.execute
55 if response
isa CurlResponseSuccess then
56 var obj
= response
.body_str
.json_to_nit_object
57 if obj
isa HashMap[String, nullable Object] then
58 if obj
.keys
.has
("message") then
59 print
"Message from Github API: {obj["message"] or else ""}"
60 print
"Requested URI: {uri}"
66 else if response
isa CurlResponseFailed then
67 print
"Request to Github API failed"
68 print
"Requested URI: {uri}"
69 print
"Error code: {response.error_code}"
70 print
"Error msg: {response.error_msg}"
76 # Return the value of `git config --get github.oauthtoken`
77 # return "" if no such a key
78 fun get_github_oauth
: String
80 var p
= new IProcess("git", "config", "--get", "github.oauthtoken")
81 var token
= p
.read_line