Fledge
An open source edge computing platform for industrial users
http_sender.h
1 #ifndef _HTTP_SENDER_H
2 #define _HTTP_SENDER_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
11  */
12 
13 #include <string>
14 #include <vector>
15 
16 #define HTTP_SENDER_USER_AGENT "Fledge http sender"
17 #define HTTP_SENDER_DEFAULT_METHOD "GET"
18 #define HTTP_SENDER_DEFAULT_PATH "/"
19 
21 {
22  public:
26  HttpSender();
27 
28  // Destructor
29  virtual ~HttpSender();
30 
34  virtual void setProxy(const std::string& proxy) = 0;
35 
39  virtual int sendRequest(
40  const std::string& method = std::string(HTTP_SENDER_DEFAULT_METHOD),
41  const std::string& path = std::string(HTTP_SENDER_DEFAULT_PATH),
42  const std::vector<std::pair<std::string, std::string>>& headers = {},
43  const std::string& payload = std::string()
44  ) = 0;
45 
46  virtual std::string getHostPort() = 0;
47  virtual std::string getHTTPResponse() = 0;
48 
49  virtual void setAuthMethod (std::string& authMethod) = 0;
50  virtual void setAuthBasicCredentials(std::string& authBasicCredentials) = 0;
51 
52  // OCS configurations
53  virtual void setOCSNamespace (std::string& OCSNamespace) = 0;
54  virtual void setOCSTenantId (std::string& OCSTenantId) = 0;
55  virtual void setOCSClientId (std::string& OCSClientId) = 0;
56  virtual void setOCSClientSecret (std::string& OCSClientSecret) = 0;
57  virtual void setOCSToken (std::string& OCSToken) = 0;
58 
64  static std::string getOMFTracePath();
65 
70  static bool createDebugTraceDirectory();
71 };
72 
76 class BadRequest : public std::exception {
77  public:
78  // Constructor with parameter
79  BadRequest(const std::string& serverReply)
80  {
81  m_errmsg = serverReply;
82  };
83 
84  virtual const char *what() const throw()
85  {
86  return m_errmsg.c_str();
87  }
88 
89  private:
90  std::string m_errmsg;
91 };
92 
96 class Unauthorized : public std::exception {
97  public:
98  // Constructor with parameter
99  Unauthorized (const std::string& serverReply)
100  {
101  m_errmsg = serverReply;
102  };
103 
104  virtual const char *what() const throw()
105  {
106  return m_errmsg.c_str();
107  }
108 
109  private:
110  std::string m_errmsg;
111 };
112 
116 class Conflict : public std::exception {
117  public:
118  // Constructor with parameter
119  Conflict (const std::string& serverReply)
120  {
121  m_errmsg = serverReply;
122  };
123 
124  virtual const char *what() const throw()
125  {
126  return m_errmsg.c_str();
127  }
128 
129  private:
130  std::string m_errmsg;
131 };
132 
133 #endif
virtual void setProxy(const std::string &proxy)=0
Set a proxy server.
virtual 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())=0
HTTP(S) request: pass method and path, HTTP headers and POST/PUT payload.
Unauthorized exception.
Definition: http_sender.h:96
HttpSender()
Constructor:
Definition: http_sender.cpp:26
static std::string getOMFTracePath()
Constructs the file path for the OMF log.
Definition: http_sender.cpp:102
BadRequest exception.
Definition: http_sender.h:76
static bool createDebugTraceDirectory()
Creates the &#39;/logs/debug-trace&#39; subdirectory in the Fledge data directory.
Definition: http_sender.cpp:42
Conflict exception.
Definition: http_sender.h:116
Definition: http_sender.h:20
virtual ~HttpSender()
Destructor.
Definition: http_sender.cpp:33