Skip to content

Commit

Permalink
AudioCom config can be read from preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
crsib committed Oct 26, 2023
1 parent 02fceab commit 3aace49
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 27 deletions.
3 changes: 2 additions & 1 deletion libraries/lib-cloud-audiocom/OAuthService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ void WriteCommonFields(
document.GetAllocator());

const auto clientID = GetServiceConfig().GetOAuthClientID();
const auto clientSecret = GetServiceConfig().GetOAuthClientSecret();

document.AddMember(
"client_id", StringRef(clientID.data(), clientID.size()),
document.GetAllocator());

document.AddMember(
"client_secret", StringRef("shKqnY2sLTfRK7hztwzNEVxnmhJfOy1i"),
"client_secret", StringRef(clientSecret.data(), clientSecret.size()),
document.GetAllocator());

document.AddMember(
Expand Down
120 changes: 101 additions & 19 deletions libraries/lib-cloud-audiocom/ServiceConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,135 @@
#include "ServiceConfig.h"
#include "Languages.h"

#include <rapidjson/document.h>
#include <cassert>
#include <stdexcept>
#include <string_view>

#include <rapidjson/document.h>

#include "Prefs.h"
#include "CodeConversions.h"



namespace cloud::audiocom
{
std::string_view ServiceConfig::GetAPIEndpoint() const
namespace
{
return "https://api.audio.com";
StringSetting audioComApiEndpoint {
L"/CloudServices/AudioCom/ApiEndpoint",
L"https://api.audio.com"
};

StringSetting audioComOAuthClientID {
L"/CloudServices/AudioCom/OAuthClientID",
L"1741964426607541"
};

StringSetting audioComOAuthClientSecret {
L"/CloudServices/AudioCom/OAuthClientSecret",
L"shKqnY2sLTfRK7hztwzNEVxnmhJfOy1i"
};

StringSetting audioComOAuthRedirectURL {
L"/CloudServices/AudioCom/OAuthRedirectURL",
L"https://audio.com/auth/sign-in/success"
};

StringSetting audioComOAuthLoginPage {
L"/CloudServices/AudioCom/OAuthLoginPage",
L"https://audio.com/audacity/link?clientId={auth_client_id}"
};

StringSetting audioComFinishUploadPage {
L"/CloudServices/AudioCom/OAuthLoginPage",
L"https://audio.com/audacity/upload?audioId={audio_id}&token={auth_token}&clientId={auth_client_id}"
};

StringSetting audioComAudioURL {
L"/CloudServices/AudioCom/AudioURL",
L"https://audio.com/{user_slug}/audio/{audio_slug}/edit"
};

StringSetting audioComAudioPreferredMimeType {
L"/CloudServices/AudioCom/PreferredMimeType",
L"https://audio.com/{user_slug}/audio/{audio_slug}/edit"
};

std::string Substitute(std::string pattern, std::initializer_list<std::pair<std::string_view, std::string_view>> substitutions)
{
for(auto& [key, value] : substitutions)
{
auto pos = pattern.find(key);

assert(pos != std::string::npos);

if(pos != std::string::npos)
pattern.replace(pos, key.size(), value);
}

return std::move(pattern);
}

} // namespace

ServiceConfig::ServiceConfig()
{
mApiEndpoint = audacity::ToUTF8(audioComApiEndpoint.Read());
mOAuthClientID = audacity::ToUTF8(audioComOAuthClientID.Read());
mOAuthClientSecret = audacity::ToUTF8(audioComOAuthClientSecret.Read());
mOAuthRedirectURL = audacity::ToUTF8(audioComOAuthRedirectURL.Read());
mOAuthLoginPage = audacity::ToUTF8(audioComOAuthLoginPage.Read());
mFinishUploadPage = audacity::ToUTF8(audioComFinishUploadPage.Read());
mAudioURL = audacity::ToUTF8(audioComAudioURL.Read());
mPreferredMimeType = audacity::ToUTF8(audioComAudioPreferredMimeType.Read());
}

std::string_view ServiceConfig::GetOAuthLoginPage() const
std::string ServiceConfig::GetAPIEndpoint() const
{
static const std::string loginPage =
std::string("https://audio.com/audacity/link?clientId=") +
std::string(GetOAuthClientID());
return mApiEndpoint;
}

return loginPage;
std::string ServiceConfig::GetOAuthLoginPage() const
{
return Substitute(
mOAuthClientID, { { "auth_client_id", GetOAuthClientID() } });
}

std::string ServiceConfig::GetOAuthClientID() const
{
return mOAuthClientID;
}

std::string_view ServiceConfig::GetOAuthClientID() const
std::string ServiceConfig::GetOAuthClientSecret() const
{
return "1741964426607541";
return mOAuthClientSecret;
}

std::string_view ServiceConfig::GetOAuthRedirectURL() const
std::string ServiceConfig::GetOAuthRedirectURL() const
{
//return "audacity://link";
return "https://audio.com/auth/sign-in/success";
return mOAuthRedirectURL;
}

std::string ServiceConfig::GetAPIUrl(std::string_view apiURI) const
{
return std::string(GetAPIEndpoint()) + std::string(apiURI);
return mApiEndpoint + std::string(apiURI);
}

std::string ServiceConfig::GetFinishUploadPage(
std::string_view audioID, std::string_view token) const
{
return "https://audio.com/audacity/upload?audioId=" + std::string(audioID) +
"&token=" + std::string(token) +
"&clientId=" + std::string(GetOAuthClientID());
return Substitute(
mFinishUploadPage, { { "audio_id", audioID },
{ "auth_token", token },
{ "auth_client_id", mOAuthClientID } });
}

std::string ServiceConfig::GetAudioURL(
std::string_view userSlug, std::string_view audioSlug) const
{
return "https://audio.com/" + std::string(userSlug) + "/audio/" + std::string(audioSlug) + "/edit";
return Substitute(
mAudioURL, { { "user_slug", userSlug }, { "audio_slug", audioSlug } });
}

std::chrono::milliseconds ServiceConfig::GetProgressCallbackTimeout() const
Expand Down Expand Up @@ -99,7 +181,7 @@ rapidjson::Document ServiceConfig::GetExportConfig(const std::string& mimeType)

std::string ServiceConfig::GetDownloadMime() const
{
return "audio/x-wav";
return mPreferredMimeType;
}

std::string ServiceConfig::GetAcceptLanguageValue() const
Expand Down
27 changes: 20 additions & 7 deletions libraries/lib-cloud-audiocom/ServiceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <chrono>
#include <string>
#include <string_view>
#include <vector>
#include <rapidjson/fwd.h>

Expand All @@ -23,14 +22,17 @@ namespace cloud::audiocom
class CLOUD_AUDIOCOM_API ServiceConfig final
{
public:
ServiceConfig();
//! API endpoint
std::string_view GetAPIEndpoint() const;
//! Page to open in browser to initiate OAuth
std::string_view GetOAuthLoginPage() const;
std::string GetAPIEndpoint() const;
//! Page to open in browser to initiate OAuth
std::string GetOAuthLoginPage() const;
//! OAuth2 client ID
std::string_view GetOAuthClientID() const;
std::string GetOAuthClientID() const;
//! OAuth2 client secret
std::string GetOAuthClientSecret() const;
//! OAuth2 redirect URL. Only used to satisfy the protocol
std::string_view GetOAuthRedirectURL() const;
std::string GetOAuthRedirectURL() const;
//! Helper to construct the full URLs for the API
std::string GetAPIUrl(std::string_view apiURI) const;
//! Helper to construct the page URL for the anonymous upload last stage
Expand All @@ -43,10 +45,21 @@ class CLOUD_AUDIOCOM_API ServiceConfig final
std::vector<std::string> GetPreferredAudioFormats() const;
//! Export configuration suitable for the mime type provided
rapidjson::Document GetExportConfig(const std::string& exporterName) const;
//! Return the mime type server should store the file. This is a requirement from audiocom
//! Return the mime type server should store the file. This is a requirement
//! from audiocom
std::string GetDownloadMime() const;
//! Returns the preferred language
std::string GetAcceptLanguageValue() const;

private:
std::string mApiEndpoint;
std::string mOAuthClientID;
std::string mOAuthClientSecret;
std::string mOAuthRedirectURL;
std::string mOAuthLoginPage;
std::string mFinishUploadPage;
std::string mAudioURL;
std::string mPreferredMimeType;
};

//! Returns the instance of the ServiceConfig
Expand Down

0 comments on commit 3aace49

Please sign in to comment.