Qore RestClient Module Reference 1.9.1
Loading...
Searching...
No Matches
RestClient.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* RestClient.qm Copyright (C) 2013 - 2022 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// minimum qore version
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
198namespace RestClient {
201
202public:
204 const DataSerializationSupport = {
205%ifndef NoJson
206 "json": MimeTypeJson,
207%endif
208%ifndef NoYaml
209 "yaml": MimeTypeYaml,
210%endif
211%ifndef NoXml
212 "xml": MimeTypeXml,
213 "rawxml": MimeTypeXmlApp,
214%endif
215 "url": MimeTypeFormUrlEncoded,
216 "text": MimeTypeText,
217 "bin": MimeTypeOctetStream,
218 };
219
220 const DeserializeYaml = {
221 "code": "yaml",
222 "in": \parse_yaml(),
223 };
224 const DeserializeXml = {
225 "code": "xml",
226 "arg": True,
227 "in": hash<auto> sub (string xml, reference<string> type) {
228 try {
229 on_success type = "xml";
230 return parse_xmlrpc_value(xml);
231 } catch (hash<ExceptionInfo> ex) {
232 try {
233 on_success type = "rawxml";
234 return parse_xml(xml);
235 } catch () {
236 rethrow;
237 }
238 }
239 },
240 };
241
243 const AcceptList = ...;
244
245
247 const Accept = AcceptList.join(",");
248
250 const AcceptMap = map {$1: True}, AcceptList;
251
253 const Version = "1.7";
254
256 const VersionString = sprintf("Qore-RestClient/%s", RestClient::Version);
257
259 const DefaultHeaders = {
260 "Accept": Accept,
261 "User-Agent": RestClient::VersionString,
262 };
263
265
275 const DataSerializationOptions = {
276 "auto": True,
277%ifndef NoJson
278 "json": True,
279%endif
280%ifndef NoYaml
281 "yaml": True,
282%endif
283%ifndef NoXml
284 "rawxml": True,
285 "xml": True,
286%endif
287 "url": True,
288 "text": True,
289 "bin": True,
290 };
291
293
299 const EncodingSupport = {
300 "gzip": {
301 "ce": "gzip",
302 "func": \gzip(),
303 },
304 "bzip2": {
305 "ce": "bzip2",
306 "func": \bzip2(),
307 },
308 "deflate": {
309 "ce": "deflate",
310 "func": \compress(),
311 },
312 "identity": {
313 "ce": NOTHING,
314 },
315 };
316
318 const CompressionThreshold = 1024;
319
320protected:
321 // headers to send with every request
322 hash<auto> headers;
323 // data serialization code
324 string ds;
325 // serialization content type
326 string sct;
327 // send content encoding hash
328 *hash<auto> seh;
329 // REST schema validator
331 // no_charset option
332 *bool noCharset;
333
334public:
335
337
398 constructor(*hash<auto> opts, *softbool do_not_connect) : HTTPClient(opts + ((opts.url || !opts.validator)
399 ? NOTHING
400 ;
401
402
404
406 clearConnectionPath();
407
408
410
421 setSerialization(string data = 'auto');
422
423
425
440 setSendEncoding(string enc = 'auto');
441
442
444
459 setContentEncoding(string enc = 'auto');
460
461
463
477 addDefaultHeaders(hash<auto> h);
478
479
481
494 hash<auto> getDefaultHeaders();
495
496
498
511 *string getSendEncoding();
512
513
515
524 string getSerialization();
525
526
528
569 hash<auto> get(string path, auto body, *reference<hash<auto>> info, *hash<auto> hdr);
570
571
573
613 hash<auto> put(string path, auto body, *reference<hash<auto>> info, *hash<auto> hdr);
614
615
617
657 hash<auto> patch(string path, auto body, *reference<hash<auto>> info, *hash<auto> hdr);
658
659
661
712 hash<auto> post(string path, auto body, *reference<hash<auto>> info, *hash<auto> hdr);
713
714
716
767 hash<auto> del(string path, auto body, *reference<hash<auto>> info, *hash<auto> hdr);
768
769
771
776
777
779 private nothing prepareMsg(string method, string path, reference<auto> body, reference<hash<auto>> hdr,
780 string ct = "Content-Type") {
781 // use {} + ... here to ensure that hdr stays "hash<auto>"
782 hdr = {} + headers + hdr;
783
784 // must get the path from the HTTPClient object if not given in the request
785 if (!path.val() && (*string p = getConnectionPath()))
786 path = p;
787
788 hash<RestRequestClientInfo> req = validator.processRequest(method, path, body, hdr, sct);
789
790 if (exists body);
791
792 }
793
795protected:
796 nothing preparePath(reference<string> path);
797public:
798
799
801
861 hash<auto> doRequest(string m, string path, auto body, *reference<hash<auto>> info, softbool decode_errors = True,
862 *hash<auto> hdr) {
863 prepareMsg(m, path, \body, \hdr);
864
865 on_exit if (exists body);
866
867
868 // prepare path
869 preparePath(\path);
870
871 return sendAndDecodeResponse(body, m, path, hdr, \info, decode_errors);
872 }
873
875
877 hash<auto> doValidatedRequest(string m, string path, auto body, *reference<hash<auto>> info,
878 softbool decode_errors = True, *hash<auto> hdr) {
879 // use {} + ... here to ensure that hdr stays "hash<auto>"
880 hdr = {} + headers + hdr;
881
882 on_exit if (exists body);
883
884
885 return sendAndDecodeResponse(body, m, path, hdr, \info, decode_errors);
886 }
887
889 private hash<auto> sendAndDecodeResponse(*data body, string m, string path, hash<auto> hdr,
890 *reference<hash<auto>> info, *softbool decode_errors) {
891 hash<auto> h;
892 try {
893 h = send(body, m, path, hdr, False, \info);
894 info."response-code" = h.status_code;
895 } catch (hash<ExceptionInfo> ex) {
896 info."response-code" = info."response-headers".status_code;
897 if (ex.arg) {
898 if (decode_errors && ex.arg.body) {
899 decodeError(ex.arg, \info);
900 ex.arg.deserialized_body = info."response-body";
901 }
902 }
903
904 rethrow ex.err, ex.desc, ex.arg;
905 }
906
907 on_error {
908 if (exists h.body && !exists info."response-body") {
909 info."response-body" = h.body;
910 }
911 }
912
913 return processRestResponse(h, m, path, \info);
914 }
915
917
924protected:
925 hash<auto> processRestResponse(hash<auto> resp, string method, string path, *reference<hash<auto>> info);
926public:
927
928
930protected:
931 static decodeError(hash<auto> h, *reference<hash<auto>> info);
932public:
933
934
936private:
937 static tryDecodeErrorResponse(reference<hash<auto>> h, *reference<hash<auto>> info);
938public:
939
940
941}; // class RestClient
942
944
985
986public:
988 hash<auto> real_opts;
989
991 const ConnectionScheme = <ConnectionSchemeInfo>{
992 "cls": Class::forName("RestConnection"),
993 "options": HttpConnection::ConnectionScheme.options + {
994 "content_encoding": <ConnectionOptionInfo>{
995 "type": "string",
996 "desc": "this sets the send encoding (if the `send_encoding` option is not set) and the "
997 "response encoding to request",
998 "allowed_values": (
999 <AllowedValueInfo>{
1000 "value": "gzip",
1001 "desc": "use GNU zip encoding ([RFC 1952](https://tools.ietf.org/html/rfc1952))",
1002 }, <AllowedValueInfo>{
1003 "value": "bzip2",
1004 "desc": "use bzip2 encoding",
1005 }, <AllowedValueInfo>{
1006 "value": "deflate",
1007 "desc": "use the deflate algorithm ([RFC 1951](https://tools.ietf.org/html/rfc1951))",
1008 }, <AllowedValueInfo>{
1009 "value": "identity",
1010 "desc": "use no content encoding",
1011 },
1012 ),
1013 },
1014 "data": <ConnectionOptionInfo>{
1015 "type": "string",
1016 "desc": "data serialization options",
1017 "allowed_values": (
1018 <AllowedValueInfo>{
1019 "value": "auto",
1020 "desc": "prefers in this order: `json`, `yaml`, `rawxml`, `xml`, `url`, and `text`",
1021 }, <AllowedValueInfo>{
1022 "value": "bin",
1023 "desc": "for binary message bodies without data serialization",
1024 }, <AllowedValueInfo>{
1025 "value": "json",
1026 "desc": "use JSON serialization",
1027 }, <AllowedValueInfo>{
1028 "value": "rawxml",
1029 "desc": "use raw XML serialization",
1030 }, <AllowedValueInfo>{
1031 "value": "text",
1032 "desc": "use only plain text; no serialization is used",
1033 }, <AllowedValueInfo>{
1034 "value": "url",
1035 "desc": "for URL-encoded message bodies",
1036 }, <AllowedValueInfo>{
1037 "value": "xml",
1038 "desc": "use only XML-RPC serialization",
1039 }, <AllowedValueInfo>{
1040 "value": "yaml",
1041 "desc": "use only YAML serialization",
1042 },
1043 ),
1044 "default_value": "auto",
1045 },
1046 "headers": <ConnectionOptionInfo>{
1047 "type": "hash",
1048 "desc": "an optional hash of headers to send with every request, these can also be "
1049 "overridden in request method calls",
1050 },
1051 "send_encoding": <ConnectionOptionInfo>{
1052 "type": "string",
1053 "desc": "this sets the send encoding",
1054 "allowed_values": (
1055 <AllowedValueInfo>{
1056 "value": "gzip",
1057 "desc": "use GNU zip encoding ([RFC 1952](https://tools.ietf.org/html/rfc1952))",
1058 }, <AllowedValueInfo>{
1059 "value": "bzip2",
1060 "desc": "use bzip2 encoding",
1061 }, <AllowedValueInfo>{
1062 "value": "deflate",
1063 "desc": "use the deflate algorithm ([RFC 1951](https://tools.ietf.org/html/rfc1951))",
1064 }, <AllowedValueInfo>{
1065 "value": "identity",
1066 "desc": "use no content encoding",
1067 },
1068 ),
1069 },
1070 "swagger": <ConnectionOptionInfo>{
1071 "type": "string",
1072 "desc": "the location of a Swagger schema to use for message validation; processed with "
1073 "`FileLocationHandler::getTextFileFromLocation()` "
1074 "(ex: `file:///path/to/swagger-schema.json`); conflicts with `validator`",
1075 },
1076 "swagger_base_path": <ConnectionOptionInfo>{
1077 "type": "string",
1078 "desc": "in case a REST validator is used, the base path in the schema can be overridden "
1079 "with this option (applies to any REST validator; not just Swagger validators)",
1080 "subst_env_vars": True,
1081 },
1082 "validator": <ConnectionOptionInfo>{
1083 "type": "any",
1084 "desc": "an `AbstractRestSchemaValidator` object for REST message validation; conflicts with "
1085 "`swagger`",
1086 },
1087 },
1088 };
1089
1091 const Options = map {$1: True}, keys ConnectionScheme.options;
1092
1094 const DefaultOptions = ...;
1095
1096
1098 const OptionList = keys ConnectionScheme.options;
1099
1101
1114 constructor(string name, string description, string url, hash<auto> attributes = {}, hash<auto> options = {})
1115 ;
1116
1117
1119
1127protected:
1128 RestClient getImpl(bool connect = True, *hash<auto> rtopts);
1129public:
1130
1131
1133
1137 object getPollImpl();
1138
1139
1141
1172 hash<auto> getOptions();
1173
1174
1176 *hash<auto> getDefaultOptions();
1177
1178
1180 string getType();
1181
1182
1184
1191
1192
1194
1199
1200
1202
1204 static hash<auto> processOptions(*hash<auto> opts);
1205
1207protected:
1209public:
1210
1211
1213protected:
1214 hash<ConnectionSchemeInfo> getConnectionSchemeInfoImpl();
1215public:
1216
1217};
1218};
const VersionString
RestClient Version String.
Definition: RestClient.qm.dox.h:256
const Version
RestClient Version.
Definition: RestClient.qm.dox.h:253
class for REST HTTP connections; returns RestClient::RestClient objects
Definition: RestClient.qm.dox.h:984
bool hasDataProvider()
returns True, as this connection always returns a data provider with the getDataProvider() method
*hash< auto > getDefaultOptions()
returns default options
const OptionList
object connection option list
Definition: RestClient.qm.dox.h:1098
constructor(string name, string description, string url, hash< auto > attributes={}, hash< auto > options={})
creates the RestConnection connection object
hash< auto > real_opts
real options used when creating an object
Definition: RestClient.qm.dox.h:988
const Options
object connection options
Definition: RestClient.qm.dox.h:1091
const DefaultOptions
default options
Definition: RestClient.qm.dox.h:1094
hash< ConnectionSchemeInfo > getConnectionSchemeInfoImpl()
Returns the ConnectionSchemeInfo hash for this object.
object getPollImpl()
Returns an unconnected object for a non-blocking poll operation.
setChildCapabilities()
Sets child data provider capabilities.
RestClient getImpl(bool connect=True, *hash< auto > rtopts)
returns a RestClient object
static hash< auto > processOptions(*hash< auto > opts)
processes options for the constructor
hash< auto > getOptions()
gets options
const ConnectionScheme
Connection entry info.
Definition: RestClient.qm.dox.h:991
string getType()
returns "rest"
DataProvider::AbstractDataProvider getDataProvider()
returns a data provider object for this connection
hash< RestRequestClientInfo > processRequest(string method, string path, auto body, *hash< auto > headers, *softlist< string > content_types)
string type(auto arg)
the RestClient namespace contains all the objects in the RestClient module
Definition: RestClient.qm.dox.h:198