Fledge
An open source edge computing platform for industrial users
simple_http.h
1 #ifndef _SIMPLE_HTTP_H
2 #define _SIMPLE_HTTP_H
3 /*
4  * Fledge HTTP Sender wrapper.
5  *
6  * Copyright (c) 2018 Dianomic Systems
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Massimiliano Pinto, Mark Riddoch
11  */
12 
13 #include <string>
14 #include <vector>
15 #include <http_sender.h>
16 #include <client_http.hpp>
17 #include <fstream>
18 
19 using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;
20 
21 class SimpleHttp: public HttpSender
22 {
23  public:
27  SimpleHttp(const std::string& host_port,
28  unsigned int connect_timeout = 0,
29  unsigned int request_timeout = 0,
30  unsigned int retry_sleep_Time = 1,
31  unsigned int max_retry = 4);
32 
33 
34  // Destructor
35  ~SimpleHttp();
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  std::string getHostPort() { return m_host_port; };
56  std::string getHTTPResponse() { return m_HTTPResponse; };
57 
58  // OCS configurations
59  void setOCSNamespace (std::string& OCSNamespace) {m_OCSNamespace = OCSNamespace; }
60  void setOCSTenantId (std::string& OCSTenantId) {m_OCSTenantId = OCSTenantId; }
61  void setOCSClientId (std::string& OCSClientId) {m_OCSClientId = OCSClientId; }
62  void setOCSClientSecret (std::string& OCSClientSecret) {m_OCSClientSecret = OCSClientSecret; }
63  void setOCSToken (std::string& OCSToken) {m_OCSToken = OCSToken; }
64 
65 
66  private:
67  // Make private the copy constructor and operator=
68  SimpleHttp(const SimpleHttp&);
69  SimpleHttp& operator=(SimpleHttp const &);
70  void setTrace();
71  void resetTrace();
72 
73  private:
74  std::string m_host_port;
75  HttpClient *m_sender;
76  std::string m_HTTPResponse;
77  unsigned int m_retry_sleep_time; // Seconds between each retry
78  unsigned int m_max_retry; // Max number of retries in the communication
79 
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 
83  // OCS configurations
84  std::string m_OCSNamespace;
85  std::string m_OCSTenantId;
86  std::string m_OCSClientId;
87  std::string m_OCSClientSecret;
88  std::string m_OCSToken;
89  bool m_log;
90  std::ofstream m_ofs;
91 };
92 
93 #endif
SimpleHttp(const std::string &host_port, unsigned int connect_timeout=0, unsigned int request_timeout=0, unsigned int retry_sleep_Time=1, unsigned int max_retry=4)
Constructor: pass host:port & connect & request timeouts.
void setProxy(const std::string &proxy)
Set the host and port of the proxy server.
Definition: simple_http.cpp:90
~SimpleHttp()
Destructor.
Definition: simple_http.cpp:98
int sendRequest(const std::string &method=std::string(HTTP_SENDER_DEFAULT_METHOD), const std::string &path=std::string(HTTP_SENDER_DEFAULT_PATH), const std::vector< std::pair< std::string, std::string >> &headers={}, const std::string &payload=std::string())
HTTP(S) request: pass method and path, HTTP headers and POST/PUT payload.
Definition: simple_http.cpp:113
Definition: http_sender.h:20
Definition: simple_http.h:21