Skip to content

Commit d251fca

Browse files
author
Andrei Popescu
authored
Adapts OlpClient to the coding style and simplifies internals. (#683)
Two unnecessary moves in ApiClientLookup::LookupApi() removed, as this resulted together with changing OlpClient::SetSettings() take settings in as copy to crash as this will move twice the same resource which is not possible. Added aliases for input parameters on OlpClient::CallApi() to make the footprint smaller. Internally refactored a little to make better use of the method chaining from NetworkSettings and NetworkRequest. Moved Bearer header to a free function. Move constructor and destructor default definition to cpp file. Relates-to: OLPEDGE-567 Signed-off-by: Andrei Popescu <[email protected]>
1 parent 232bc9d commit d251fca

File tree

3 files changed

+164
-183
lines changed

3 files changed

+164
-183
lines changed

olp-cpp-sdk-core/include/olp/core/client/OlpClient.h

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,10 +25,9 @@
2525
#include <string>
2626
#include <vector>
2727

28-
#include "CancellationContext.h"
29-
#include "OlpClientSettings.h"
30-
3128
#include <olp/core/CoreApi.h>
29+
#include <olp/core/client/CancellationContext.h>
30+
#include <olp/core/client/OlpClientSettings.h>
3231

3332
namespace olp {
3433
namespace network {
@@ -38,19 +37,24 @@ class NetworkConfig;
3837

3938
namespace client {
4039
/**
41-
* @brief Executes HTTP requests.
40+
* @brief Executes HTTP requests by using the base url and the provided
41+
* parameters and body.
4242
*/
4343
class CORE_API OlpClient {
4444
public:
45+
/// Alias for the parameters and headers type.
46+
using ParametersType = std::multimap<std::string, std::string>;
47+
using RequestBodyType = std::shared_ptr<std::vector<std::uint8_t>>;
48+
4549
OlpClient();
46-
virtual ~OlpClient() = default;
50+
virtual ~OlpClient();
4751

4852
/**
4953
* @brief Sets the base URL used for all requests.
5054
51-
* @param baseUrl The base URL.
55+
* @param base_url The base URL.
5256
*/
53-
void SetBaseUrl(const std::string& baseUrl);
57+
void SetBaseUrl(const std::string& base_url);
5458

5559
/**
5660
* @brief Gets the base URL.
@@ -64,7 +68,7 @@ class CORE_API OlpClient {
6468
*
6569
* @return The default headers.
6670
*/
67-
std::multimap<std::string, std::string>& GetMutableDefaultHeaders();
71+
ParametersType& GetMutableDefaultHeaders();
6872

6973
/**
7074
* @brief Sets the client settings.
@@ -92,14 +96,13 @@ class CORE_API OlpClient {
9296
*
9397
* @return The method used to call or to cancel the request.
9498
*/
95-
CancellationToken CallApi(
96-
const std::string& path, const std::string& method,
97-
const std::multimap<std::string, std::string>& query_params,
98-
const std::multimap<std::string, std::string>& header_params,
99-
const std::multimap<std::string, std::string>& form_params,
100-
const std::shared_ptr<std::vector<unsigned char>>& post_body,
101-
const std::string& content_type,
102-
const NetworkAsyncCallback& callback) const;
99+
CancellationToken CallApi(const std::string& path, const std::string& method,
100+
const ParametersType& query_params,
101+
const ParametersType& header_params,
102+
const ParametersType& form_params,
103+
const RequestBodyType& post_body,
104+
const std::string& content_type,
105+
const NetworkAsyncCallback& callback) const;
103106

104107
/**
105108
* @brief Executes the HTTP request through the network stack in a blocking
@@ -122,24 +125,20 @@ class CORE_API OlpClient {
122125
* @return The `HttpResponse` instance.
123126
*/
124127
HttpResponse CallApi(std::string path, std::string method,
125-
std::multimap<std::string, std::string> query_params,
126-
std::multimap<std::string, std::string> header_params,
127-
std::multimap<std::string, std::string> form_params,
128-
std::shared_ptr<std::vector<unsigned char>> post_body,
129-
std::string content_type,
128+
ParametersType query_params,
129+
ParametersType header_params, ParametersType form_params,
130+
RequestBodyType post_body, std::string content_type,
130131
CancellationContext context) const;
131132

132133
private:
133134
std::shared_ptr<http::NetworkRequest> CreateRequest(
134135
const std::string& path, const std::string& method,
135-
const std::multimap<std::string, std::string>& query_params,
136-
const std::multimap<std::string, std::string>& header_params,
137-
const std::shared_ptr<std::vector<unsigned char>>& post_body,
138-
const std::string& content_type) const;
136+
const ParametersType& query_params, const ParametersType& header_params,
137+
const RequestBodyType& post_body, const std::string& content_type) const;
139138

140139
private:
141140
std::string base_url_;
142-
std::multimap<std::string, std::string> default_headers_;
141+
ParametersType default_headers_;
143142
OlpClientSettings settings_;
144143
};
145144

0 commit comments

Comments
 (0)