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"
54 headers
["Authorization"] = "token {auth.to_s}"
56 if user_agent
!= null then
57 headers
["User-Agent"] = user_agent
.to_s
63 if not self.curl
.is_ok
then
64 return answer_failure
(0, "Curl instance is not correctly initialized")
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)
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
)
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
)
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
)
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
)
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
)
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
)
97 var err_hook
= execute_hook
98 if err_hook
!= null then return err_hook
100 var err_resp
= perform
101 if err_resp
!= null then return err_resp
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
106 return success_response
109 # Hook to implement in concrete requests
110 protected fun execute_hook
: nullable CurlResponse do return null
115 super JsonCurlRequest
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
)
124 # HTTP POST command that sends JSON data
126 super JsonCurlRequest
128 var data
: nullable Jsonable = null is writable
130 redef fun init_headers
do
132 headers
["Content-Type"] = "application/json"
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
)
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
)
148 # HTTP DELETE command
150 super JsonCurlRequest
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
)
159 # HTTP PUT command that sends JSON data
161 super JsonCurlRequest
163 var data
: nullable Jsonable = null is writable
165 redef fun init_headers
do
167 headers
["Content-Type"] = "application/json"
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
)
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
)