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