misc/vim: inform the user when no results are found
[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 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 headers["X-Stream"] = "true"
54 if auth != null then
55 headers["Authorization"] = "token {auth.to_s}"
56 end
57 if user_agent != null then
58 headers["User-Agent"] = user_agent.to_s
59 end
60 end
61
62 redef fun execute do
63 init_headers
64 if not self.curl.is_ok then
65 return answer_failure(0, "Curl instance is not correctly initialized")
66 end
67
68 var success_response = new CurlResponseSuccess
69 var callback_receiver: CurlCallbacks = success_response
70 if self.delegate != null then callback_receiver = self.delegate.as(not null)
71
72 var err
73
74 err = self.curl.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
75 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
76
77 err = self.curl.prim_curl.easy_setopt(new CURLOption.http_version, 1)
78 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
79
80
81 err = self.curl.prim_curl.easy_setopt(new CURLOption.url, url)
82 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
83
84 err = self.curl.prim_curl.register_callback(callback_receiver, new CURLCallbackType.header)
85 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
86
87 err = self.curl.prim_curl.register_callback(callback_receiver, new CURLCallbackType.body)
88 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
89
90 # HTTP Header
91 if self.headers != null then
92 var headers_joined = self.headers.join_pairs(": ")
93 err = self.curl.prim_curl.easy_setopt(
94 new CURLOption.httpheader, headers_joined.to_curlslist)
95 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
96 end
97
98 var err_hook = execute_hook
99 if err_hook != null then return err_hook
100
101 var err_resp = perform
102 if err_resp != null then return err_resp
103
104 var st_code = self.curl.prim_curl.easy_getinfo_long(new CURLInfoLong.response_code)
105 if not st_code == null then success_response.status_code = st_code.response
106
107 return success_response
108 end
109
110 # Hook to implement in concrete requests
111 protected fun execute_hook: nullable CurlResponse do return null
112 end
113
114 # HTTP GET command
115 class JsonGET
116 super JsonCurlRequest
117
118 redef fun execute_hook do
119 var err = self.curl.prim_curl.easy_setopt(new CURLOption.get, true)
120 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
121 return null
122 end
123 end
124
125 # HTTP POST command that sends JSON data
126 class JsonPOST
127 super JsonCurlRequest
128
129 var data: nullable Jsonable = null is writable
130
131 redef fun init_headers do
132 super
133 headers["Content-Type"] = "application/json"
134 end
135
136 redef fun execute_hook do
137 var err = self.curl.prim_curl.easy_setopt(new CURLOption.post, true)
138 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
139
140 if self.data != null then
141 var postdatas = self.data.to_json
142 err = self.curl.prim_curl.easy_setopt(new CURLOption.postfields, postdatas)
143 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
144 end
145 return null
146 end
147 end
148
149 # HTTP DELETE command
150 class JsonDELETE
151 super JsonCurlRequest
152
153 redef fun execute_hook do
154 var err = self.curl.prim_curl.easy_setopt(new CURLOption.custom_request, "DELETE")
155 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
156 return null
157 end
158 end
159
160 # HTTP PUT command that sends JSON data
161 class JsonPUT
162 super JsonCurlRequest
163
164 var data: nullable Jsonable = null is writable
165
166 redef fun init_headers do
167 super
168 headers["Content-Type"] = "application/json"
169 end
170
171 redef fun execute_hook do
172 var err = self.curl.prim_curl.easy_setopt(new CURLOption.custom_request, "PUT")
173 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
174
175 if self.data != null then
176 var postdatas = self.data.to_json
177 err = self.curl.prim_curl.easy_setopt(new CURLOption.postfields, postdatas)
178 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
179 end
180 return null
181 end
182 end
183