-redef class Curl
- # Headers to use on all requests
- var header: HeaderMap
-
- # OAuth token
- fun auth: String do return "OAUTH TOKEN (replace with your own)"
-
- # User agent (is used by github to contact devs in case of problems)
- var user_agent = "JNI project finder (nitlanguage.org)"
-
- redef init
- do
- super
-
- header = new HeaderMap
- header["Authorization"] = "token {auth}"
- end
-
- # Get the requested URI, and check the HTTP response. Then convert to JSON
- # and check for Github errors.
- fun get_and_check(uri: String): HashMap[String, nullable Object]
- do
- var request = new CurlHTTPRequest(uri, self)
- request.user_agent = user_agent
- request.headers = header
- var response = request.execute
-
- if response isa CurlResponseSuccess then
- var obj = response.body_str.json_to_nit_object
- assert obj isa HashMap[String, nullable Object]
-
- if obj.keys.has("message") then
- print "Message from Github API: {obj["message"]}"
- print "Requested URI: {uri}"
- abort
- end
-
- return obj
- else if response isa CurlResponseFailed then
- print "Request to Github API failed"
- print "Requested URI: {uri}"
- print "Error code: {response.error_code}"
- print "Error msg: {response.error_msg}"
- abort
- else abort
- end
-end
-