ci: do not error when nothing with nitunit_some
[nit.git] / lib / github / github_curl.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Curl extention to access the Github API
16 # See https://developer.github.com/v3/ for details
17 module github_curl
18
19 import curl
20 import json::static
21 import json
22
23 # Specific Curl that know hot to talk to the github API
24 class GithubCurl
25
26 # Headers to use on all requests
27 var header: HeaderMap is noinit
28
29 # OAuth token
30 #
31 # Use an empty string to disable authentication and connect
32 # anonymously (thus less capabilities and more rate limits)
33 var auth: String
34
35 # User agent (is used by github to contact devs in case of problems)
36 # Eg. "Awesome-Octocat-App"
37 var user_agent: String
38
39 init do
40 header = new HeaderMap
41 if auth != "" then header["Authorization"] = "token {auth}"
42 end
43
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
47 do
48 var request = new CurlHTTPRequest(uri)
49 request.user_agent = user_agent
50 request.headers = header
51 var response = request.execute
52
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}"
59 abort
60 end
61 end
62 return obj
63
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}"
69 abort
70 else abort
71 end
72
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
78 do
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
92 return err
93 end
94 end
95 return obj
96
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
104 return err
105 else abort
106 end
107
108 end
109
110 # An error thrown by the Github API.
111 #
112 # Depending on the kind of error, additionnal informations can be stored in
113 # the error object.
114 # Check the `json` value to find them.
115 class GithubError
116 super Error
117 super Serializable
118
119 # The name of the error.
120 var name: String
121
122 # The json content of the error.
123 var json = new JsonObject
124
125 redef init do
126 super
127 json["error"] = name.to_json
128 json["message"] = message.to_json
129 end
130
131 redef fun serialize_to(v) do json.serialize_to v
132
133 redef fun to_s do return "[{name}] {super}"
134 end
135
136 # Gets the Github token from `git` configuration
137 #
138 # Return the value of `git config --get github.oauthtoken`
139 # or `""` if no key exists.
140 fun get_github_oauth: String
141 do
142 var p = new ProcessReader("git", "config", "--get", "github.oauthtoken")
143 var token = p.read_line
144 p.wait
145 p.close
146 return token.trim
147 end