Fix for SVACE and Klocwork issues.
[platform/upstream/iotivity.git] / service / simulator / src / client / request_sender.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "request_sender.h"
22 #include "simulator_logger.h"
23 #include "simulator_utils.h"
24 #include "logger.h"
25
26 #define TAG "GET_REQUEST_SNDR"
27
28 GETRequestSender::GETRequestSender(const std::shared_ptr<OC::OCResource> &ocResource)
29     :   m_ocResource(ocResource) {}
30
31 SimulatorResult GETRequestSender::send(const ResponseCallback &callback)
32 {
33     std::map<std::string, std::string> queryParams;
34     return send(queryParams, callback);
35 }
36
37 SimulatorResult GETRequestSender::send(const std::map<std::string, std::string> &queryParams,
38                                        const ResponseCallback &callback)
39 {
40     // Create request info
41     RequestInfo requestInfo;
42     requestInfo.type = RequestType::RQ_TYPE_GET;
43     requestInfo.queryParams = queryParams;
44
45     SIM_LOG(ILogger::INFO, "Sending GET request." << "\n" << getRequestString(queryParams))
46
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);
51 }
52
53 void GETRequestSender::onResponseReceived(const OC::HeaderOptions &headerOptions,
54         const OC::OCRepresentation &rep, const int errorCode, RequestInfo &requestInfo,
55         ResponseCallback callback)
56 {
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);
60 }
61
62 PUTRequestSender::PUTRequestSender(const std::shared_ptr<OC::OCResource> &ocResource)
63     :   m_ocResource(ocResource) {}
64
65 SimulatorResult PUTRequestSender::send(const SimulatorResourceModel &representation,
66                                        const ResponseCallback &callback)
67 {
68     std::map<std::string, std::string> queryParams;
69     return send(queryParams, representation, callback);
70 }
71
72 SimulatorResult PUTRequestSender::send(const std::map<std::string, std::string> &queryParams,
73                                        const SimulatorResourceModel &representation, const ResponseCallback &callback)
74 {
75     // Create request info
76     RequestInfo requestInfo;
77     requestInfo.type = RequestType::RQ_TYPE_PUT;
78     requestInfo.queryParams = queryParams;
79     requestInfo.payLoad = representation;
80
81     OC::OCRepresentation ocRep = representation.asOCRepresentation();
82     SIM_LOG(ILogger::INFO, "Sending PUT request." << "\n**Payload Details**\n" << getRequestString(
83                 queryParams, ocRep));
84
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);
89 }
90
91 void PUTRequestSender::onResponseReceived(const OC::HeaderOptions &headerOptions,
92         const OC::OCRepresentation &rep, const int errorCode, RequestInfo &requestInfo,
93         ResponseCallback callback)
94 {
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);
98 }
99
100 POSTRequestSender::POSTRequestSender(const std::shared_ptr<OC::OCResource> &ocResource)
101     :   m_ocResource(ocResource) {}
102
103 SimulatorResult POSTRequestSender::send(const SimulatorResourceModel &representation,
104                                         const ResponseCallback &callback)
105 {
106     std::map<std::string, std::string> queryParams;
107     return send(queryParams, representation, callback);
108 }
109
110 SimulatorResult POSTRequestSender::send(const std::map<std::string, std::string> &queryParams,
111                                         const SimulatorResourceModel &representation, const ResponseCallback &callback)
112 {
113     // Create request info
114     RequestInfo requestInfo;
115     requestInfo.type = RequestType::RQ_TYPE_POST;
116     requestInfo.queryParams = queryParams;
117     requestInfo.payLoad = representation;
118
119     OC::OCRepresentation ocRep = representation.asOCRepresentation();
120     SIM_LOG(ILogger::INFO, "Sending POST request." << "\n**Payload Details**\n" << getRequestString(
121                 queryParams, ocRep));
122
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);
127 }
128
129 void POSTRequestSender::onResponseReceived(const OC::HeaderOptions &headerOptions,
130         const OC::OCRepresentation &rep, const int errorCode, RequestInfo &requestInfo,
131         ResponseCallback callback)
132 {
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);
136 }