1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # cURL requests compatible with the JSON REST APIs.
21 # An abstract request that defines most of the standard options for Neo4j REST API
22 abstract class JsonCurlRequest
25 super CurlCallbacksRegisterIntern
27 # REST API service URL
30 init (url
: String, curl
: nullable Curl) do
38 var auth
: nullable String is writable
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
44 # HTTP headers to send
45 var headers
: nullable HeaderMap = null is writable
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"
55 headers
["Authorization"] = "token {auth.to_s}"
57 if user_agent
!= null then
58 headers
["User-Agent"] = user_agent
.to_s
64 if not self.curl
.is_ok
then
65 return answer_failure
(0, "Curl instance is not correctly initialized")
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)
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
)
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
)
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
)
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
)
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
)
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
)
98 var err_hook
= execute_hook
99 if err_hook
!= null then return err_hook
101 var err_resp
= perform
102 if err_resp
!= null then return err_resp
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
107 return success_response
110 # Hook to implement in concrete requests
111 protected fun execute_hook
: nullable CurlResponse do return null
116 super JsonCurlRequest
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
)
125 # HTTP POST command that sends JSON data
127 super JsonCurlRequest
129 var data
: nullable Jsonable = null is writable
131 redef fun init_headers
do
133 headers
["Content-Type"] = "application/json"
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
)
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
)
149 # HTTP DELETE command
151 super JsonCurlRequest
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
)
160 # HTTP PUT command that sends JSON data
162 super JsonCurlRequest
164 var data
: nullable Jsonable = null is writable
166 redef fun init_headers
do
168 headers
["Content-Type"] = "application/json"
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
)
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
)