identt
MailSmsService.hpp
Go to the documentation of this file.
1 
33 #ifndef _IDENTT_QUERY_MAILSMS_SERVICE_HPP_
34 #define _IDENTT_QUERY_MAILSMS_SERVICE_HPP_
35 
36 #include <query/QueryBase.hpp>
37 #include <store/MailSmsService.hpp>
38 #include <hrpc/HrpcClient.hpp>
39 
40 namespace identt {
41 namespace query {
42 
43 template <class HttpServerT>
45  public identt::query::ServiceBase<HttpServerT>,
47 public:
48 
69  identt::utils::SharedTable::pointer stptr,
70  typename std::shared_ptr<HttpServerT> server,
71  ::identt::query::HelpQuery::pointer helpquery,
72  unsigned int scope)
73  : identt::query::ServiceBase<HttpServerT>(IDENTT_SERVICE_SCOPE_HTTP | IDENTT_SERVICE_SCOPE_HTTPS)
74  {
75  if (!(this->myscope & scope)) return; // scope mismatch
76 
77  // Endpoint : POST _identt/identity/api/v1/getmailstosend
78  helpquery->add({scope,"POST _identt/identity/api/v1/getmailstosend", {
79  "params : lastid, limit, payload[] ",
80  "Needs shared secret in header.",
81  "This will fetch the mail/sms that need sending.",
82  "If request payload contains items marked done they will be marked deleted",
83  "This is to be used only by an internal program"
84  }
85  });
86 
87  server->resource["/_identt/identity/api/v1/getmailstosend$"]["POST"]
88  =[this,stptr](typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request) {
89  IDENTT_PARALLEL_ONE([this,stptr,response,request] {
90  try {
91  LOG(INFO) << request->path;
92  std::string err;
93  bool use_json = this->JsonRequest(request);
94 
95  ::identt::mail::MailQueryT mailq;
96  if (use_json)
97  {
98  int stat = json2pb( request->content.string() , &mailq , err);
99  if (stat<0) throw SydentException("Bad Json Format",M_BAD_JSON);
100  } else {
101  form2pb( request->content.string() , &mailq); // throws on error
102  }
103  if (!stptr->is_ready.Get()) throw identt::BadDataException("System Not Ready");
104 
105  // check shared secret
106  auto it=request->header.find("Shared-Secret");
107  if (it==request->header.end())
108  throw identt::BadDataException("No Header Shared Secret");
109  if (it->second != stptr->shared_secret.Get())
110  throw identt::BadDataException("Bad Shared Secret");
111  mailq.set_shared_secret(it->second);
112 
114  // action
115  if (stptr->is_master.Get())
116  {
117  mservice.PendingAction(stptr, &mailq);
118  } else {
119  identt::hrpc::HrpcClient hclient;
120  hclient.SendToMaster(stptr,::identt::hrpc::M_PENDING,&mailq);
121  }
122 
123  mailq.clear_shared_secret();
124 
125  // aftermath
126  std::string output;
127  pb2json(&mailq , output);
128 
129  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
130  } catch (SydentException& e)
131  {
132  int ecode = (e.ecode()>=IDENTT_SYDENT_ERROR_MIN && e.ecode()<=IDENTT_SYDENT_ERROR_MAX) ? e.ecode() : M_UNKNOWN;
133  std::string output = err2json(SydentErrors.at(ecode),e.what());
134  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
135  } catch (identt::JdException& e)
136  {
137  std::string output = err2json(SydentErrors.at(M_UNKNOWN),e.what());
138  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
139  } catch (std::exception& e)
140  {
141  std::string output = err2json(SydentErrors.at(M_UNKNOWN),e.what());
142  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
143  } catch (...)
144  {
145  this->HttpErrorAction(response,request,500,"INTERNAL SERVER ERROR");
146  }
147  });
148  };
149 
150  }
151 
152 private:
153 
154 };
155 } // namespace query
156 } // namespace identt
157 
158 #endif // _IDENTT_QUERY_MAILSMS_SERVICE_HPP_
159 
void HttpOKAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ec, const char *em, const char *content_type, std::string &payload, bool add_cors)
HttpOKAction : OK Action template.
Definition: ServiceBase.hpp:154
Definition: MailSmsService.hpp:44
Sydent Exceptions.
Definition: SydentQuery.hpp:91
void PendingAction(::identt::utils::SharedTable::pointer stptr, identt::mail::MailQueryT *mailq)
PendingAction : Get Pending as required.
Definition: MailSmsService.cc:46
void HttpErrorAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ec, const char *em)
HttpErrorAction : Error Action Template no payload maybe template.
Definition: ServiceBase.hpp:85
Definition: BaseUtils.hpp:52
void pb2json(const google::protobuf::Message *msg, std::string &str)
pb2json : Convert protobuf to json
Definition: ProtoJson.cc:415
Definition: MailSmsService.hpp:42
Definition: ServiceBase.hpp:55
Definition: CryptoBase.hpp:49
Definition: BaseUtils.hpp:89
void form2pb(const std::string &form, google::protobuf::Message *msg)
form2pb : Convert url-encoded form to protobuf
Definition: ProtoForm.cc:129
MailSmsService(identt::utils::SharedTable::pointer stptr, typename std::shared_ptr< HttpServerT > server, ::identt::query::HelpQuery::pointer helpquery, unsigned int scope)
MailSmsService : constructor.
Definition: MailSmsService.hpp:68
std::string err2json(const std::string errorcode, const std::string error)
err2json : Generate a Json for Error Message
Definition: ProtoJson.cc:511
bool JsonRequest(typename HttpServerT::ReqPtr request)
JsonRequest : Check if request is json from Content-Type and Accept fields.
Definition: ServiceBase.hpp:178
Definition: HrpcClient.hpp:46
int json2pb(const std::string &json, google::protobuf::Message *msg, std::string &err)
json2pb : Convert json to protobuf
Definition: ProtoJson.cc:446
bool SendToMaster(::identt::utils::SharedTable::pointer stptr, ::identt::hrpc::MasterCmdTypeE service_id, google::protobuf::Message *msg, bool nothrow=false)
SendToMaster : send to master and get output.