RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
email.hpp
Go to the documentation of this file.
1 #ifndef RAPP_CLOUD_EMAIL
2 #define RAPP_CLOUD_EMAIL
3 #include "includes.ihh"
4 namespace rapp {
5 namespace cloud {
13 {
14 public:
29  const std::string email,
30  const std::string pwd,
31  const std::string server,
32  const std::string port,
33  const std::string email_status,
34  const unsigned int from_date,
35  const unsigned int to_date,
36  const unsigned int num_emails,
37  const std::string token,
38  std::function<void(std::string)> callback
39  )
40  : asio_service_http(token), delegate_(callback)
41  {
42  boost::property_tree::ptree tree;
43  tree.put("email", email);
44  tree.put("passwd", pwd);
45  tree.put("server", server);
46  tree.put("port", port);
47  tree.put("email_status", email_status);
48  tree.put("from_date", boost::lexical_cast<std::string>(from_date));
49  tree.put("to_date", boost::lexical_cast<std::string>(to_date));
50  tree.put("num_emails", boost::lexical_cast<std::string>(num_emails));
51  std::stringstream ss;
52  boost::property_tree::write_json(ss, tree, false);
53  post_ = ss.str();
54  header_ = "POST /hop/email_fetch HTTP/1.1\r\n"
55  + "Content-Type: application/x-www-form-urlencoded\r\n";
56  callback_ = std::bind(&email_fetch::handle_reply, this, std::placeholders::_1);
57  }
58 
59 private:
63  void handle_reply(std::string json)
64  {
65  std::stringstream ss(json);
66  delegate_(std::move(json));
67  }
68 
70  std::function<void(std::string)> delegate_;
71 };
72 
80 {
81 public:
96  const std::string email,
97  const std::string pwd,
98  const std::string server,
99  const std::string port,
100  const std::vector<std::string> recipients,
101  const std::string body,
102  const std::string subject,
103  const std::vector<rapp::types::byte> data,
104  const std::string token,
105  std::function<void(std::string)> callback
106  )
107  : asio_service_http(token), delegate_(callback)
108  {
109  std::string boundary = random_boundary();
110  std::string fname = random_boundary();
111  boost::property_tree::ptree tree;
112  tree.put("email", email);
113  tree.put("passwd", pwd);
114  tree.put("server", server);
115  tree.put("port", port);
116  tree.put("body", body);
117  tree.put("subject", subject);
118  boost::property_tree::ptree array;
119  for (const auto rec : recipients) {
120  array.push_back(std::make_pair("", rec));
121  }
122  tree.add_child("recipients", array);
123  tree.put("file", fname);
124  std::stringstream ss;
125  boost::property_tree::write_json(ss, tree, false);
126  post_ = "--" + boundary + "\r\n"
127  + "Content-Disposition: form-data; name=\"json\"\r\n\r\n"
128  + ss.str() + "\r\n";
129  // new multipart - append binary data
130  post_ += "--" + boundary + "\r\n"
131  + "Content-Disposition: form-data; name=\"file_uri\"; filename\"" + fname + "\"\r\n"
132  + "Content-Transfer-Encoding: binary\r\n\r\n";
133  post_.insert(post_.end(), data.begin(), data.end());
134  post_ += "\r\n--" + boundary + "--";
135  // form the Header
136  header_ = "POST /hop/email_send HTTP/1.1\r\n";
137  + "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n\r\n";
138  callback_ = std::bind(&email_send::handle_reply, this, std::placeholders::_1);
139  }
140 
141 private:
145  void handle_reply(std::string json)
146  {
147  std::stringstream ss(json);
148  delegate_(std::move(json));
149  }
150 
152  std::function<void(std::string)> delegate_;
153 };
154 }
155 }
156 #endif
std::string header_
Header that will be used.
void handle_reply(std::string json)
handle platform's JSON reply
Definition: email.hpp:145
std::string random_boundary() const
Create a random boundary for the multipart/form in HTTP.
std::function< void(std::string)> delegate_
Definition: email.hpp:70
constexpr char port[]
api.rapp.cloud - HOP server port
Definition: asio_socket.hpp:10
std::string post_
Actual post Data.
email_send(const std::string email, const std::string pwd, const std::string server, const std::string port, const std::vector< std::string > recipients, const std::string body, const std::string subject, const std::vector< rapp::types::byte > data, const std::string token, std::function< void(std::string)> callback)
construct in order to send email
Definition: email.hpp:95
std::function< void(std::string)> callback_
Callback Handler - use with std::bind or boost variant.
send an email
Definition: email.hpp:79
void handle_reply(std::string json)
handle platform's JSON reply
Definition: email.hpp:63
std::function< void(std::string)> delegate_
Definition: email.hpp:152
fetch email(s)
Definition: email.hpp:12
email_fetch(const std::string email, const std::string pwd, const std::string server, const std::string port, const std::string email_status, const unsigned int from_date, const unsigned int to_date, const unsigned int num_emails, const std::string token, std::function< void(std::string)> callback)
construct in order to acquire email(s)
Definition: email.hpp:28
base class for asynchronous http websockets used for connecting to cloud services ...