Fledge
An open source edge computing platform for industrial users
libcurl_https.h
1 #ifndef _LIBCURL_HTTPS_H
2 #define _LIBCURL_HTTPS_H
3 /*
4  * Fledge HTTP Sender wrapper.
5  *
6  * Copyright (c) 2019 Diamnomic Systems
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Stefano Simonelli
11  */
12 
13 #include <string>
14 #include <vector>
15 #include <http_sender.h>
16 #include <curl/curl.h>
17 #include <fstream>
18 
19 using namespace std;
20 
21 class LibcurlHttps: public HttpSender
22 {
23 public:
28  LibcurlHttps(const std::string& host_port,
29  unsigned int connect_timeout = 0,
30  unsigned int request_timeout = 0,
31  unsigned int retry_sleep_Time = 1,
32  unsigned int max_retry = 4);
33 
34  // Destructor
35  ~LibcurlHttps();
36 
40  void setProxy(const std::string& proxy);
41 
45  int sendRequest(
46  const std::string& method = std::string(HTTP_SENDER_DEFAULT_METHOD),
47  const std::string& path = std::string(HTTP_SENDER_DEFAULT_PATH),
48  const std::vector<std::pair<std::string, std::string>>& headers = {},
49  const std::string& payload = std::string()
50  );
51 
52  void setAuthMethod (std::string& authMethod) {m_authMethod = authMethod; }
53  void setAuthBasicCredentials(std::string& authBasicCredentials) {m_authBasicCredentials = authBasicCredentials; }
54  void setMaxRetries (unsigned int retries) {m_max_retry = retries; };
55 
56  // OCS configurations
57  void setOCSNamespace (std::string& OCSNamespace) {m_OCSNamespace = OCSNamespace; }
58  void setOCSTenantId (std::string& OCSTenantId) {m_OCSTenantId = OCSTenantId; }
59  void setOCSClientId (std::string& OCSClientId) {m_OCSClientId = OCSClientId; }
60  void setOCSClientSecret (std::string& OCSClientSecret) {m_OCSClientSecret = OCSClientSecret; }
61  void setOCSToken (std::string& OCSToken) {m_OCSToken = OCSToken; }
62 
63  std::string getHostPort() { return m_host_port; };
64  std::string getHTTPResponse() { return m_HTTPResponse; };
65  unsigned int getMaxRetries() { return m_max_retry; };
66 
67 private:
68  // Make private the copy constructor and operator=
69  LibcurlHttps(const LibcurlHttps&);
70  LibcurlHttps& operator=(LibcurlHttps const &);
71 
72  void setLibCurlOptions(CURL *sender, const std::string& path, const vector<pair<std::string, std::string>>& headers);
73  void setTrace();
74  void resetTrace();
75 
76 private:
77  CURL *m_sender;
78  std::string m_HTTPResponse;
79  std::string m_host_port;
80  unsigned int m_retry_sleep_time; // Seconds between each retry
81  unsigned int m_max_retry; // Max number of retries in the communication
82  std::string m_authMethod; // Authentication method to be used
83  std::string m_authBasicCredentials; // Credentials is the base64 encoding of id and password joined by a single colon (:)
84  struct curl_slist *m_chunk = NULL;
85  unsigned int m_request_timeout;
86  unsigned int m_connect_timeout;
87 
88  // OCS configurations
89  std::string m_OCSNamespace;
90  std::string m_OCSTenantId;
91  std::string m_OCSClientId;
92  std::string m_OCSClientSecret;
93  std::string m_OCSToken;
94  std::ofstream m_ofs;
95  bool m_log;
96 };
97 
98 #endif
LibcurlHttps
Definition: libcurl_https.h:21
HttpSender
Definition: http_sender.h:20