@@ -555,81 +555,46 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(
555555client::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
635600client::CancellationToken AuthenticationClientImpl::SignOut (
0 commit comments