Qore RestClient Module Reference  1.3
 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 (C) 2013 - 2016 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.12 or better
26 
27 // require type definitions everywhere
28 
29 // enable all warnings
30 
31 // don't use "$" for vars, members, and methods, assume local variable scope
32 
33 
34 
35 
36 
37 }
38 
39 /* Version History - see below in docs
40 */
41 
110 namespace RestClient {
113 class RestClient : public Qore::HTTPClient {
114 
115 public:
116  public :
119  "json": (
120  "ct": MimeTypeJson,
121  "out": \make_json(),
122  ),
123  "yaml": (
124  "ct": MimeTypeYaml,
125  "out": \make_yaml(),
126  ),
127  "xml": (
128  "ct": MimeTypeXml,
129  "out": \make_xmlrpc_value(),
130  ),
131  };
132 
133 
134  const DeserializeYaml = (
135  "code": "yaml",
136  "in": \parse_yaml(),
137  );
138  const DeserializeXmlRpc = (
139  "code": "xml",
140  "in": \parse_xmlrpc_value(),
141  );
142 
145  MimeTypeJson: (
146  "code": "json",
147  "in": \parse_json(),
148  ),
149  MimeTypeYamlRpc: DeserializeYaml,
150  MimeTypeYaml: DeserializeYaml,
151  MimeTypeXml: DeserializeXmlRpc,
152  MimeTypeXmlApp: DeserializeXmlRpc,
153  };
154 
156  const AcceptList = (
160  );
161 
163  const Accept = AcceptList.join(",");
164 
166  const Version = "1.3";
167 
169  const VersionString = sprintf("Qore-RestClient/%s", RestClient::Version);
170 
172  const DefaultHeaders = (
173  "Accept": Accept,
174  "User-Agent": RestClient::VersionString,
175  );
176 
178 
185  "auto": True,
186  "xml": True,
187  "json": True,
188  "yaml": True,
189  );
190 
192 
198  const EncodingSupport = (
199  "gzip": (
200  "ce": "gzip",
201  "func": \gzip(),
202  ),
203  "bzip2": (
204  "ce": "bzip2",
205  "func": \bzip2(),
206  ),
207  "deflate": (
208  "ce": "deflate",
209  "func": \compress(),
210  ),
211  "identity": (
212  "ce": NOTHING,
213  ),
214  );
215 
217  const CompressionThreshold = 1024;
218 
219 public:
220 
221  private :
222  // headers to send with every request
223  hash headers;
224  // data serialization code
225  string ds;
226  // serialization info hash
227  hash sih;
228  // send content encoding hash
229  *hash seh;
230 
231 public:
232 
234 
258  constructor(*hash opts, *softbool do_not_connect) ;
259 
260 
262 
273  setSerialization(string data = "auto");
274 
275 
277 
292  setSendEncoding(string enc = "auto");
293 
294 
296 
311  setContentEncoding(string enc = "auto");
312 
313 
315 
330 
331 
333 
347 
348 
350 
363  *string getSendEncoding();
364 
365 
367 
376  string getSerialization();
377 
378 
380 
404  hash get(string path, any body, *reference info, *hash hdr);
405 
406 
408 
432  hash put(string path, any body, *reference info, *hash hdr);
433 
434 
436 
460  hash post(string path, any body, *reference info, *hash hdr);
461 
462 
464 
488  hash del(string path, any body, *reference info, *hash hdr);
489 
490 
492  private nothing prepareMsg(reference body, reference hdr, string ct = "Content-Type");
493 
494 
496  private nothing preparePath(reference path);
497 
498 
500 
527  hash doRequest(string m, string path, any body, *reference info, softbool decode_errors = True, *hash hdr);
528 
529 
531  hash sendAndDecodeResponse(*data body, string m, string path, hash hdr, *reference info, *softbool decode_errors);
532 
533 
535  private decodeError(hash h, *reference info);
536 
537 
539  decodeResponse(reference h, *reference info);
540 
541  };
542 };
hash doRequest(string m, string path, any body, *reference info, softbool decode_errors=True, *hash hdr)
sends an HTTP request to the REST server and returns the response
this class provides the REST client API
Definition: RestClient.qm.dox.h:113
const Version
RestClient Version.
Definition: RestClient.qm.dox.h:166
string sprintf(string fmt,...)
const AcceptList
Accept header list.
Definition: RestClient.qm.dox.h:156
const DataSerializationSupport
Data serialization support mapping codes to MIME types and de/serialization functions.
Definition: RestClient.qm.dox.h:118
hash del(string path, any body, *reference info, *hash hdr)
sends an HTTP DELETE request to the REST server and returns the response
addDefaultHeaders(hash h)
adds default headers to each request; these headers will be sent in all requests but can be overridde...
setSendEncoding(string enc="auto")
change the data content encoding (compression) option for the object; see EncodingSupport for valid o...
const MimeTypeYaml
const MimeTypeXml
const True
hash sendAndDecodeResponse(*data body, string m, string path, hash hdr, *reference info, *softbool decode_errors)
sends the outgoing HTTP message and recodes the response to data
const DefaultHeaders
default HTTP headers (Content-Type is added before sending)
Definition: RestClient.qm.dox.h:172
const MimeTypeJsonRpc
hash getDefaultHeaders()
returns the hash of default headers to sent in all requests
const MimeTypeJson
hash put(string path, any body, *reference info, *hash hdr)
sends an HTTP PUT request to the REST server and returns the response
const DataDeserializationSupport
Data deserialization support MIME types to codes and de/serialization functions.
Definition: RestClient.qm.dox.h:144
private nothing prepareMsg(reference body, reference hdr, string ct="Content-Type")
sets up the Content-Type header and encodes any body for sending
const DataSerializationOptions
Data serialization options; this is a hash to simulate a set of strings.
Definition: RestClient.qm.dox.h:184
const Accept
Accept header value.
Definition: RestClient.qm.dox.h:163
const MimeTypeYamlRpc
const VersionString
RestClient Version String.
Definition: RestClient.qm.dox.h:169
string getSerialization()
returns the current data serialization format currently in effect for the object (see DataSerializati...
binary bzip2(binary bin, softint level=BZ2_DEFAULT_COMPRESSION)
decodeResponse(reference h, *reference info)
decodes the response
const NOTHING
*string getSendEncoding()
returns the current data content encoding (compression) object or NOTHING if no encoding option is se...
binary gzip(string str, int level=Z_DEFAULT_COMPRESSION)
const CompressionThreshold
default threadhold for data compressions; transfers smaller than this size will not be compressed ...
Definition: RestClient.qm.dox.h:217
private decodeError(hash h, *reference info)
decode any REST errors returned if possible
hash post(string path, any body, *reference info, *hash hdr)
sends an HTTP POST request to the REST server and returns the response
const VersionString
private nothing preparePath(reference path)
sets up the path for the HTTP request URI
hash hash(object obj)
setSerialization(string data="auto")
change the serialization option for the object; see DataSerializationOptions for valid options ...
const MimeTypeXmlApp
setContentEncoding(string enc="auto")
sets the request and desired response encoding for the object; see EncodingSupport for valid options ...
const EncodingSupport
Send content encoding options.
Definition: RestClient.qm.dox.h:198
binary compress(string str, int level=Z_DEFAULT_COMPRESSION)