Qore RestClient Module Reference  1.4.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 (C) 2013 - 2018 Qore Technologies, s.r.o.
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.13 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 
40 /* Version History - see below in docs
41 */
42 
138 namespace RestClient {
141 class RestClient : public Qore::HTTPClient {
142 
143 public:
144  public :
147  "json": MimeTypeJson,
148  "yaml": MimeTypeYaml,
149  "xml": MimeTypeXml,
150  "rawxml": MimeTypeXmlApp,
151  "url": MimeTypeFormUrlEncoded,
152  "text": MimeTypeText,
153  "bin": MimeTypeOctetStream,
154  };
155 
156  const DeserializeYaml = (
157  "code": "yaml",
158  "in": \parse_yaml(),
159  );
160  const DeserializeXml = (
161  "code": "xml",
162  "arg": True,
163  "in": hash (string xml, reference<string> type) {
164  try {
165  on_success type = "xml";
166  return parse_xmlrpc_value(xml);
167  }
168  catch (hash<ExceptionInfo> ex);
169 
170  },
171  );
172 
174  const AcceptList = (
179  MimeTypeText,
181  );
182 
184  const Accept = AcceptList.join(",");
185 
187  const Version = "1.4";
188 
190  const VersionString = sprintf("Qore-RestClient/%s", RestClient::Version);
191 
193  const DefaultHeaders = (
194  "Accept": Accept,
195  "User-Agent": RestClient::VersionString,
196  );
197 
199 
210  "auto": True,
211  "json": True,
212  "yaml": True,
213  "rawxml": True,
214  "xml": True,
215  "url": True,
216  "text": True,
217  "bin": True,
218  );
219 
221 
227  const EncodingSupport = (
228  "gzip": (
229  "ce": "gzip",
230  "func": \gzip(),
231  ),
232  "bzip2": (
233  "ce": "bzip2",
234  "func": \bzip2(),
235  ),
236  "deflate": (
237  "ce": "deflate",
238  "func": \compress(),
239  ),
240  "identity": (
241  "ce": NOTHING,
242  ),
243  );
244 
246  const CompressionThreshold = 1024;
247 
248 public:
249 
250  private :
251  // headers to send with every request
252  hash headers;
253  // data serialization code
254  string ds;
255  // serialization content type
256  string sct;
257  // send content encoding hash
258  *hash seh;
259  // REST schema validator
261 
262 public:
263 
265 
298  constructor(*hash opts, *softbool do_not_connect) ;
299 
300 
302 
313  setSerialization(string data = "auto");
314 
315 
317 
332  setSendEncoding(string enc = "auto");
333 
334 
336 
351  setContentEncoding(string enc = "auto");
352 
353 
355 
370 
371 
373 
387 
388 
390 
403  *string getSendEncoding();
404 
405 
407 
416  string getSerialization();
417 
418 
420 
455  hash get(string path, auto body, *reference<hash> info, *hash hdr);
456 
457 
459 
493  hash put(string path, auto body, *reference<hash> info, *hash hdr);
494 
495 
497 
531  hash patch(string path, auto body, *reference<hash> info, *hash hdr);
532 
533 
535 
569  hash post(string path, auto body, *reference<hash> info, *hash hdr);
570 
571 
573 
607  hash del(string path, auto body, *reference<hash> info, *hash hdr);
608 
609 
611 
616 
617 
619 
620 private:
621  nothing prepareMsg(string method, string path, reference body, reference<hash> hdr, string ct = "Content-Type");
622 public:
623 
624 
626 
627 private:
628  nothing preparePath(reference<string> path);
629 public:
630 
631 
633 
671  hash doRequest(string m, string path, auto body, *reference<hash> info, softbool decode_errors = True, *hash hdr);
672 
673 
675  hash sendAndDecodeResponse(*data body, string m, string path, hash hdr, *reference<hash> info, *softbool decode_errors);
676 
677 
679 
680 private:
681  static decodeError(hash h, *reference<hash> info);
682 public:
683 
684 
686  static private;
687 
688 
689  }; // class RestClient
690 
692 
711 
712 public:
713  public :
716 
718  const Options = HttpConnection::Options + (
719  "data": True,
720  "send_encoding": True,
721  "content_encoding": True,
722  "swagger": True,
723  );
724 
725  const OptionList = Options.keys();
726 
727 public:
728 
730 
737  constructor(string name, string desc, string url, bool monitor, *hash opts, hash urlh) ;
738 
739 
741 
748 private:
749  RestClient getImpl(bool connect = True, *hash rtopts);
750 public:
751 
752 
754 
767  hash getOptions();
768 
769 
772 
773 
775  string getType();
776 
777 
779  static RestConnection make(string name, string desc, string url, bool monitor, *hash opts, hash urlh);
780  };
781 };
this class provides the REST client API
Definition: RestClient.qm.dox.h:141
string getType()
returns &quot;rest&quot;
const Version
RestClient Version.
Definition: RestClient.qm.dox.h:187
string sprintf(string fmt,...)
const AcceptList
Accept header list.
Definition: RestClient.qm.dox.h:174
const DataSerializationSupport
Data serialization support mapping codes to MIME types and de/serialization functions.
Definition: RestClient.qm.dox.h:146
hash getOptions()
gets options
hash real_opts
real options used when creating an object
Definition: RestClient.qm.dox.h:715
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
const DefaultHeaders
default HTTP headers (Content-Type is added before sending)
Definition: RestClient.qm.dox.h:193
const MimeTypeJsonRpc
hash getDefaultHeaders()
returns the hash of default headers to sent in all requests
const MimeTypeJson
const Options
RestConnection object connection options.
Definition: RestClient.qm.dox.h:718
RestClient getImpl(bool connect=True, *hash rtopts)
returns a RestClient::RestClient object
const DataSerializationOptions
Data serialization options; this is a hash to simulate a set of strings.
Definition: RestClient.qm.dox.h:209
const Accept
Accept header value.
Definition: RestClient.qm.dox.h:184
const MimeTypeOctetStream
hash doRequest(string m, string path, auto body, *reference< hash > info, softbool decode_errors=True, *hash hdr)
sends an HTTP request to the REST server and returns the response
const MimeTypeFormUrlEncoded
hash post(string path, auto body, *reference< hash > info, *hash hdr)
sends an HTTP POST request to the REST server and returns the response
*hash getDefaultOptions()
returns default options
RestSchemaValidator::AbstractRestSchemaValidator getValidator()
returns the current validator object
const MimeTypeYamlRpc
static decodeError(hash h, *reference< hash > info)
decode any REST errors returned if possible
constructor(string name, string desc, string url, bool monitor, *hash opts, hash urlh)
creates the RestConnection object
const VersionString
RestClient Version String.
Definition: RestClient.qm.dox.h:190
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)
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:246
string type(auto arg)
hash patch(string path, auto body, *reference< hash > info, *hash hdr)
sends an HTTP PATCH request to the REST server and returns the response
nothing prepareMsg(string method, string path, reference body, reference< hash > hdr, string ct="Content-Type")
sets up the Content-Type header and encodes any body for sending
hash sendAndDecodeResponse(*data body, string m, string path, hash hdr, *reference< hash > info, *softbool decode_errors)
sends the outgoing HTTP message and recodes the response to data
nothing preparePath(reference< string > path)
sets up the path for the HTTP request URI
const VersionString
class for REST HTTP connections; returns RestClient::RestClient objects
Definition: RestClient.qm.dox.h:710
hash del(string path, auto body, *reference< hash > info, *hash hdr)
sends an HTTP DELETE request to the REST server and returns the response
hash hash(object obj)
static RestConnection make(string name, string desc, string url, bool monitor, *hash opts, hash urlh)
static constructor
setSerialization(string data="auto")
change the serialization option for the object; see DataSerializationOptions for valid options ...
hash put(string path, auto body, *reference< hash > info, *hash hdr)
sends an HTTP PUT request to the REST server and returns the response
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:227
binary compress(string str, int level=Z_DEFAULT_COMPRESSION)
const MimeTypeText