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