Qore RestClient Module Reference  1.4.1
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 :
146  const DataSerializationSupport = {
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 
209  const DataSerializationOptions = (
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 
369  addDefaultHeaders(hash h);
370 
371 
373 
386  hash getDefaultHeaders();
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 };
string getType()
returns "rest"
string sprintf(string fmt,...)
hash getOptions()
gets options
hash real_opts
real options used when creating an object
Definition: RestClient.qm.dox.h:715
const MimeTypeYaml
const MimeTypeXml
const True
const MimeTypeJsonRpc
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 MimeTypeOctetStream
const MimeTypeFormUrlEncoded
*hash getDefaultOptions()
returns default options
const MimeTypeYamlRpc
constructor(string name, string desc, string url, bool monitor, *hash opts, hash urlh)
creates the RestConnection object
binary bzip2(binary bin, softint level=BZ2_DEFAULT_COMPRESSION)
const NOTHING
binary gzip(string str, int level=Z_DEFAULT_COMPRESSION)
string type(auto arg)
const VersionString
class for REST HTTP connections; returns RestClient::RestClient objects
Definition: RestClient.qm.dox.h:710
hash hash(object obj)
static RestConnection make(string name, string desc, string url, bool monitor, *hash opts, hash urlh)
static constructor
const MimeTypeXmlApp
binary compress(string str, int level=Z_DEFAULT_COMPRESSION)
const MimeTypeText
the RestClient namespace contains all the objects in the RestClient module
Definition: RestClient.qm.dox.h:139