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 
55  // OCS configurations
56  void setOCSNamespace (std::string& OCSNamespace) {m_OCSNamespace = OCSNamespace; }
57  void setOCSTenantId (std::string& OCSTenantId) {m_OCSTenantId = OCSTenantId; }
58  void setOCSClientId (std::string& OCSClientId) {m_OCSClientId = OCSClientId; }
59  void setOCSClientSecret (std::string& OCSClientSecret) {m_OCSClientSecret = OCSClientSecret; }
60  void setOCSToken (std::string& OCSToken) {m_OCSToken = OCSToken; }
61 
62  std::string getHostPort() { return m_host_port; };
63  std::string getHTTPResponse() { return m_HTTPResponse; };
64 
65 private:
66  // Make private the copy constructor and operator=
67  LibcurlHttps(const LibcurlHttps&);
68  LibcurlHttps& operator=(LibcurlHttps const &);
69 
70  void setLibCurlOptions(CURL *sender, const std::string& path, const vector<pair<std::string, std::string>>& headers);
71  void setTrace();
72  void resetTrace();
73 
74 private:
75  CURL *m_sender;
76  std::string m_HTTPResponse;
77  std::string m_host_port;
78  unsigned int m_retry_sleep_time; // Seconds between each retry
79  unsigned int m_max_retry; // Max number of retries in the communication
80  std::string m_authMethod; // Authentication method to be used
81  std::string m_authBasicCredentials; // Credentials is the base64 encoding of id and password joined by a single colon (:)
82  struct curl_slist *m_chunk = NULL;
83  unsigned int m_request_timeout;
84  unsigned int m_connect_timeout;
85 
86  // OCS configurations
87  std::string m_OCSNamespace;
88  std::string m_OCSTenantId;
89  std::string m_OCSClientId;
90  std::string m_OCSClientSecret;
91  std::string m_OCSToken;
92  std::ofstream m_ofs;
93  bool m_log;
94 };
95 
96 #endif
Definition: asset_tracking.h:108
Definition: libcurl_https.h:21
Definition: http_sender.h:20