All Classes Namespaces Functions Variables Groups Pages
RestClient.qm.dox.h
// -*- mode: c++; indent-tabs-mode: nil -*-
// @file RestClient.qm Qore user module for calling REST services
/* RestClient.qm Copyright (C) 2013 - 2018 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// this module requires Qore 0.8.13 or better
// require type definitions everywhere
// enable all warnings
// don't use "$" for vars, members, and methods, assume local variable scope
}
/* Version History - see below in docs
*/
namespace RestClient {
class RestClient : public Qore::HTTPClient {
public:
public :
const DataSerializationSupport = {
"json": MimeTypeJson,
"yaml": MimeTypeYaml,
"xml": MimeTypeXml,
"rawxml": MimeTypeXmlApp,
"url": MimeTypeFormUrlEncoded,
"text": MimeTypeText,
"bin": MimeTypeOctetStream,
};
const DeserializeYaml = (
"code": "yaml",
"in": \parse_yaml(),
);
const DeserializeXml = (
"code": "xml",
"arg": True,
"in": hash (string xml, reference<string> type) {
try {
on_success type = "xml";
return parse_xmlrpc_value(xml);
}
catch (hash<ExceptionInfo> ex);
},
);
const AcceptList = (
MimeTypeYaml, MimeTypeYamlRpc,
MimeTypeXml, MimeTypeXmlApp,
MimeTypeJsonRpc,
MimeTypeFormUrlEncoded,
MimeTypeText,
MimeTypeOctetStream,
);
const Accept = AcceptList.join(",");
const Version = "1.4";
const VersionString = sprintf("Qore-RestClient/%s", RestClient::Version);
const DefaultHeaders = (
"Accept": Accept,
"User-Agent": RestClient::VersionString,
);
const DataSerializationOptions = (
"auto": True,
"json": True,
"yaml": True,
"rawxml": True,
"xml": True,
"url": True,
"text": True,
"bin": True,
);
const EncodingSupport = (
"gzip": (
"ce": "gzip",
"func": \gzip(),
),
"bzip2": (
"ce": "bzip2",
"func": \bzip2(),
),
"deflate": (
"ce": "deflate",
"func": \compress(),
),
"identity": (
"ce": NOTHING,
),
);
const CompressionThreshold = 1024;
public:
private :
// headers to send with every request
hash headers;
// data serialization code
string ds;
// serialization content type
string sct;
// send content encoding hash
*hash seh;
// REST schema validator
RestSchemaValidator::AbstractRestSchemaValidator validator;
public:
constructor(*hash opts, *softbool do_not_connect) ;
setSerialization(string data = "auto");
setSendEncoding(string enc = "auto");
setContentEncoding(string enc = "auto");
addDefaultHeaders(hash h);
hash getDefaultHeaders();
*string getSendEncoding();
string getSerialization();
hash get(string path, auto body, *reference<hash> info, *hash hdr);
hash put(string path, auto body, *reference<hash> info, *hash hdr);
hash patch(string path, auto body, *reference<hash> info, *hash hdr);
hash post(string path, auto body, *reference<hash> info, *hash hdr);
hash del(string path, auto body, *reference<hash> info, *hash hdr);
RestSchemaValidator::AbstractRestSchemaValidator getValidator();
private:
nothing prepareMsg(string method, string path, reference body, reference<hash> hdr, string ct = "Content-Type");
public:
private:
nothing preparePath(reference<string> path);
public:
hash doRequest(string m, string path, auto body, *reference<hash> info, softbool decode_errors = True, *hash hdr);
hash sendAndDecodeResponse(*data body, string m, string path, hash hdr, *reference<hash> info, *softbool decode_errors);
private:
static decodeError(hash h, *reference<hash> info);
public:
static private;
}; // class RestClient
class RestConnection : public ConnectionProvider::HttpBasedConnection {
public:
public :
hash real_opts;
const Options = HttpConnection::Options + (
"data": True,
"send_encoding": True,
"content_encoding": True,
"swagger": True,
);
const OptionList = Options.keys();
public:
constructor(string name, string desc, string url, bool monitor, *hash opts, hash urlh) ;
private:
RestClient getImpl(bool connect = True, *hash rtopts);
public:
hash getOptions();
*hash getDefaultOptions();
string getType();
static RestConnection make(string name, string desc, string url, bool monitor, *hash opts, hash urlh);
};
};
RestClient::RestClient
this class provides the REST client API
Definition: RestClient.qm.dox.h:141
ConnectionProvider::AbstractConnection::monitor
bool monitor
RestClient::RestConnection::getType
string getType()
returns &quot;rest&quot;
ConnectionProvider::HttpBasedConnection
RestClient::RestClient::Version
const Version
RestClient Version.
Definition: RestClient.qm.dox.h:187
sprintf
string sprintf(string fmt,...)
RestClient::RestClient::AcceptList
const AcceptList
Accept header list.
Definition: RestClient.qm.dox.h:174
RestClient::RestClient::DataSerializationSupport
const DataSerializationSupport
Data serialization support mapping codes to MIME types and de/serialization functions.
Definition: RestClient.qm.dox.h:146
RestClient::RestConnection::getOptions
hash getOptions()
gets options
RestClient::RestConnection::real_opts
hash real_opts
real options used when creating an object
Definition: RestClient.qm.dox.h:715
RestClient::RestClient::addDefaultHeaders
addDefaultHeaders(hash h)
adds default headers to each request; these headers will be sent in all requests but can be overridde...
RestClient::RestClient::setSendEncoding
setSendEncoding(string enc="auto")
change the data content encoding (compression) option for the object; see EncodingSupport for valid o...
MimeTypeYaml
const MimeTypeYaml
MimeTypeXml
const MimeTypeXml
RestSchemaValidator::AbstractRestSchemaValidator
ConnectionProvider::AbstractConnection::opts
*hash opts
True
const True
ConnectionProvider::AbstractConnection::name
string name
RestClient::RestClient::DefaultHeaders
const DefaultHeaders
default HTTP headers (Content-Type is added before sending)
Definition: RestClient.qm.dox.h:193
MimeTypeJsonRpc
const MimeTypeJsonRpc
RestClient::RestClient::getDefaultHeaders
hash getDefaultHeaders()
returns the hash of default headers to sent in all requests
Qore::HTTPClient::constructor
constructor()
MimeTypeJson
const MimeTypeJson
ConnectionProvider::AbstractConnection::urlh
hash urlh
RestClient::RestConnection::Options
const Options
RestConnection object connection options.
Definition: RestClient.qm.dox.h:718
RestClient::RestConnection::getImpl
RestClient getImpl(bool connect=True, *hash rtopts)
returns a RestClient::RestClient object
RestClient::RestClient::DataSerializationOptions
const DataSerializationOptions
Data serialization options; this is a hash to simulate a set of strings.
Definition: RestClient.qm.dox.h:209
RestClient::RestClient::Accept
const Accept
Accept header value.
Definition: RestClient.qm.dox.h:184
MimeTypeOctetStream
const MimeTypeOctetStream
RestClient::RestClient::doRequest
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
MimeTypeFormUrlEncoded
const MimeTypeFormUrlEncoded
RestClient::RestClient::post
hash post(string path, auto body, *reference< hash > info, *hash hdr)
sends an HTTP POST request to the REST server and returns the response
RestClient::RestConnection::getDefaultOptions
*hash getDefaultOptions()
returns default options
RestClient::RestClient::getValidator
RestSchemaValidator::AbstractRestSchemaValidator getValidator()
returns the current validator object
MimeTypeYamlRpc
const MimeTypeYamlRpc
RestClient::RestClient::decodeError
static decodeError(hash h, *reference< hash > info)
decode any REST errors returned if possible
Qore::HTTPClient
RestClient::RestConnection::constructor
constructor(string name, string desc, string url, bool monitor, *hash opts, hash urlh)
creates the RestConnection object
RestClient::RestClient::VersionString
const VersionString
RestClient Version String.
Definition: RestClient.qm.dox.h:190
RestClient::RestClient::getSerialization
string getSerialization()
returns the current data serialization format currently in effect for the object (see DataSerializati...
bzip2
binary bzip2(binary bin, softint level=BZ2_DEFAULT_COMPRESSION)
NOTHING
const NOTHING
RestClient::RestClient::getSendEncoding
*string getSendEncoding()
returns the current data content encoding (compression) object or NOTHING if no encoding option is se...
gzip
binary gzip(string str, int level=Z_DEFAULT_COMPRESSION)
RestClient::RestClient::CompressionThreshold
const CompressionThreshold
default threadhold for data compressions; transfers smaller than this size will not be compressed ...
Definition: RestClient.qm.dox.h:246
type
string type(auto arg)
ConnectionProvider::AbstractConnection::desc
string desc
RestClient::RestClient::patch
hash patch(string path, auto body, *reference< hash > info, *hash hdr)
sends an HTTP PATCH request to the REST server and returns the response
RestClient::RestClient::prepareMsg
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
RestClient::RestClient::sendAndDecodeResponse
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
RestClient::RestClient::preparePath
nothing preparePath(reference< string > path)
sets up the path for the HTTP request URI
VersionString
const VersionString
RestClient::RestConnection
class for REST HTTP connections; returns RestClient::RestClient objects
Definition: RestClient.qm.dox.h:710
RestClient::RestClient::del
hash del(string path, auto body, *reference< hash > info, *hash hdr)
sends an HTTP DELETE request to the REST server and returns the response
ConnectionProvider::AbstractConnection::url
string url
hash
hash hash(object obj)
RestClient::RestConnection::make
static RestConnection make(string name, string desc, string url, bool monitor, *hash opts, hash urlh)
static constructor
RestClient::RestClient::setSerialization
setSerialization(string data="auto")
change the serialization option for the object; see DataSerializationOptions for valid options ...
RestClient::RestClient::put
hash put(string path, auto body, *reference< hash > info, *hash hdr)
sends an HTTP PUT request to the REST server and returns the response
MimeTypeXmlApp
const MimeTypeXmlApp
RestClient::RestClient::setContentEncoding
setContentEncoding(string enc="auto")
sets the request and desired response encoding for the object; see EncodingSupport for valid options ...
RestClient::RestClient::EncodingSupport
const EncodingSupport
Send content encoding options.
Definition: RestClient.qm.dox.h:227
compress
binary compress(string str, int level=Z_DEFAULT_COMPRESSION)
MimeTypeText
const MimeTypeText