Qore RestClient Module Reference  1.1
 All Classes Namespaces Functions Variables Groups Pages
RestClient.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file RestClient.qm Qore user module for calling REST services
3 
4 /* RestClient.qm Copyright 2013 Qore Technologies, sro
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 // this module requires Qore 0.8.8 or better
26 
27 // require type definitions everywhere
28 
29 // enable all warnings
30 
31 
32 
33 
34 
35 }
36 
37 /* Version History
38  * 2013-12-17 v1.1: David Nichols <david@qore.org>
39  + updated to use encode_url() before sending HTTP messages
40  + add charset=xxx to Content-Type in requests
41  + fixed Accept header in requests
42  * 2013-10-04 v1.0: David Nichols <david@qore.org>
43  + the initial version of the RestClient module
44 */
45 
83 namespace RestClient {
86 class RestClient : public Qore::HTTPClient {
87 
88 public:
89  public :
92  "json": (
93  "ct": MimeTypeJson,
94  "out": \makeJSONString(),
95  ),
96  "yaml": (
97  "ct": MimeTypeYaml,
98  "out": \makeYAML(),
99  ),
100  "xml": (
101  "ct": MimeTypeXml,
102  "out": \makeXMLRPCValueString(),
103  ),
104  );
105 
106 
107  const DeserializeYaml = (
108  "code": "yaml",
109  "in": \parseYAML(),
110  );
111 
114  MimeTypeJson: (
115  "code": "json",
116  "in": \parseJSON(),
117  ),
118  MimeTypeYamlRpc: DeserializeYaml,
119  MimeTypeYaml: DeserializeYaml,
120  MimeTypeXml: (
121  "code": "xml",
122  "in": \parseXMLRPCValue(),
123  ),
124  );
125 
127  const AcceptList = (
129  MimeTypeXml,
131  );
132 
134  const Accept = AcceptList.join(",");
135 
137  const Version = "1.0";
138 
140  const VersionString = sprintf("Qore-RestClient/%s", RestClient::Version);
141 
143  const DefaultHeaders = (
144  "Accept": Accept,
145  "User-Agent": RestClient::VersionString,
146  );
147 
149 
156  "auto": True,
157  "xml": True,
158  "json": True,
159  "yaml": True,
160  );
161 
162 public:
163 
164  private :
165  // headers to send with every request
166  hash headers;
167  // data serialization code
168  string ds;
169  // serialization info hash
170  hash sih;
171 
172 public:
173 
175 
194  constructor(*hash opts, *softbool do_not_connect);
195 
196 
198 
209  setSerialization(string data = "auto");
210 
211 
213 
222  string getSerialization();
223 
224 
226 
243  hash get(string path, any body, *reference info);
244 
245 
247 
264  hash put(string path, any body, *reference info);
265 
266 
268 
285  hash post(string path, any body, *reference info);
286 
287 
289 
306  hash del(string path, any body, *reference info);
307 
308 
310 
331  hash doRequest(string m, string path, any body, *reference info, *softbool decode_errors, *hash hdr);
332 
333 
334  decodeResponse(reference h, *reference info);
335 
336  };
337 };
this class provides the REST client API
Definition: RestClient.qm.dox.h:86
const Version
RestClient Version.
Definition: RestClient.qm.dox.h:137
string sprintf(string fmt,...)
const AcceptList
Accept header list.
Definition: RestClient.qm.dox.h:127
const DataSerializationSupport
Data serialization support mapping codes to MIME types and de/serialization functions.
Definition: RestClient.qm.dox.h:91
hash del(string path, any body, *reference info)
sends an HTTP DELETE request to the REST server and returns the response
const MimeTypeYaml
const MimeTypeXml
const True
hash put(string path, any body, *reference info)
sends an HTTP PUT request to the REST server and returns the response
const DefaultHeaders
default HTTP headers (Content-Type is added before sending)
Definition: RestClient.qm.dox.h:143
const MimeTypeJsonRpc
const MimeTypeJson
const DataDeserializationSupport
Data deserialization support MIME types to codes and de/serialization functions.
Definition: RestClient.qm.dox.h:113
const DataSerializationOptions
Data serialization options; this is a hash to similulate a set of strings.
Definition: RestClient.qm.dox.h:155
const Accept
Accept header value.
Definition: RestClient.qm.dox.h:134
const MimeTypeYamlRpc
const VersionString
RestClient Version String.
Definition: RestClient.qm.dox.h:140
string getSerialization()
returns the current data serialization format currently in effect for the object (see DataSerializati...
const VersionString
hash post(string path, any body, *reference info)
sends an HTTP POST request to the REST server and returns the response
hash hash(object obj)
hash doRequest(string m, string path, any body, *reference info, *softbool decode_errors, *hash hdr)
sends an HTTP request to the REST server and returns the response
setSerialization(string data="auto")
change the serialization option for the object; see DataSerializationOptions for valid options ...