Skip to content

Commit ad2c28b

Browse files
authored
Change SignUpHereUser method to a sync task (#1290)
Change using Network object directly to using OlpClient's CallApi method to make requests. Additionally added a check for a valid network_request_handler variable. Move the request to a lambda function wich is executed by TaskScheduler. Add a test for checking SignUpHereUser method with default credentials and properties parameters. Remove a test which checks Network class(send method) as these calls aren't a part of AuthenticationClientImpl now. Relates-To: OLPEDGE-2020 Signed-off-by: Yevhenii Dudnyk <[email protected]>
1 parent cf988aa commit ad2c28b

File tree

2 files changed

+39
-90
lines changed

2 files changed

+39
-90
lines changed

olp-cpp-sdk-authentication/src/AuthenticationClientImpl.cpp

Lines changed: 31 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -555,81 +555,46 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(
555555
client::CancellationToken AuthenticationClientImpl::SignUpHereUser(
556556
const AuthenticationCredentials& credentials,
557557
const SignUpProperties& properties, const SignUpCallback& callback) {
558-
if (!settings_.network_request_handler) {
559-
thread::ExecuteOrSchedule(settings_.task_scheduler, [callback] {
560-
callback(
561-
client::ApiError::NetworkConnection("Cannot sign up while offline"));
562-
});
563-
return client::CancellationToken();
564-
}
565-
std::weak_ptr<http::Network> weak_network(settings_.network_request_handler);
566-
std::string url = settings_.token_endpoint_url;
567-
url.append(kUserEndpoint);
568-
http::NetworkRequest request(url);
569-
auto network_settings =
570-
http::NetworkSettings()
571-
.WithTransferTimeout(settings_.retry_settings.timeout)
572-
.WithConnectionTimeout(settings_.retry_settings.timeout)
573-
.WithProxySettings(settings_.network_proxy_settings.get_value_or({}));
558+
using ResponseType = client::ApiResponse<SignUpResult, client::ApiError>;
574559

575-
request.WithVerb(http::NetworkRequest::HttpVerb::POST);
560+
auto signup_task = [=](client::CancellationContext context) -> ResponseType {
561+
if (!settings_.network_request_handler) {
562+
return client::ApiError::NetworkConnection(
563+
"Cannot sign up while offline");
564+
}
576565

577-
auto auth_header = GenerateAuthorizationHeader(
578-
credentials, url, std::time(nullptr), GenerateUid());
566+
if (context.IsCancelled()) {
567+
return client::ApiError::Cancelled();
568+
}
579569

580-
request.WithHeader(http::kAuthorizationHeader, std::move(auth_header));
581-
request.WithHeader(http::kContentTypeHeader, kApplicationJson);
582-
request.WithHeader(http::kUserAgentHeader, http::kOlpSdkUserAgent);
583-
request.WithSettings(std::move(network_settings));
570+
client::OlpClient client = CreateOlpClient(settings_, {}, false);
584571

585-
std::shared_ptr<std::stringstream> payload =
586-
std::make_shared<std::stringstream>();
587-
request.WithBody(GenerateSignUpBody(properties));
588-
auto send_outcome = settings_.network_request_handler->Send(
589-
request, payload,
590-
[callback, payload,
591-
credentials](const http::NetworkResponse& network_response) {
592-
auto response_status = network_response.GetStatus();
593-
auto error_msg = network_response.GetError();
572+
const auto url = settings_.token_endpoint_url + kUserEndpoint;
573+
auto auth_header = GenerateAuthorizationHeader(
574+
credentials, url, std::time(nullptr), GenerateUid());
594575

595-
if (response_status < 0) {
596-
// Network error response
597-
AuthenticationError error(response_status, error_msg);
598-
callback(error);
599-
return;
600-
}
576+
client::OlpClient::ParametersType headers = {
577+
{http::kAuthorizationHeader, std::move(auth_header)}};
601578

602-
auto document = std::make_shared<rapidjson::Document>();
603-
rapidjson::IStreamWrapper stream(*payload.get());
604-
document->ParseStream(stream);
605-
606-
std::shared_ptr<SignUpResultImpl> resp_impl =
607-
std::make_shared<SignUpResultImpl>(response_status, error_msg,
608-
document);
609-
SignUpResult response(resp_impl);
610-
callback(response);
611-
});
579+
auto signup_response = client.CallApi(kUserEndpoint, "POST", {}, headers,
580+
{}, GenerateSignUpBody(properties),
581+
kApplicationJson, std::move(context));
612582

613-
if (!send_outcome.IsSuccessful()) {
614-
thread::ExecuteOrSchedule(settings_.task_scheduler, [send_outcome,
615-
callback] {
616-
std::string error_message =
617-
ErrorCodeToString(send_outcome.GetErrorCode());
618-
AuthenticationError result({static_cast<int>(send_outcome.GetErrorCode()),
619-
std::move(error_message)});
620-
callback(result);
621-
});
622-
return client::CancellationToken();
623-
}
583+
const auto status = signup_response.GetStatus();
584+
std::string response_text;
585+
signup_response.GetResponse(response_text);
586+
if (status < 0) {
587+
return client::ApiError(status, response_text);
588+
}
624589

625-
auto request_id = send_outcome.GetRequestId();
626-
return client::CancellationToken([weak_network, request_id]() {
627-
auto network = weak_network.lock();
590+
auto document = std::make_shared<rapidjson::Document>();
591+
document->Parse(response_text.c_str());
592+
return {std::make_shared<SignUpResultImpl>(
593+
status, olp::http::HttpErrorToString(status), document)};
594+
};
628595

629-
if (network) {
630-
network->Cancel(request_id);
631-
}
632-
});
596+
return AddTask(settings_.task_scheduler, pending_requests_,
597+
std::move(signup_task), std::move(callback));
633598
}
634599

635600
client::CancellationToken AuthenticationClientImpl::SignOut(

olp-cpp-sdk-authentication/tests/AuthenticationClientTest.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ TEST(AuthenticationClientTest, AuthenticationWithoutNetwork) {
8888
}
8989
}
9090

91-
TEST(AuthenticationClientTest, AuthenticationWithUnsuccessfulSend) {
91+
TEST(AuthenticationClientTest, SignUpWithUnsuccessfulSend) {
9292
using testing::_;
9393

9494
auth::AuthenticationSettings settings;
@@ -108,29 +108,13 @@ TEST(AuthenticationClientTest, AuthenticationWithUnsuccessfulSend) {
108108

109109
const auth::AuthenticationCredentials credentials("", "");
110110

111-
{
112-
SCOPED_TRACE("SignUpHereUser, Unsuccessful send");
113-
114-
auth_impl.SignUpHereUser(
115-
credentials, {},
116-
[=](const auth::AuthenticationClient::SignUpResponse& response) {
117-
EXPECT_FALSE(response.IsSuccessful());
118-
EXPECT_EQ(response.GetError().GetErrorCode(),
119-
client::ErrorCode::Unknown);
120-
});
121-
}
122-
123-
{
124-
SCOPED_TRACE("SignOut, Unsuccessful send");
125-
126-
auth_impl.SignOut(
127-
credentials, {},
128-
[=](const auth::AuthenticationClient::SignOutUserResponse& response) {
129-
EXPECT_FALSE(response.IsSuccessful());
130-
EXPECT_EQ(response.GetError().GetErrorCode(),
131-
client::ErrorCode::Unknown);
132-
});
133-
}
111+
auth_impl.SignUpHereUser(
112+
credentials, {},
113+
[=](const auth::AuthenticationClient::SignUpResponse& response) {
114+
EXPECT_FALSE(response.IsSuccessful());
115+
EXPECT_EQ(response.GetError().GetErrorCode(),
116+
client::ErrorCode::Unknown);
117+
});
134118
}
135119

136120
TEST(AuthenticationClientTest, Timestamp) {

0 commit comments

Comments
 (0)