HttpRequest.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 #include <Bentley/WString.h>
11 #include <functional>
12 
13 #include <BeHttp/Http.h>
16 #include <BeHttp/HttpResponse.h>
17 #include <BeHttp/IHttpHandler.h>
18 #include <folly/futures/Future.h>
19 
21 
22 
23 //=======================================================================================
24 // @bsiclass Bentley Systems
25 //=======================================================================================
26 struct Request
27  {
28  enum class RetryOption
29  {
30  DontRetry,
33  };
34 
35  typedef std::function<void (double bytesTransfered, double bytesTotal)> ProgressCallback;
37 
38 private:
39  Utf8String m_url;
40  Utf8String m_method;
41  Utf8String m_proxyUrl;
42  bool m_validateCertificate = false;
43  HttpRequestHeaders m_requestHeaders;
44  HttpBodyPtr m_requestBody;
45  HttpBodyPtr m_responseBody;
46  Tasks::ICancellationTokenPtr m_cancellationToken;
47  ProgressCallback m_uploadProgressCallback;
48  ProgressCallback m_downloadProgressCallback;
49  unsigned m_connectionTimeoutSeconds = 60;
50  unsigned m_transferTimeoutSeconds = 60;
51  RetryOption m_retryOption = RetryOption::DontRetry;
52  unsigned m_retriesMax = 0;
53  bool m_followRedirects = true;
54  bool m_useNewConnection = false;
55  IHttpHandlerPtr m_httpHandler;
56 
57 public:
59 
60  BEHTTP_EXPORT Request(Utf8StringCR url, Utf8StringCR method = "GET");
61 
62  HttpRequestHeadersR GetHeaders(){return m_requestHeaders;}
63  HttpRequestHeadersCR GetHeaders() const {return m_requestHeaders;}
64 
65  Utf8StringCR GetUrl() const {return m_url;}
66  Utf8StringCR GetMethod() const {return m_method;}
67 
69  void SetValidateCertificate(bool validate) {m_validateCertificate = validate;}
70  bool GetValidateCertificate() const {return m_validateCertificate;}
71 
72  // Set proxy for request. Pass empty string to not use proxy
73  void SetProxy(Utf8StringCR proxyUrl) {m_proxyUrl = EscapeUnsafeSymbolsInUrl(proxyUrl);}
74  Utf8StringCR GetProxy() const {return m_proxyUrl;}
75 
76  // Request body to send, default is no body
77  void SetRequestBody(HttpBodyPtr body) {m_requestBody = body;}
78  HttpBodyPtr GetRequestBody() const {return m_requestBody;}
79 
80  // Response body to write received data to
81  void SetResponseBody(HttpBodyPtr body) {m_responseBody = body;}
82  HttpBodyPtr GetResponseBody() const {return m_responseBody;}
83 
85  void SetTimeoutSeconds(unsigned connectionAndTransferTimeout) {m_connectionTimeoutSeconds = connectionAndTransferTimeout; m_transferTimeoutSeconds = connectionAndTransferTimeout;}
86 
87  void SetConnectionTimeoutSeconds(unsigned connectionTimeout){m_connectionTimeoutSeconds = connectionTimeout;}
88  unsigned GetConnectionTimeoutSeconds() const {return m_connectionTimeoutSeconds;}
89 
90  void SetTransferTimeoutSeconds(unsigned transferTimeout) {m_transferTimeoutSeconds = transferTimeout;}
91  unsigned GetTransferTimeoutSeconds() const {return m_transferTimeoutSeconds;}
92 
94  void SetFollowRedirects(bool follow){m_followRedirects = follow;}
95  bool GetFollowRedirects() const {return m_followRedirects;}
96 
97  // Used to workaround a CURL bug:
98  // https://sourceforge.net/p/curl/bugs/1275/
99  void SetUseNewConnection(bool useNewConnection) {m_useNewConnection = useNewConnection;}
100  bool GetUseNewConnection() const {return m_useNewConnection;}
101 
102  // Use this if you want to specify settings for poor conectivity. Useful for downloads.
103  // Retries if connection lost happen for maximumRetries or indefinetely if maximumRetries = 0
104  // Default - RetryOption::DontRetry
105  void SetRetryOptions(RetryOption option, unsigned maximumRetries = 1) {m_retryOption = option;m_retriesMax = maximumRetries;}
106  RetryOption const& GetRetryOption() const {return m_retryOption;}
107 
108  unsigned GetMaxRetries() const {return m_retriesMax;}
109 
110  void SetCancellationToken(Tasks::ICancellationTokenPtr token) {m_cancellationToken = token;}
111  Tasks::ICancellationTokenPtr GetCancellationToken() const {return m_cancellationToken;}
112 
113  void SetDownloadProgressCallback(ProgressCallbackCR onProgress) {m_downloadProgressCallback = onProgress;}
114  ProgressCallbackCR GetDownloadProgressCallback() const {return m_downloadProgressCallback;}
115 
116  void SetUploadProgressCallback(ProgressCallbackCR onProgress) {m_uploadProgressCallback = onProgress;}
117  ProgressCallbackCR GetUploadProgressCallback() const {return m_uploadProgressCallback;}
118 
119  // Execute request asynchronously
120  BEHTTP_EXPORT folly::Future<Response> Perform();
121 
122  // DEPRECATED: Use Perform.
123  Tasks::AsyncTaskPtr<Response> PerformAsync() {return m_httpHandler->_PerformRequest(*this);}
124  };
125 
void SetProxy(Utf8StringCR proxyUrl)
Definition: HttpRequest.h:73
bool GetFollowRedirects() const
Definition: HttpRequest.h:95
void SetRequestBody(HttpBodyPtr body)
Definition: HttpRequest.h:77
unsigned GetMaxRetries() const
Definition: HttpRequest.h:108
HttpBodyPtr GetResponseBody() const
Definition: HttpRequest.h:82
void SetCancellationToken(Tasks::ICancellationTokenPtr token)
Definition: HttpRequest.h:110
#define BEGIN_BENTLEY_HTTP_NAMESPACE
Definition: Http.h:17
HttpRequestHeadersR GetHeaders()
Definition: HttpRequest.h:62
const ProgressCallback & ProgressCallbackCR
Definition: HttpRequest.h:36
Tasks::ICancellationTokenPtr GetCancellationToken() const
Definition: HttpRequest.h:111
void SetResponseBody(HttpBodyPtr body)
Definition: HttpRequest.h:81
bool GetValidateCertificate() const
Definition: HttpRequest.h:70
Utf8StringCR GetUrl() const
Definition: HttpRequest.h:65
struct Bentley::Utf8String const & Utf8StringCR
Definition: Bentley.h:241
void SetConnectionTimeoutSeconds(unsigned connectionTimeout)
Definition: HttpRequest.h:87
ProgressCallbackCR GetUploadProgressCallback() const
Definition: HttpRequest.h:117
HttpRequestHeadersCR GetHeaders() const
Definition: HttpRequest.h:63
#define BEHTTP_EXPORT
Definition: Http.h:14
ProgressCallbackCR GetDownloadProgressCallback() const
Definition: HttpRequest.h:114
void SetDownloadProgressCallback(ProgressCallbackCR onProgress)
Definition: HttpRequest.h:113
HttpBodyPtr GetRequestBody() const
Definition: HttpRequest.h:78
Utf8StringCR GetProxy() const
Definition: HttpRequest.h:74
void SetFollowRedirects(bool follow)
Enable or disable following redirects. Default is true.
Definition: HttpRequest.h:94
unsigned GetConnectionTimeoutSeconds() const
Definition: HttpRequest.h:88
bool GetUseNewConnection() const
Definition: HttpRequest.h:100
RefCountedPtr< HttpBody > HttpBodyPtr
Definition: HttpBody.h:20
std::shared_ptr< struct ICancellationToken > ICancellationTokenPtr
Definition: CancellationToken.h:49
Tasks::AsyncTaskPtr< Response > PerformAsync()
Definition: HttpRequest.h:123
BEHTTP_EXPORT Request(Utf8StringCR url, Utf8StringCR method="GET")
void SetRetryOptions(RetryOption option, unsigned maximumRetries=1)
Definition: HttpRequest.h:105
RetryOption const & GetRetryOption() const
Definition: HttpRequest.h:106
Definition: HttpHeaders.h:64
HttpRequestHeaders const & HttpRequestHeadersCR
Definition: HttpHeaders.h:32
BEHTTP_EXPORT folly::Future< Response > Perform()
BEGIN_BENTLEY_HTTP_NAMESPACE typedef std::shared_ptr< struct IHttpHandler > IHttpHandlerPtr
Definition: IHttpHandler.h:15
BEHTTP_EXPORT Utf8String EscapeUnsafeSymbolsInUrl(Utf8StringCR url)
Definition: HttpRequest.h:26
void SetUseNewConnection(bool useNewConnection)
Definition: HttpRequest.h:99
void SetUploadProgressCallback(ProgressCallbackCR onProgress)
Definition: HttpRequest.h:116
RetryOption
Definition: HttpRequest.h:28
#define END_BENTLEY_HTTP_NAMESPACE
Definition: Http.h:18
void SetTimeoutSeconds(unsigned connectionAndTransferTimeout)
Sets connection and transfer timeouts with one value. Default - 60 seconds.
Definition: HttpRequest.h:85
void SetValidateCertificate(bool validate)
Enable or disable server certificate and hostname validation.
Definition: HttpRequest.h:69
Utf8StringCR GetMethod() const
Definition: HttpRequest.h:66
unsigned GetTransferTimeoutSeconds() const
Definition: HttpRequest.h:91
void SetTransferTimeoutSeconds(unsigned transferTimeout)
Definition: HttpRequest.h:90
std::function< void(double bytesTransfered, double bytesTotal)> ProgressCallback
Definition: HttpRequest.h:35

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.