diff --git a/docs/classmastodonpp_1_1CURLWrapper-members.html b/docs/classmastodonpp_1_1CURLWrapper-members.html index 4f37947..b0bbfcd 100644 --- a/docs/classmastodonpp_1_1CURLWrapper-members.html +++ b/docs/classmastodonpp_1_1CURLWrapper-members.html @@ -77,7 +77,7 @@ $(function() { CURLWrapper(const CURLWrapper &other)=defaultmastodonpp::CURLWrapper CURLWrapper(CURLWrapper &&other) noexcept=defaultmastodonpp::CURLWrapper get_curl_easy_handle()mastodonpp::CURLWrapperinline - make_request(const http_method &method, const string_view &uri)mastodonpp::CURLWrapperprotected + make_request(const http_method &method, string uri, const parametermap &parameters)mastodonpp::CURLWrapperprotected operator=(const CURLWrapper &other)=defaultmastodonpp::CURLWrapper operator=(CURLWrapper &&other) noexcept=defaultmastodonpp::CURLWrapper ~CURLWrapper() noexceptmastodonpp::CURLWrappervirtual diff --git a/docs/classmastodonpp_1_1CURLWrapper.html b/docs/classmastodonpp_1_1CURLWrapper.html index 1f5bfc6..ccc6fa7 100644 --- a/docs/classmastodonpp_1_1CURLWrapper.html +++ b/docs/classmastodonpp_1_1CURLWrapper.html @@ -116,9 +116,9 @@ Public Member Functions - - - + + +

Protected Member Functions

answer_type make_request (const http_method &method, const string_view &uri)
 Make a request. More...
 
answer_type make_request (const http_method &method, string uri, const parametermap &parameters)
 Make a HTTP request. More...
 

Detailed Description

Handles the details of network connections.

@@ -143,18 +143,18 @@ Protected Member Functions

Initializes curl and sets up connection.

The first time an instance of CURLWrapper is created, it calls curl_global_init, which is not thread-safe. For more information consult curl_global_init(3).

Since
0.1.0
-
36  : _curl_buffer_error{}
-
37 {
-
38  if (curlwrapper_instances == 0)
-
39  {
-
40  curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
-
41  }
-
42  ++curlwrapper_instances;
-
43  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (+1)\n";
-
44 
-
45  _connection = curl_easy_init();
-
46  setup_curl();
-
47 }
+
42  : _curl_buffer_error{}
+
43 {
+
44  if (curlwrapper_instances == 0)
+
45  {
+
46  curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
+
47  }
+
48  ++curlwrapper_instances;
+
49  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (+1)\n";
+
50 
+
51  _connection = curl_easy_init();
+
52  setup_curl();
+
53 }
@@ -240,16 +240,16 @@ Protected Member Functions

Cleans up curl and connection.

Calls curl_global_cleanup, which is not thread-safe. For more information consult curl_global_cleanup(3).

Since
0.1.0
-
49 {
-
50  curl_easy_cleanup(_connection);
-
51 
-
52  --curlwrapper_instances;
-
53  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
-
54  if (curlwrapper_instances == 0)
-
55  {
-
56  curl_global_cleanup();
-
57  }
-
58 }
+
55 {
+
56  curl_easy_cleanup(_connection);
+
57 
+
58  --curlwrapper_instances;
+
59  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
+
60  if (curlwrapper_instances == 0)
+
61  {
+
62  curl_global_cleanup();
+
63  }
+
64 }
@@ -286,8 +286,8 @@ Protected Member Functions - -

◆ make_request()

+ +

◆ make_request()

@@ -304,8 +304,14 @@ Protected Member Functions - const string_view &  - uri  + string  + uri, + + + + + const parametermap &  + parameters  @@ -320,88 +326,134 @@ Protected Member Functions
-

Make a request.

+

Make a HTTP request.

Parameters
- + +
methodThe HTTP method.
uriThe full URI.
uriThe full URI.
parametersA map of parameters.
Since
0.1.0
-
62 {
-
63  debuglog << "Making request to: " << uri << '\n';
-
64 
-
65  CURLcode code;
-
66  switch (method)
-
67  {
-
68  case http_method::GET:
-
69  {
-
70  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
-
71  code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
-
72  break;
-
73  }
-
74  case http_method::POST:
-
75  {
-
76  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
-
77  code = curl_easy_setopt(_connection, CURLOPT_POST, 1L);
-
78  break;
-
79  }
-
80  case http_method::PATCH:
-
81  {
-
82  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
-
83  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PATCH");
-
84  break;
-
85  }
-
86  case http_method::PUT:
-
87  {
-
88  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
-
89  code = curl_easy_setopt(_connection, CURLOPT_UPLOAD, 1L);
-
90  break;
-
91  }
-
92  case http_method::DELETE:
-
93  {
-
94  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
-
95  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "DELETE");
-
96  break;
-
97  }
-
98  }
-
99  if (code != CURLE_OK)
-
100  {
-
101  throw CURLException{code, "Failed to set HTTP method",
-
102  _curl_buffer_error};
-
103  }
-
104 
-
105  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
-
106  code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
-
107  if (code != CURLE_OK)
-
108  {
-
109  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
-
110  }
-
111 
-
112  answer_type answer;
-
113  code = curl_easy_perform(_connection);
-
114  if (code == CURLE_OK)
-
115  {
-
116  long http_status; // NOLINT(google-runtime-int)
-
117  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
-
118  curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
-
119  answer.http_status = static_cast<uint16_t>(http_status);
-
120  debuglog << "HTTP status code: " << http_status << '\n';
+
68 {
+
69  CURLcode code;
+
70  switch (method)
+
71  {
+
72  case http_method::GET:
+
73  {
+
74  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+
75  code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
+
76 
+
77  for (const auto &param : parameters)
+
78  {
+
79  static constexpr array replace_in_uri
+
80  {
+
81  "id", "nickname", "nickname_or_id",
+
82  "hashtag", "permission_group"
+
83  };
+
84  if (any_of(replace_in_uri.begin(), replace_in_uri.end(),
+
85  [&param](const auto &s) { return s == param.first; }))
+
86  {
+
87  const auto pos{uri.find('<')};
+
88  if (pos != string::npos)
+
89  {
+
90  uri.replace(pos, param.first.size() + 2,
+
91  get<string>(param.second));
+
92  }
+
93  continue;
+
94  }
+
95  static bool first{true};
+
96  if (first)
+
97  {
+
98  uri.append("?");
+
99  first = false;
+
100  }
+
101  else
+
102  {
+
103  uri.append("&");
+
104  }
+
105  if (holds_alternative<string>(param.second))
+
106  {
+
107  uri.append(param.first + '=' + get<string>(param.second));
+
108  }
+
109  else
+
110  {
+
111  for (const auto &arg : get<vector<string>>(param.second))
+
112  {
+
113  uri.append(param.first + "[]=" + arg);
+
114  if (arg != *get<vector<string>>(param.second).rbegin())
+
115  {
+
116  uri.append("&");
+
117  }
+
118  }
+
119  }
+
120  }
121 
-
122  answer.headers = _curl_buffer_headers;
-
123  answer.body = _curl_buffer_body;
-
124  }
-
125  else
-
126  {
-
127  answer.curl_error_code = static_cast<uint8_t>(code);
-
128  answer.error_message = _curl_buffer_error;
-
129  debuglog << "libcurl error: " << code << '\n';
-
130  debuglog << _curl_buffer_error << '\n';
-
131  }
-
132 
-
133  return answer;
-
134 }
+
122  break;
+
123  }
+
124  case http_method::POST:
+
125  {
+
126  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+
127  code = curl_easy_setopt(_connection, CURLOPT_POST, 1L);
+
128  break;
+
129  }
+
130  case http_method::PATCH:
+
131  {
+
132  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+
133  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PATCH");
+
134  break;
+
135  }
+
136  case http_method::PUT:
+
137  {
+
138  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+
139  code = curl_easy_setopt(_connection, CURLOPT_UPLOAD, 1L);
+
140  break;
+
141  }
+
142  case http_method::DELETE:
+
143  {
+
144  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+
145  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "DELETE");
+
146  break;
+
147  }
+
148  }
+
149  if (code != CURLE_OK)
+
150  {
+
151  throw CURLException{code, "Failed to set HTTP method",
+
152  _curl_buffer_error};
+
153  }
+
154  debuglog << "Making request to: " << uri << '\n';
+
155 
+
156  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+
157  code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
+
158  if (code != CURLE_OK)
+
159  {
+
160  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
+
161  }
+
162 
+
163  answer_type answer;
+
164  code = curl_easy_perform(_connection);
+
165  if (code == CURLE_OK)
+
166  {
+
167  long http_status; // NOLINT(google-runtime-int)
+
168  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+
169  curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
+
170  answer.http_status = static_cast<uint16_t>(http_status);
+
171  debuglog << "HTTP status code: " << http_status << '\n';
+
172 
+
173  answer.headers = _curl_buffer_headers;
+
174  answer.body = _curl_buffer_body;
+
175  }
+
176  else
+
177  {
+
178  answer.curl_error_code = static_cast<uint8_t>(code);
+
179  answer.error_message = _curl_buffer_error;
+
180  debuglog << "libcurl error: " << code << '\n';
+
181  debuglog << _curl_buffer_error << '\n';
+
182  }
+
183 
+
184  return answer;
+
185 }
diff --git a/docs/classmastodonpp_1_1Connection-members.html b/docs/classmastodonpp_1_1Connection-members.html index 04cae95..febe376 100644 --- a/docs/classmastodonpp_1_1Connection-members.html +++ b/docs/classmastodonpp_1_1Connection-members.html @@ -77,10 +77,10 @@ $(function() { CURLWrapper()mastodonpp::CURLWrapper CURLWrapper(const CURLWrapper &other)=defaultmastodonpp::CURLWrapper CURLWrapper(CURLWrapper &&other) noexcept=defaultmastodonpp::CURLWrapper - get(const API::endpoint_type &endpoint)mastodonpp::Connection - get(const string_view &endpoint)mastodonpp::Connection + get(const endpoint_variant &endpoint, const parametermap &parameters)mastodonpp::Connection + get(const endpoint_variant &endpoint)mastodonpp::Connectioninline get_curl_easy_handle()mastodonpp::CURLWrapperinline - make_request(const http_method &method, const string_view &uri)mastodonpp::CURLWrapperprotected + make_request(const http_method &method, string uri, const parametermap &parameters)mastodonpp::CURLWrapperprotected operator=(const CURLWrapper &other)=defaultmastodonpp::CURLWrapper operator=(CURLWrapper &&other) noexcept=defaultmastodonpp::CURLWrapper ~CURLWrapper() noexceptmastodonpp::CURLWrappervirtual diff --git a/docs/classmastodonpp_1_1Connection.html b/docs/classmastodonpp_1_1Connection.html index a3116ec..63db500 100644 --- a/docs/classmastodonpp_1_1Connection.html +++ b/docs/classmastodonpp_1_1Connection.html @@ -93,12 +93,12 @@ Public Member Functions  Connection (Instance &instance)  Construct a new Connection object. More...
  -answer_type get (const API::endpoint_type &endpoint) - Make a HTTP GET call. More...
-  -answer_type get (const string_view &endpoint) - Make a HTTP GET call. More...
-  +answer_type get (const endpoint_variant &endpoint, const parametermap &parameters) + Make a HTTP GET call with parameters. More...
+  +answer_type get (const endpoint_variant &endpoint) + Make a HTTP GET call. Example: More...
- Public Member Functions inherited from mastodonpp::CURLWrapper  CURLWrapper ()  Initializes curl and sets up connection. More...
@@ -125,9 +125,9 @@ Public Member Functions

Additional Inherited Members

- Protected Member Functions inherited from mastodonpp::CURLWrapper -answer_type make_request (const http_method &method, const string_view &uri) - Make a request. More...
-  +answer_type make_request (const http_method &method, string uri, const parametermap &parameters) + Make a HTTP request. More...

Detailed Description

Represents a connection to an instance. Used for requests.

@@ -165,47 +165,54 @@ Additional Inherited Members
Since
0.1.0
-
23  : _instance{instance}
-
24  , _baseuri{instance.get_baseuri()}
-
25 {}
+
25  : _instance{instance}
+
26  , _baseuri{instance.get_baseuri()}
+
27 {}

Member Function Documentation

- -

◆ get() [1/2]

+ +

◆ get() [1/2]

+ + + + + +
- +
answer_type mastodonpp::Connection::get (const API::endpoint_typeconst endpoint_variant &  endpoint)
+
+inline
-

Make a HTTP GET call.

-
Parameters
+

Make a HTTP GET call. Example:

+
auto answer{connection.get("/api/v1/instance")};
+
Parameters
- +
endpointEndpoint as API::endpoint_type, for example: mastodonpp::API::v1::instance.
endpointEndpoint as API::endpoint_type or std::string.
Since
0.1.0
-
28 {
-
29  return make_request(
-
30  http_method::GET,
-
31  string(_baseuri).append(API{endpoint}.to_string_view()));
-
32 }
+
92  {
+
93  return get(endpoint, {});
+
94  }
- -

◆ get() [2/2]

+ +

◆ get() [2/2]

@@ -213,24 +220,50 @@ Additional Inherited Members answer_type mastodonpp::Connection::get ( - const string_view &  - endpoint) + const endpoint_variant &  + endpoint, + + + + const parametermap &  + parameters  + + + + ) +
-

Make a HTTP GET call.

-
Parameters
+

Make a HTTP GET call with parameters.

+

Example:

auto answer{connection.get(mastodonpp::API::v1::accounts_id_followers,
+
{
+
{"id", "12"},
+
{"limit", "10"}
+
})};
+
Parameters
- + +
endpointEndpoint as string, for example: "/api/v1/instance".
endpointEndpoint as API::endpoint_type or std::string.
parametersA map of parameters.
Since
0.1.0
-
35 {
-
36  return make_request(http_method::GET, string(_baseuri).append(endpoint));
-
37 }
+
31 {
+
32  string uri{[&]
+
33  {
+
34  if (holds_alternative<API::endpoint_type>(endpoint))
+
35  {
+
36  return string(_baseuri).append(
+
37  API{std::get<API::endpoint_type>(endpoint)}.to_string_view());
+
38  }
+
39 
+
40  return std::get<string>(endpoint);
+
41  }()};
+
42  return make_request(http_method::GET, uri, parameters);
+
43 }
@@ -239,7 +272,8 @@ Additional Inherited Members
  • src/connection.cpp
  • -
    answer_type make_request(const http_method &method, const string_view &uri)
    Make a request.
    Definition: curl_wrapper.cpp:60
    +
    answer_type get(const endpoint_variant &endpoint, const parametermap &parameters)
    Make a HTTP GET call with parameters.
    Definition: connection.cpp:29
    +
    answer_type make_request(const http_method &method, string uri, const parametermap &parameters)
    Make a HTTP request.
    Definition: curl_wrapper.cpp:66
    -
    Connection(Instance &instance)
    Construct a new Connection object.
    Definition: connection.cpp:22
    +
    answer_type get(const endpoint_variant &endpoint, const parametermap &parameters)
    Make a HTTP GET call with parameters.
    Definition: connection.cpp:29
    +
    Connection(Instance &instance)
    Construct a new Connection object.
    Definition: connection.cpp:24
    +
    answer_type get(const endpoint_variant &endpoint)
    Make a HTTP GET call. Example:
    Definition: connection.hpp:91
    C++ wrapper for the Mastodon API.
    Definition: answer.cpp:19
    -
    answer_type get(const API::endpoint_type &endpoint)
    Make a HTTP GET call.
    Definition: connection.cpp:27
    Return type for Requests.
    Definition: answer.hpp:40
    +
    map< string, variant< string, vector< string > >> parametermap
    std::map of parameters for API calls.
    Definition: curl_wrapper.hpp:67
    Holds the access data of an instance.
    Definition: instance.hpp:40
    -
    variant< v1, v2, oauth, other, pleroma > endpoint_type
    Type for endpoints. Can be API::v1, API::v2, API::oauth, API::other or API::pleroma.
    Definition: api.hpp:295
    -
    Represents a connection to an instance. Used for requests.
    Definition: connection.hpp:41
    +
    Represents a connection to an instance. Used for requests.
    Definition: connection.hpp:45
    Handles the details of network connections.
    Definition: curl_wrapper.hpp:78