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