Qore RestClient Module Reference  1.2
 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 - 2014 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.10 or better
26 
27 // require type definitions everywhere
28 
29 // enable all warnings
30 
31 
32 
33 
34 
35 }
36 
37 /* Version History - see below in docs
38 */
39 
94 namespace RestClient {
97 class RestClient : public Qore::HTTPClient {
98 
99 public:
100  public :
103  "json": (
104  "ct": MimeTypeJson,
105  "out": \makeJSONString(),
106  ),
107  "yaml": (
108  "ct": MimeTypeYaml,
109  "out": \makeYAML(),
110  ),
111  "xml": (
112  "ct": MimeTypeXml,
113  "out": \makeXMLRPCValueString(),
114  ),
115  );
116 
117 
118  const DeserializeYaml = (
119  "code": "yaml",
120  "in": \parseYAML(),
121  );
122  const DeserializeXmlRpc = (
123  "code": "xml",
124  "in": \parseXMLRPCValue(),
125  );
126 
129  MimeTypeJson: (
130  "code": "json",
131  "in": \parseJSON(),
132  ),
133  MimeTypeYamlRpc: DeserializeYaml,
134  MimeTypeYaml: DeserializeYaml,
135  MimeTypeXml: DeserializeXmlRpc,
136  MimeTypeXmlApp: DeserializeXmlRpc,
137  );
138 
140  const AcceptList = (
144  );
145 
147  const Accept = AcceptList.join(",");
148 
150  const Version = "1.2";
151 
153  const VersionString = sprintf("Qore-RestClient/%s", RestClient::Version);
154 
156  const DefaultHeaders = (
157  "Accept": Accept,
158  "User-Agent": RestClient::VersionString,
159  );
160 
162 
169  "auto": True,
170  "xml": True,
171  "json": True,
172  "yaml": True,
173  );
174 
176 
181  const EncodingSupport = (
182  "deflate": (
183  "ce": "deflate",
184  "func": \compress(),
185  ),
186  "bzip2": (
187  "ce": "bzip2",
188  "func": \bzip2(),
189  ),
190  "gzip": (
191  "ce": "gzip",
192  "func": \gzip(),
193  ),
194  );
195 
196 public:
197 
198  private :
199  // headers to send with every request
200  hash headers;
201  // data serialization code
202  string ds;
203  // serialization info hash
204  hash sih;
205  // send content encoding hash
206  *hash seh;
207 
208 public:
209 
211 
233  constructor(*hash opts, *softbool do_not_connect);
234 
235 
237 
248  setSerialization(string data = "auto");
249 
250 
252 
263  setSendEncoding(string enc = "auto");
264 
265 
267 
276  string getSerialization();
277 
278 
280 
298  hash get(string path, any body, *reference info, *hash hdr);
299 
300 
302 
320  hash put(string path, any body, *reference info, *hash hdr);
321 
322 
324 
342  hash post(string path, any body, *reference info, *hash hdr);
343 
344 
346 
364  hash del(string path, any body, *reference info, *hash hdr);
365 
366 
368  private nothing prepareMsg(reference body, reference hdr, string ct = "Content-Type");
369 
370 
372  private nothing preparePath(reference path);
373 
374 
376 
397  hash doRequest(string m, string path, any body, *reference info, *softbool decode_errors, *hash hdr);
398 
399 
401  decodeResponse(reference h, *reference info);
402 
403  };
404 };
this class provides the REST client API
Definition: RestClient.qm.dox.h:97
const Version
RestClient Version.
Definition: RestClient.qm.dox.h:150
string sprintf(string fmt,...)
const AcceptList
Accept header list.
Definition: RestClient.qm.dox.h:140
const DataSerializationSupport
Data serialization support mapping codes to MIME types and de/serialization functions.
Definition: RestClient.qm.dox.h:102
hash del(string path, any body, *reference info, *hash hdr)
sends an HTTP DELETE request to the REST server and returns the response
setSendEncoding(string enc="auto")
change the serialization option for the object; see DataSerializationOptions for valid options ...
const MimeTypeYaml
const MimeTypeXml
const True
const DefaultHeaders
default HTTP headers (Content-Type is added before sending)
Definition: RestClient.qm.dox.h:156
const MimeTypeJsonRpc
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:128
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:168
const Accept
Accept header value.
Definition: RestClient.qm.dox.h:147
const MimeTypeYamlRpc
const VersionString
RestClient Version String.
Definition: RestClient.qm.dox.h:153
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
binary gzip(string str, int level=Z_DEFAULT_COMPRESSION)
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)
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 ...
const MimeTypeXmlApp
const EncodingSupport
Send content encoding options.
Definition: RestClient.qm.dox.h:181
binary compress(string str, int level=Z_DEFAULT_COMPRESSION)