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
23 # Specific Curl that know hot to talk to the github API
26 # Headers to use on all requests
27 var header
: HeaderMap is noinit
31 # Use an empty string to disable authentication and connect
32 # anonymously (thus less capabilities and more rate limits)
35 # User agent (is used by github to contact devs in case of problems)
36 # Eg. "Awesome-Octocat-App"
37 var user_agent
: String
40 header
= new HeaderMap
41 if auth
!= "" then header
["Authorization"] = "token {auth}"
44 # Get the requested URI, and check the HTTP response. Then convert to JSON
45 # and check for Github errors.
46 fun get_and_check
(uri
: String): nullable Serializable
48 var request
= new CurlHTTPRequest(uri
)
49 request
.user_agent
= user_agent
50 request
.headers
= header
51 var response
= request
.execute
53 if response
isa CurlResponseSuccess then
54 var obj
= response
.body_str
.parse_json
55 if obj
isa JsonObject then
56 if obj
.keys
.has
("message") and obj
.keys
.has
("documentation_url") then
57 print
"Message from Github API: {obj["message"] or else ""}"
58 print
"Requested URI: {uri}"
64 else if response
isa CurlResponseFailed then
65 print
"Request to Github API failed"
66 print
"Requested URI: {uri}"
67 print
"Error code: {response.error_code}"
68 print
"Error msg: {response.error_msg}"
73 # Get the requested URI, and check the HTTP response.
74 # Then convert to JSON and check for Github errors.
75 # Unlike `get_and_check`, error do not trigger an abort but
76 # are reported as `GithubError`.
77 fun get_and_parse
(uri
: String): nullable Serializable
79 var request
= new CurlHTTPRequest(uri
)
80 request
.user_agent
= user_agent
81 request
.headers
= header
82 var response
= request
.execute
83 if response
isa CurlResponseSuccess then
84 var obj
= response
.body_str
.parse_json
85 if obj
isa JsonObject then
86 if obj
.keys
.has
("message") and obj
.keys
.has
("documentation_url") then
87 var title
= "GithubAPIError"
88 var msg
= obj
["message"].as(not null).to_s
89 var err
= new GithubError(msg
, title
)
90 err
.json
["requested_uri"] = uri
91 err
.json
["status_code"] = response
.status_code
97 else if response
isa CurlResponseFailed then
98 var title
= "CurlResponseFailed"
99 var msg
= "Request to Github API failed"
100 var err
= new GithubError(msg
, title
)
101 err
.json
["requested_uri"] = uri
102 err
.json
["error_code"] = response
.error_code
103 err
.json
["response"] = response
.error_msg
110 # An error thrown by the Github API.
112 # Depending on the kind of error, additionnal informations can be stored in
114 # Check the `json` value to find them.
119 # The name of the error.
122 # The json content of the error.
123 var json
= new JsonObject
127 json
["error"] = name
.to_json
128 json
["message"] = message
.to_json
131 redef fun serialize_to
(v
) do json
.serialize_to v
133 redef fun to_s
do return "[{name}] {super}"
136 # Gets the Github token from `git` configuration
138 # Return the value of `git config --get github.oauthtoken`
139 # or `""` if no key exists.
140 fun get_github_oauth
: String
142 var p
= new ProcessReader("git", "config", "--get", "github.oauthtoken")
143 var token
= p
.read_line