From a00b35568ef53e7783991312bc021aab83e9a558 Mon Sep 17 00:00:00 2001 From: "r.kuznetsov" Date: Thu, 29 Nov 2018 18:02:16 +0300 Subject: [PATCH] Fixed requesting basic user details --- map/user.cpp | 89 ++++++++++++++++++++++++++++++++++++++-------------- map/user.hpp | 2 ++ 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/map/user.cpp b/map/user.cpp index d69addb280..9d83dc55f6 100644 --- a/map/user.cpp +++ b/map/user.cpp @@ -367,9 +367,33 @@ void User::Authenticate(std::string const & socialToken, SocialTokenType socialT { Request(url, authParams, [this](std::string const & response) { - SetAccessToken(ParseAccessToken(response)); - FinishAuthentication(); - }, [this](int code, std::string const & response) + // Parse access token. + std::string accessToken; + try + { + accessToken = ParseAccessToken(response); + } + catch (base::Json::Exception const & ex) + { + LOG(LWARNING, ("Access token parsing failed. ", ex.what())); + FinishAuthentication(); + return; + } + + // Request basic user details. + RequestBasicUserDetails(accessToken, [this](std::string const & accessToken) + { + SetAccessToken(accessToken); + FinishAuthentication(); + }, + [this](int code, std::string const & response) + { + LOG(LWARNING, ("Requesting of basic user details failed. Code =", + code, "Response =", response)); + FinishAuthentication(); + }); + }, + [this](int code, std::string const & response) { LOG(LWARNING, ("Authentication failed. Code =", code, "Response =", response)); FinishAuthentication(); @@ -435,7 +459,8 @@ void User::ClearSubscribersImpl() base::EraseIf(m_subscribers, [](auto const & s) { return s == nullptr; }); } -void User::RequestUserDetails() +void User::RequestBasicUserDetails(std::string const & accessToken, + SuccessHandler && onSuccess, ErrorHandler && onError) { std::string const detailsUrl = UserDetailsUrl(); if (detailsUrl.empty()) @@ -444,24 +469,15 @@ void User::RequestUserDetails() return; } - std::string const reviewsUrl = UserReviewsUrl(); - if (reviewsUrl.empty()) + GetPlatform().RunTask(Platform::Thread::Network, [this, detailsUrl, accessToken, + onSuccess = std::move(onSuccess), + onError = std::move(onError)]() { - LOG(LWARNING, ("User reviews service is unavailable.")); - return; - } - - if (m_accessToken.empty()) - return; - - GetPlatform().RunTask(Platform::Thread::Network, [this, detailsUrl, reviewsUrl]() - { - // Request user details. - Request(detailsUrl, [this](platform::HttpClient & request) + Request(detailsUrl, [accessToken](platform::HttpClient & request) { - request.SetRawHeader("Authorization", BuildAuthorizationToken(m_accessToken)); + request.SetRawHeader("Authorization", BuildAuthorizationToken(accessToken)); }, - [this](std::string const & response) + [this, accessToken, onSuccess = std::move(onSuccess)](std::string const & response) { UserDetailsResponseData userDetails; DeserializeFromJson(response, userDetails); @@ -477,11 +493,38 @@ void User::RequestUserDetails() GetPlatform().GetSecureStorage().Save(kUserNameKey, userName); GetPlatform().GetSecureStorage().Save(kUserIdKey, userDetails.m_userId); - std::lock_guard lock(m_mutex); - m_userName = userName; - m_userId = userDetails.m_userId; - }); + // Set user credentials. + { + std::lock_guard lock(m_mutex); + m_userName = userName; + m_userId = userDetails.m_userId; + } + if (onSuccess) + onSuccess(accessToken); + }, + [onError = std::move(onError)](int code, std::string const & response) + { + if (onError) + onError(code, response); + }); + }); +} + +void User::RequestUserDetails() +{ + std::string const reviewsUrl = UserReviewsUrl(); + if (reviewsUrl.empty()) + { + LOG(LWARNING, ("User reviews service is unavailable.")); + return; + } + + if (m_accessToken.empty()) + return; + + GetPlatform().RunTask(Platform::Thread::Network, [this, reviewsUrl]() + { // Request user's reviews. Request(reviewsUrl, [this](platform::HttpClient & request) { diff --git a/map/user.hpp b/map/user.hpp index 171668b81f..69246e157c 100644 --- a/map/user.hpp +++ b/map/user.hpp @@ -80,6 +80,8 @@ public: private: void Init(); void SetAccessToken(std::string const & accessToken); + void RequestBasicUserDetails(std::string const & accessToken, + SuccessHandler && onSuccess, ErrorHandler && onError); void RequestUserDetails(); void Request(std::string const & url, BuildRequestHandler const & onBuildRequest, SuccessHandler const & onSuccess, ErrorHandler const & onError = nullptr);