$darkmode
curl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
22 #pragma once
23 
24 #include <string>
25 #include <vector>
26 
27 namespace oak {
28 
29 struct Curl {
30  std::string method;
31  std::string address;
32  int port;
33  std::string endpoint;
34  std::string data;
35  std::vector<std::string> headers;
36 
37  public:
38  static Curl get(const std::string& address, int port, const std::string& endpoint) {
39  Curl c;
40  c.method = "GET";
41  c.address = address;
42  c.port = port;
43  c.endpoint = endpoint;
44  return std::move(c);
45  }
46 
47  static Curl post(const std::string& address, int port, const std::string& endpoint,
48  const std::string& data, const std::string& mime_type) {
49  Curl c;
50  c.method = "POST";
51  c.address = address;
52  c.port = port;
53  c.endpoint = endpoint;
54  c.data = data;
55  c.headers.push_back(std::string("Content-Type: ") + mime_type);
56  return c;
57  }
58 
59  std::string to_string() const {
60  std::string buf;
61  buf += "curl -q";
62  buf += " -X " + method;
63  for (const auto& h : headers) {
64  buf += " -H '" + h + "'";
65  }
66  if (data != "") {
67  buf += " -d '" + data + "'";
68  }
69  buf += " http://" + address + ":" + std::to_string(port);
70  if (endpoint != "" && endpoint[0] != '/') {
71  buf += "/";
72  }
73  buf += endpoint;
74  return buf;
75  }
76 };
77 
78 } // namespace oak
Definition: curl.hpp:29