| 61 | } // namespace |
| 62 | |
| 63 | void UserService::UpdateUserData() |
| 64 | { |
| 65 | auto& oauthService = GetOAuthService(); |
| 66 | |
| 67 | if (!oauthService.HasAccessToken()) |
| 68 | return; |
| 69 | |
| 70 | using namespace audacity::network_manager; |
| 71 | |
| 72 | Request request(GetServiceConfig().GetAPIUrl("/me")); |
| 73 | |
| 74 | request.setHeader( |
| 75 | common_headers::Authorization, |
| 76 | std::string(oauthService.GetAccessToken())); |
| 77 | |
| 78 | request.setHeader( |
| 79 | common_headers::Accept, common_content_types::ApplicationJson); |
| 80 | |
| 81 | SetOptionalHeaders(request); |
| 82 | |
| 83 | auto response = NetworkManager::GetInstance().doGet(request); |
| 84 | |
| 85 | response->setRequestFinishedCallback( |
| 86 | [response, this](auto) |
| 87 | { |
| 88 | const auto httpCode = response->getHTTPCode(); |
| 89 | |
| 90 | if (httpCode != 200) |
| 91 | return; |
| 92 | |
| 93 | const auto body = response->readAll<std::string>(); |
| 94 | |
| 95 | using namespace rapidjson; |
| 96 | |
| 97 | Document document; |
| 98 | document.Parse(body.data(), body.size()); |
| 99 | |
| 100 | if (!document.IsObject()) |
| 101 | return; |
| 102 | |
| 103 | const auto id = document["id"].GetString(); |
| 104 | const auto username = document["username"].GetString(); |
| 105 | const auto avatar = document["avatar"].GetString(); |
| 106 | const auto profileName = document["profile"]["name"].GetString(); |
| 107 | |
| 108 | BasicUI::CallAfter( |
| 109 | [this, |
| 110 | id = std::string(id), |
| 111 | username = std::string(username), |
| 112 | profileName = std::string(profileName), |
| 113 | avatar = std::string(avatar)]() |
| 114 | { |
| 115 | userId.Write(audacity::ToWXString(id)); |
| 116 | userName.Write(audacity::ToWXString(username)); |
| 117 | displayName.Write(audacity::ToWXString(profileName)); |
| 118 | |
| 119 | gPrefs->Flush(); |
| 120 |
no test coverage detected