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  virtual unsigned int getMaxRetries() = 0;
49 
50  virtual void setAuthMethod (std::string& authMethod) = 0;
51  virtual void setAuthBasicCredentials(std::string& authBasicCredentials) = 0;
52  virtual void setMaxRetries (unsigned int retries) = 0;
53 
54  // OCS configurations
55  virtual void setOCSNamespace (std::string& OCSNamespace) = 0;
56  virtual void setOCSTenantId (std::string& OCSTenantId) = 0;
57  virtual void setOCSClientId (std::string& OCSClientId) = 0;
58  virtual void setOCSClientSecret (std::string& OCSClientSecret) = 0;
59  virtual void setOCSToken (std::string& OCSToken) = 0;
60 
66  static std::string getOMFTracePath();
67 
72  static bool createDebugTraceDirectory();
73 };
74 
78 class BadRequest : public std::exception {
79  public:
80  // Constructor with parameter
81  BadRequest(const std::string& serverReply)
82  {
83  m_errmsg = serverReply;
84  };
85 
86  virtual const char *what() const throw()
87  {
88  return m_errmsg.c_str();
89  }
90 
91  private:
92  std::string m_errmsg;
93 };
94 
98 class Unauthorized : public std::exception {
99  public:
100  // Constructor with parameter
101  Unauthorized (const std::string& serverReply)
102  {
103  m_errmsg = serverReply;
104  };
105 
106  virtual const char *what() const throw()
107  {
108  return m_errmsg.c_str();
109  }
110 
111  private:
112  std::string m_errmsg;
113 };
114 
118 class Conflict : public std::exception {
119  public:
120  // Constructor with parameter
121  Conflict (const std::string& serverReply)
122  {
123  m_errmsg = serverReply;
124  };
125 
126  virtual const char *what() const throw()
127  {
128  return m_errmsg.c_str();
129  }
130 
131  private:
132  std::string m_errmsg;
133 };
134 
135 #endif
Unauthorized
Unauthorized exception.
Definition: http_sender.h:98
HttpSender::createDebugTraceDirectory
static bool createDebugTraceDirectory()
Creates the '/logs/debug-trace' subdirectory in the Fledge data directory.
Definition: http_sender.cpp:42
HttpSender::~HttpSender
virtual ~HttpSender()
Destructor.
Definition: http_sender.cpp:33
Conflict
Conflict exception.
Definition: http_sender.h:118
HttpSender::HttpSender
HttpSender()
Constructor:
Definition: http_sender.cpp:26
HttpSender
Definition: http_sender.h:20
HttpSender::getOMFTracePath
static std::string getOMFTracePath()
Constructs the file path for the OMF log.
Definition: http_sender.cpp:102
HttpSender::sendRequest
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.
BadRequest
BadRequest exception.
Definition: http_sender.h:78
HttpSender::setProxy
virtual void setProxy(const std::string &proxy)=0
Set a proxy server.