lib & contrib: update imports
[nit.git] / lib / neo4j / curl_json.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 requests compatible with the JSON REST APIs.
16 module curl_json
17
18 import json::static
19 import json
20 intrude import curl
21
22 # An abstract request that defines most of the standard options for Neo4j REST API
23 abstract class JsonCurlRequest
24 super CurlHTTPRequest
25
26 # OAuth token
27 var auth: nullable String is writable
28
29 # init HTTP headers for Neo4j REST API
30 protected fun init_headers do
31 headers = new HeaderMap
32 headers["Accept"] = "application/json; charset=UTF-8"
33 headers["Transfer-Encoding"] = "chunked"
34 headers["X-Stream"] = "true"
35 if auth != null then
36 headers["Authorization"] = "token {auth.to_s}"
37 end
38
39 # User agent (is used by github to contact devs in case of problems)
40 if user_agent != null then
41 headers["User-Agent"] = user_agent.to_s
42 end
43 end
44
45 redef fun execute do
46 init_headers
47
48 if not self.curl.is_ok then
49 return answer_failure(0, "Curl instance is not correctly initialized")
50 end
51
52 var success_response = new CurlResponseSuccess
53 var callback_receiver: CurlCallbacks = success_response
54 if self.delegate != null then callback_receiver = self.delegate.as(not null)
55
56 var err = self.curl.native.easy_setopt(new CURLOption.follow_location, 1)
57 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
58
59 err = self.curl.native.easy_setopt(new CURLOption.http_version, 1)
60 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
61
62 err = self.curl.native.easy_setopt(new CURLOption.url, url)
63 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
64
65 err = self.curl.native.register_callback_header(callback_receiver)
66 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
67
68 err = self.curl.native.register_callback_body(callback_receiver)
69 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
70
71 # HTTP Header
72 if self.headers != null then
73 var headers_joined = self.headers.join_pairs(": ")
74 err = self.curl.native.easy_setopt(
75 new CURLOption.httpheader, headers_joined.to_curlslist)
76 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
77 end
78
79 var err_hook = execute_hook
80 if err_hook != null then return err_hook
81
82 var err_resp = perform
83 if err_resp != null then return err_resp
84
85 var st_code = self.curl.native.easy_getinfo_long(new CURLInfoLong.response_code)
86 if not st_code == null then success_response.status_code = st_code
87
88 return success_response
89 end
90
91 # Hook to implement in concrete requests
92 protected fun execute_hook: nullable CurlResponse do return null
93 end
94
95 # HTTP GET command
96 class JsonGET
97 super JsonCurlRequest
98
99 redef fun execute_hook do
100 var err = self.curl.native.easy_setopt(new CURLOption.get, true)
101 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
102 return null
103 end
104 end
105
106 # HTTP POST command that sends JSON data
107 class JsonPOST
108 super JsonCurlRequest
109
110 var json_data: nullable Jsonable = null is writable
111
112 redef fun init_headers do
113 super
114 headers["Content-Type"] = "application/json"
115 end
116
117 redef fun execute_hook do
118 var err = self.curl.native.easy_setopt(new CURLOption.post, true)
119 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
120
121 if self.json_data != null then
122 var postdatas = self.json_data.to_json
123 err = self.curl.native.easy_setopt(new CURLOption.postfields, postdatas)
124 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
125 end
126 return null
127 end
128 end
129
130 # HTTP DELETE command
131 class JsonDELETE
132 super JsonCurlRequest
133
134 redef fun execute_hook do
135 var err = self.curl.native.easy_setopt(new CURLOption.custom_request, "DELETE")
136 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
137 return null
138 end
139 end
140
141 # HTTP PUT command that sends JSON data
142 class JsonPUT
143 super JsonCurlRequest
144
145 var json_data: nullable Jsonable = null is writable
146
147 redef fun init_headers do
148 super
149 headers["Content-Type"] = "application/json"
150 end
151
152 redef fun execute_hook do
153 var err = self.curl.native.easy_setopt(new CURLOption.custom_request, "PUT")
154 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
155
156 if self.json_data != null then
157 var postdatas = self.json_data.to_json
158 err = self.curl.native.easy_setopt(new CURLOption.postfields, postdatas)
159 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
160 end
161 return null
162 end
163 end
164