From 8c89278cdff807cb6cc1e99f744962b50e0bd150 Mon Sep 17 00:00:00 2001 From: Ilya Zverev Date: Mon, 25 Jan 2016 14:53:24 +0300 Subject: [PATCH] [editor] A method for getting a user's display name from API --- editor/editor_tests/server_api_test.cpp | 8 +++++++- editor/server_api.cpp | 20 ++++++++++++++++++++ editor/server_api.hpp | 9 +++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/editor/editor_tests/server_api_test.cpp b/editor/editor_tests/server_api_test.cpp index 7df5f053f8..f3af97c056 100644 --- a/editor/editor_tests/server_api_test.cpp +++ b/editor/editor_tests/server_api_test.cpp @@ -28,10 +28,16 @@ namespace ServerApi06 CreateAPI() { OsmOAuth auth = OsmOAuth::DevServerAuth(); - OsmOAuth::AuthResult result = auth.AuthorizePassword(kValidOsmUser, kValidOsmPassword); + OsmOAuth::AuthResult const result = auth.AuthorizePassword(kValidOsmUser, kValidOsmPassword); TEST_EQUAL(result, OsmOAuth::AuthResult::OK, ()); TEST(auth.IsAuthorized(), ("OSM authorization")); ServerApi06 api(auth); + // Test user preferences reading along the way. + osm::UserPreferences prefs; + OsmOAuth::ResponseCode const code = api.GetUserPreferences(prefs); + TEST_EQUAL(code, OsmOAuth::ResponseCode::OK, ("Request user preferences")); + TEST_EQUAL(prefs.m_displayName, kValidOsmUser, ("User display name")); + TEST_EQUAL(prefs.m_id, 3500, ("User id")); return api; } diff --git a/editor/server_api.cpp b/editor/server_api.cpp index 876764133f..2ac876a179 100644 --- a/editor/server_api.cpp +++ b/editor/server_api.cpp @@ -9,6 +9,8 @@ #include "std/sstream.hpp" +#include "3party/pugixml/src/pugixml.hpp" + namespace osm { @@ -103,6 +105,24 @@ OsmOAuth::ResponseCode ServerApi06::TestUserExists(string const & userName) return m_auth.DirectRequest(method, false).first; } +OsmOAuth::ResponseCode ServerApi06::GetUserPreferences(UserPreferences & pref) const +{ + OsmOAuth::Response const response = m_auth.Request("/user/details"); + if (response.first != OsmOAuth::ResponseCode::OK) + return response.first; + pugi::xml_document details; + if (!details.load_string(response.second.c_str())) + return OsmOAuth::ResponseCode::NotFound; + pugi::xml_node user = details.child("osm").child("user"); + if (!user || !user.attribute("id")) + return OsmOAuth::ResponseCode::BadXML; + pref.m_id = user.attribute("id").as_ullong(); + pref.m_displayName = user.attribute("display_name").as_string(); + pref.m_imageUrl = user.child("img").attribute("href").as_string(); + pref.m_changesets = user.child("changesets").attribute("count").as_uint(); + return OsmOAuth::ResponseCode::OK; +} + OsmOAuth::Response ServerApi06::GetXmlFeaturesInRect(m2::RectD const & latLonRect) const { using strings::to_string_dac; diff --git a/editor/server_api.hpp b/editor/server_api.hpp index d4fb8309df..49c4fc15f3 100644 --- a/editor/server_api.hpp +++ b/editor/server_api.hpp @@ -10,6 +10,13 @@ namespace osm { +struct UserPreferences +{ + uint64_t m_id; + string m_displayName; + string m_imageUrl; + uint32_t m_changesets; +}; /// All methods here are synchronous and need wrappers for async usage. /// TODO(AlexZ): Rewrite ServerAPI interface to accept XMLFeature. @@ -31,6 +38,8 @@ public: /// This function can be used to check if user did not confirm email validation link after registration. /// @returns OK if user exists, NotFound if it is not, and ServerError if there is no connection. OsmOAuth::ResponseCode TestUserExists(string const & userName); + /// A convenience method for UI + OsmOAuth::ResponseCode GetUserPreferences(UserPreferences & pref) const; /// Please use at least created_by=* and comment=* tags. bool CreateChangeSet(TKeyValueTags const & kvTags, uint64_t & outChangeSetId) const; /// nodeXml should be wrapped into ... tags.