code: remove useless `or else` now that they are detected
[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
22 # Specific Curl that know hot to talk to the github API
23 class GithubCurl
24 super Curl
25
26 # Headers to use on all requests
27 var header: HeaderMap is noinit
28
29 # OAuth token
30 var auth: String
31
32 # User agent (is used by github to contact devs in case of problems)
33 # Eg. "Awesome-Octocat-App"
34 var user_agent: String
35
36 init do
37 header = new HeaderMap
38 header["Authorization"] = "token {auth}"
39 end
40
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
44 do
45 var request = new CurlHTTPRequest(uri, self)
46 request.user_agent = user_agent
47 request.headers = header
48 var response = request.execute
49
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}"
56 abort
57 end
58 end
59 return obj
60
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}"
66 abort
67 else abort
68 end
69
70 # Get the requested URI, and check the HTTP response.
71 # Then convert to JSON and check for Github errors.
72 # Unlike `get_and_check`, error do not trigger an abort but
73 # are reported as `GithubError`.
74 fun get_and_parse(uri: String): nullable Jsonable
75 do
76 var request = new CurlHTTPRequest(uri, self)
77 request.user_agent = user_agent
78 request.headers = header
79 var response = request.execute
80 if response isa CurlResponseSuccess then
81 var obj = response.body_str.parse_json
82 if obj isa JsonObject then
83 if obj.keys.has("message") then
84 var title = "GithubAPIError"
85 var msg = obj["message"].to_s
86 var err = new GithubError(msg, title)
87 err.json["requested_uri"] = uri
88 err.json["status_code"] = response.status_code
89 return err
90 end
91 end
92 return obj
93
94 else if response isa CurlResponseFailed then
95 var title = "CurlResponseFailed"
96 var msg = "Request to Github API failed"
97 var err = new GithubError(msg, title)
98 err.json["requested_uri"] = uri
99 err.json["error_code"] = response.error_code
100 err.json["response"] = response.error_msg
101 return err
102 else abort
103 end
104
105 end
106
107 # An error thrown by the Github API.
108 #
109 # Depending on the kind of error, additionnal informations can be stored in
110 # the error object.
111 # Check the `json` value to find them.
112 class GithubError
113 super Error
114 super Jsonable
115
116 # The name of the error.
117 var name: String
118
119 # The json content of the error.
120 var json = new JsonObject
121
122 redef init do
123 super
124 json["error"] = name.to_json
125 json["message"] = message.to_json
126 end
127
128 redef fun to_json do
129 return json.to_json
130 end
131
132 redef fun to_s do return "[{name}] {super}"
133 end
134
135 # Return the value of `git config --get github.oauthtoken`
136 # return "" if no such a key
137 fun get_github_oauth: String
138 do
139 var p = new ProcessReader("git", "config", "--get", "github.oauthtoken")
140 var token = p.read_line
141 p.wait
142 p.close
143 return token.trim
144 end