1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "request_sender.h"
22 #include "simulator_logger.h"
23 #include "simulator_utils.h"
26 #define TAG "GET_REQUEST_SNDR"
28 GETRequestSender::GETRequestSender(const std::shared_ptr<OC::OCResource> &ocResource)
29 : m_ocResource(ocResource) {}
31 SimulatorResult GETRequestSender::send(const ResponseCallback &callback)
33 std::map<std::string, std::string> queryParams;
34 return send(queryParams, callback);
37 SimulatorResult GETRequestSender::send(const std::map<std::string, std::string> &queryParams,
38 const ResponseCallback &callback)
40 // Create request info
41 RequestInfo requestInfo;
42 requestInfo.type = RequestType::RQ_TYPE_GET;
43 requestInfo.queryParams = queryParams;
45 SIM_LOG(ILogger::INFO, "Sending GET request." << "\n" << getRequestString(queryParams))
47 OCStackResult ocResult = m_ocResource->get(queryParams,
48 std::bind(&GETRequestSender::onResponseReceived, this, std::placeholders::_1,
49 std::placeholders::_2, std::placeholders::_3, requestInfo, callback));
50 return static_cast<SimulatorResult>(ocResult);
53 void GETRequestSender::onResponseReceived(const OC::HeaderOptions &headerOptions,
54 const OC::OCRepresentation &rep, const int errorCode, RequestInfo &requestInfo,
55 ResponseCallback callback)
57 SIM_LOG(ILogger::INFO, "Response received for GET." << "\n" << getPayloadString(rep));
58 SimulatorResourceModel resourceModel = SimulatorResourceModel::build(rep);
59 callback(static_cast<SimulatorResult>(errorCode), resourceModel, requestInfo);
62 PUTRequestSender::PUTRequestSender(const std::shared_ptr<OC::OCResource> &ocResource)
63 : m_ocResource(ocResource) {}
65 SimulatorResult PUTRequestSender::send(const SimulatorResourceModel &representation,
66 const ResponseCallback &callback)
68 std::map<std::string, std::string> queryParams;
69 return send(queryParams, representation, callback);
72 SimulatorResult PUTRequestSender::send(const std::map<std::string, std::string> &queryParams,
73 const SimulatorResourceModel &representation, const ResponseCallback &callback)
75 // Create request info
76 RequestInfo requestInfo;
77 requestInfo.type = RequestType::RQ_TYPE_PUT;
78 requestInfo.queryParams = queryParams;
79 requestInfo.payLoad = representation;
81 OC::OCRepresentation ocRep = representation.asOCRepresentation();
82 SIM_LOG(ILogger::INFO, "Sending PUT request." << "\n**Payload Details**\n" << getRequestString(
85 OCStackResult ocResult = m_ocResource->put(ocRep, queryParams,
86 std::bind(&PUTRequestSender::onResponseReceived, this, std::placeholders::_1,
87 std::placeholders::_2, std::placeholders::_3, requestInfo, callback));
88 return static_cast<SimulatorResult>(ocResult);
91 void PUTRequestSender::onResponseReceived(const OC::HeaderOptions &headerOptions,
92 const OC::OCRepresentation &rep, const int errorCode, RequestInfo &requestInfo,
93 ResponseCallback callback)
95 SIM_LOG(ILogger::INFO, "Response received for PUT." << "\n" << getPayloadString(rep));
96 SimulatorResourceModel resourceModel = SimulatorResourceModel::build(rep);
97 callback(static_cast<SimulatorResult>(errorCode), resourceModel, requestInfo);
100 POSTRequestSender::POSTRequestSender(const std::shared_ptr<OC::OCResource> &ocResource)
101 : m_ocResource(ocResource) {}
103 SimulatorResult POSTRequestSender::send(const SimulatorResourceModel &representation,
104 const ResponseCallback &callback)
106 std::map<std::string, std::string> queryParams;
107 return send(queryParams, representation, callback);
110 SimulatorResult POSTRequestSender::send(const std::map<std::string, std::string> &queryParams,
111 const SimulatorResourceModel &representation, const ResponseCallback &callback)
113 // Create request info
114 RequestInfo requestInfo;
115 requestInfo.type = RequestType::RQ_TYPE_POST;
116 requestInfo.queryParams = queryParams;
117 requestInfo.payLoad = representation;
119 OC::OCRepresentation ocRep = representation.asOCRepresentation();
120 SIM_LOG(ILogger::INFO, "Sending POST request." << "\n**Payload Details**\n" << getRequestString(
121 queryParams, ocRep));
123 OCStackResult ocResult = m_ocResource->post(ocRep, queryParams,
124 std::bind(&POSTRequestSender::onResponseReceived, this, std::placeholders::_1,
125 std::placeholders::_2, std::placeholders::_3, requestInfo, callback));
126 return static_cast<SimulatorResult>(ocResult);
129 void POSTRequestSender::onResponseReceived(const OC::HeaderOptions &headerOptions,
130 const OC::OCRepresentation &rep, const int errorCode, RequestInfo &requestInfo,
131 ResponseCallback callback)
133 SIM_LOG(ILogger::INFO, "Response received for POST." << "\n" << getPayloadString(rep));
134 SimulatorResourceModel resourceModel = SimulatorResourceModel::build(rep);
135 callback(static_cast<SimulatorResult>(errorCode), resourceModel, requestInfo);