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