input/qobuz: use class DelegateCurlResponseHandler to eliminate duplicate code

This commit is contained in:
Max Kellermann 2018-01-24 11:13:04 +01:00
parent 75e60669a7
commit 4a330a4c33
6 changed files with 120 additions and 161 deletions

View File

@ -40,24 +40,18 @@ static constexpr yajl_callbacks qobuz_error_parser_callbacks = {
QobuzErrorParser::QobuzErrorParser(unsigned _status, QobuzErrorParser::QobuzErrorParser(unsigned _status,
const std::multimap<std::string, std::string> &headers) const std::multimap<std::string, std::string> &headers)
:status(_status), :YajlResponseParser(&qobuz_error_parser_callbacks, nullptr, this),
parser(&qobuz_error_parser_callbacks, nullptr, this) status(_status)
{ {
auto i = headers.find("content-type"); auto i = headers.find("content-type");
if (i == headers.end() || i->second.find("/json") == i->second.npos) if (i == headers.end() || i->second.find("/json") == i->second.npos)
throw FormatRuntimeError("Status %u from Qobuz", status); throw FormatRuntimeError("Status %u from Qobuz", status);
} }
void
QobuzErrorParser::OnData(ConstBuffer<void> data)
{
parser.Parse((const unsigned char *)data.data, data.size);
}
void void
QobuzErrorParser::OnEnd() QobuzErrorParser::OnEnd()
{ {
parser.CompleteParse(); YajlResponseParser::OnEnd();
if (!message.empty()) if (!message.empty())
throw FormatRuntimeError("Error from Qobuz: %s", throw FormatRuntimeError("Error from Qobuz: %s",

View File

@ -21,9 +21,8 @@
#define QOBUZ_ERROR_PARSER_HXX #define QOBUZ_ERROR_PARSER_HXX
#include "check.h" #include "check.h"
#include "lib/yajl/Handle.hxx" #include "lib/yajl/ResponseParser.hxx"
#include <exception>
#include <string> #include <string>
#include <map> #include <map>
@ -33,11 +32,9 @@ struct StringView;
/** /**
* Parse an error JSON response. * Parse an error JSON response.
*/ */
class QobuzErrorParser { class QobuzErrorParser final : public YajlResponseParser {
const unsigned status; const unsigned status;
Yajl::Handle parser;
enum class State { enum class State {
NONE, NONE,
MESSAGE, MESSAGE,
@ -53,16 +50,9 @@ public:
QobuzErrorParser(unsigned status, QobuzErrorParser(unsigned status,
const std::multimap<std::string, std::string> &headers); const std::multimap<std::string, std::string> &headers);
/** protected:
* Feed response body data into the JSON parser. /* virtual methods from CurlResponseParser */
*/ void OnEnd() override;
void OnData(ConstBuffer<void> data);
/**
* Throw an exception describing the error condition. Call
* this at the end of the response body.
*/
void OnEnd();
public: public:
/* yajl callbacks */ /* yajl callbacks */

View File

@ -20,11 +20,12 @@
#include "config.h" #include "config.h"
#include "QobuzLoginRequest.hxx" #include "QobuzLoginRequest.hxx"
#include "QobuzErrorParser.hxx" #include "QobuzErrorParser.hxx"
#include "QobuzSession.hxx"
#include "lib/curl/Form.hxx" #include "lib/curl/Form.hxx"
#include "lib/yajl/Callbacks.hxx" #include "lib/yajl/Callbacks.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
using Wrapper = Yajl::CallbacksWrapper<QobuzLoginRequest>; using Wrapper = Yajl::CallbacksWrapper<QobuzLoginRequest::ResponseParser>;
static constexpr yajl_callbacks parse_callbacks = { static constexpr yajl_callbacks parse_callbacks = {
nullptr, nullptr,
nullptr, nullptr,
@ -39,6 +40,43 @@ static constexpr yajl_callbacks parse_callbacks = {
nullptr, nullptr,
}; };
class QobuzLoginRequest::ResponseParser final : public YajlResponseParser {
enum class State {
NONE,
DEVICE,
DEVICE_ID,
USER_AUTH_TOKEN,
} state = State::NONE;
unsigned map_depth = 0;
QobuzSession session;
public:
explicit ResponseParser() noexcept
:YajlResponseParser(&parse_callbacks, nullptr, this) {}
QobuzSession &&GetSession();
/* yajl callbacks */
bool String(StringView value) noexcept;
bool StartMap() noexcept;
bool MapKey(StringView value) noexcept;
bool EndMap() noexcept;
};
inline QobuzSession &&
QobuzLoginRequest::ResponseParser::GetSession()
{
if (session.user_auth_token.empty())
throw std::runtime_error("No user_auth_token in login response");
if (session.device_id.empty())
throw std::runtime_error("No device id in login response");
return std::move(session);
}
static std::multimap<std::string, std::string> static std::multimap<std::string, std::string>
MakeLoginForm(const char *app_id, MakeLoginForm(const char *app_id,
const char *username, const char *email, const char *username, const char *email,
@ -95,50 +133,26 @@ QobuzLoginRequest::~QobuzLoginRequest() noexcept
request.StopIndirect(); request.StopIndirect();
} }
void std::unique_ptr<CurlResponseParser>
QobuzLoginRequest::OnHeaders(unsigned status, QobuzLoginRequest::MakeParser(unsigned status,
std::multimap<std::string, std::string> &&headers) std::multimap<std::string, std::string> &&headers)
{ {
if (status != 200) { if (status != 200)
error_parser = std::make_unique<QobuzErrorParser>(status, headers); return std::make_unique<QobuzErrorParser>(status, headers);
return;
}
auto i = headers.find("content-type"); auto i = headers.find("content-type");
if (i == headers.end() || i->second.find("/json") == i->second.npos) if (i == headers.end() || i->second.find("/json") == i->second.npos)
throw std::runtime_error("Not a JSON response from Qobuz"); throw std::runtime_error("Not a JSON response from Qobuz");
parser = {&parse_callbacks, nullptr, this}; return std::make_unique<ResponseParser>();
} }
void void
QobuzLoginRequest::OnData(ConstBuffer<void> data) QobuzLoginRequest::FinishParser(std::unique_ptr<CurlResponseParser> p)
{ {
if (error_parser) { assert(dynamic_cast<ResponseParser *>(p.get()) != nullptr);
error_parser->OnData(data); auto &rp = (ResponseParser &)*p;
return; handler.OnQobuzLoginSuccess(rp.GetSession());
}
parser.Parse((const unsigned char *)data.data, data.size);
}
void
QobuzLoginRequest::OnEnd()
{
if (error_parser) {
error_parser->OnEnd();
return;
}
parser.CompleteParse();
if (session.user_auth_token.empty())
throw std::runtime_error("No user_auth_token in login response");
if (session.device_id.empty())
throw std::runtime_error("No device id in login response");
handler.OnQobuzLoginSuccess(std::move(session));
} }
void void
@ -148,7 +162,7 @@ QobuzLoginRequest::OnError(std::exception_ptr e) noexcept
} }
inline bool inline bool
QobuzLoginRequest::String(StringView value) noexcept QobuzLoginRequest::ResponseParser::String(StringView value) noexcept
{ {
switch (state) { switch (state) {
case State::NONE: case State::NONE:
@ -168,7 +182,7 @@ QobuzLoginRequest::String(StringView value) noexcept
} }
inline bool inline bool
QobuzLoginRequest::StartMap() noexcept QobuzLoginRequest::ResponseParser::StartMap() noexcept
{ {
switch (state) { switch (state) {
case State::NONE: case State::NONE:
@ -187,7 +201,7 @@ QobuzLoginRequest::StartMap() noexcept
} }
inline bool inline bool
QobuzLoginRequest::MapKey(StringView value) noexcept QobuzLoginRequest::ResponseParser::MapKey(StringView value) noexcept
{ {
switch (state) { switch (state) {
case State::NONE: case State::NONE:
@ -217,7 +231,7 @@ QobuzLoginRequest::MapKey(StringView value) noexcept
} }
inline bool inline bool
QobuzLoginRequest::EndMap() noexcept QobuzLoginRequest::ResponseParser::EndMap() noexcept
{ {
switch (state) { switch (state) {
case State::NONE: case State::NONE:

View File

@ -21,17 +21,11 @@
#define QOBUZ_LOGIN_REQUEST_HXX #define QOBUZ_LOGIN_REQUEST_HXX
#include "check.h" #include "check.h"
#include "QobuzSession.hxx" #include "lib/curl/Delegate.hxx"
#include "lib/curl/Handler.hxx"
#include "lib/curl/Request.hxx" #include "lib/curl/Request.hxx"
#include "lib/yajl/Handle.hxx"
#include <exception>
#include <memory>
#include <string>
class CurlRequest; class CurlRequest;
class QobuzErrorParser; struct QobuzSession;
class QobuzLoginHandler { class QobuzLoginHandler {
public: public:
@ -39,29 +33,14 @@ public:
virtual void OnQobuzLoginError(std::exception_ptr error) noexcept = 0; virtual void OnQobuzLoginError(std::exception_ptr error) noexcept = 0;
}; };
class QobuzLoginRequest final : CurlResponseHandler { class QobuzLoginRequest final : DelegateCurlResponseHandler {
CurlRequest request; CurlRequest request;
std::unique_ptr<QobuzErrorParser> error_parser;
Yajl::Handle parser;
enum class State {
NONE,
DEVICE,
DEVICE_ID,
USER_AUTH_TOKEN,
} state = State::NONE;
unsigned map_depth = 0;
QobuzSession session;
std::exception_ptr error;
QobuzLoginHandler &handler; QobuzLoginHandler &handler;
public: public:
class ResponseParser;
QobuzLoginRequest(CurlGlobal &curl, QobuzLoginRequest(CurlGlobal &curl,
const char *base_url, const char *app_id, const char *base_url, const char *app_id,
const char *username, const char *email, const char *username, const char *email,
@ -76,19 +55,13 @@ public:
} }
private: private:
/* virtual methods from CurlResponseHandler */ /* virtual methods from DelegateCurlResponseHandler */
void OnHeaders(unsigned status, std::unique_ptr<CurlResponseParser> MakeParser(unsigned status,
std::multimap<std::string, std::string> &&headers) override; std::multimap<std::string, std::string> &&headers) override;
void OnData(ConstBuffer<void> data) override; void FinishParser(std::unique_ptr<CurlResponseParser> p) override;
void OnEnd() override;
void OnError(std::exception_ptr e) noexcept override;
public: /* virtual methods from CurlResponseHandler */
/* yajl callbacks */ void OnError(std::exception_ptr e) noexcept override;
bool String(StringView value) noexcept;
bool StartMap() noexcept;
bool MapKey(StringView value) noexcept;
bool EndMap() noexcept;
}; };
#endif #endif

View File

@ -24,7 +24,7 @@
#include "lib/yajl/Callbacks.hxx" #include "lib/yajl/Callbacks.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
using Wrapper = Yajl::CallbacksWrapper<QobuzTrackRequest>; using Wrapper = Yajl::CallbacksWrapper<QobuzTrackRequest::ResponseParser>;
static constexpr yajl_callbacks parse_callbacks = { static constexpr yajl_callbacks parse_callbacks = {
nullptr, nullptr,
nullptr, nullptr,
@ -39,6 +39,31 @@ static constexpr yajl_callbacks parse_callbacks = {
nullptr, nullptr,
}; };
class QobuzTrackRequest::ResponseParser final : public YajlResponseParser {
enum class State {
NONE,
URL,
} state = State::NONE;
std::string url;
public:
explicit ResponseParser() noexcept
:YajlResponseParser(&parse_callbacks, nullptr, this) {}
std::string &&GetUrl() {
if (url.empty())
throw std::runtime_error("No url in track response");
return std::move(url);
}
/* yajl callbacks */
bool String(StringView value) noexcept;
bool MapKey(StringView value) noexcept;
bool EndMap() noexcept;
};
static std::string static std::string
MakeTrackUrl(QobuzClient &client, const char *track_id) MakeTrackUrl(QobuzClient &client, const char *track_id)
{ {
@ -68,47 +93,26 @@ QobuzTrackRequest::~QobuzTrackRequest() noexcept
request.StopIndirect(); request.StopIndirect();
} }
void std::unique_ptr<CurlResponseParser>
QobuzTrackRequest::OnHeaders(unsigned status, QobuzTrackRequest::MakeParser(unsigned status,
std::multimap<std::string, std::string> &&headers) std::multimap<std::string, std::string> &&headers)
{ {
if (status != 200) { if (status != 200)
error_parser = std::make_unique<QobuzErrorParser>(status, headers); return std::make_unique<QobuzErrorParser>(status, headers);
return;
}
auto i = headers.find("content-type"); auto i = headers.find("content-type");
if (i == headers.end() || i->second.find("/json") == i->second.npos) if (i == headers.end() || i->second.find("/json") == i->second.npos)
throw std::runtime_error("Not a JSON response from Qobuz"); throw std::runtime_error("Not a JSON response from Qobuz");
parser = {&parse_callbacks, nullptr, this}; return std::make_unique<ResponseParser>();
} }
void void
QobuzTrackRequest::OnData(ConstBuffer<void> data) QobuzTrackRequest::FinishParser(std::unique_ptr<CurlResponseParser> p)
{ {
if (error_parser) { assert(dynamic_cast<ResponseParser *>(p.get()) != nullptr);
error_parser->OnData(data); auto &rp = (ResponseParser &)*p;
return; handler.OnQobuzTrackSuccess(rp.GetUrl());
}
parser.Parse((const unsigned char *)data.data, data.size);
}
void
QobuzTrackRequest::OnEnd()
{
if (error_parser) {
error_parser->OnEnd();
return;
}
parser.CompleteParse();
if (url.empty())
throw std::runtime_error("No url in track response");
handler.OnQobuzTrackSuccess(std::move(url));
} }
void void
@ -118,7 +122,7 @@ QobuzTrackRequest::OnError(std::exception_ptr e) noexcept
} }
inline bool inline bool
QobuzTrackRequest::String(StringView value) noexcept QobuzTrackRequest::ResponseParser::String(StringView value) noexcept
{ {
switch (state) { switch (state) {
case State::NONE: case State::NONE:
@ -133,7 +137,7 @@ QobuzTrackRequest::String(StringView value) noexcept
} }
inline bool inline bool
QobuzTrackRequest::MapKey(StringView value) noexcept QobuzTrackRequest::ResponseParser::MapKey(StringView value) noexcept
{ {
if (value.Equals("url")) if (value.Equals("url"))
state = State::URL; state = State::URL;
@ -144,7 +148,7 @@ QobuzTrackRequest::MapKey(StringView value) noexcept
} }
inline bool inline bool
QobuzTrackRequest::EndMap() noexcept QobuzTrackRequest::ResponseParser::EndMap() noexcept
{ {
state = State::NONE; state = State::NONE;

View File

@ -21,17 +21,11 @@
#define QOBUZ_TRACK_REQUEST_HXX #define QOBUZ_TRACK_REQUEST_HXX
#include "check.h" #include "check.h"
#include "lib/curl/Handler.hxx" #include "lib/curl/Delegate.hxx"
#include "lib/curl/Slist.hxx" #include "lib/curl/Slist.hxx"
#include "lib/curl/Request.hxx" #include "lib/curl/Request.hxx"
#include "lib/yajl/Handle.hxx"
#include <exception>
#include <memory>
#include <string>
class QobuzClient; class QobuzClient;
class QobuzErrorParser;
struct QobuzSession; struct QobuzSession;
class QobuzTrackHandler class QobuzTrackHandler
@ -42,27 +36,16 @@ public:
virtual void OnQobuzTrackError(std::exception_ptr error) noexcept = 0; virtual void OnQobuzTrackError(std::exception_ptr error) noexcept = 0;
}; };
class QobuzTrackRequest final : CurlResponseHandler { class QobuzTrackRequest final : DelegateCurlResponseHandler {
CurlSlist request_headers; CurlSlist request_headers;
CurlRequest request; CurlRequest request;
std::unique_ptr<QobuzErrorParser> error_parser;
Yajl::Handle parser;
enum class State {
NONE,
URL,
} state = State::NONE;
std::string url;
std::exception_ptr error;
QobuzTrackHandler &handler; QobuzTrackHandler &handler;
public: public:
class ResponseParser;
QobuzTrackRequest(QobuzClient &client, const QobuzSession &session, QobuzTrackRequest(QobuzClient &client, const QobuzSession &session,
const char *track_id, const char *track_id,
QobuzTrackHandler &_handler) noexcept; QobuzTrackHandler &_handler) noexcept;
@ -74,11 +57,12 @@ public:
} }
private: private:
/* virtual methods from DelegateCurlResponseHandler */
std::unique_ptr<CurlResponseParser> MakeParser(unsigned status,
std::multimap<std::string, std::string> &&headers) override;
void FinishParser(std::unique_ptr<CurlResponseParser> p) override;
/* virtual methods from CurlResponseHandler */ /* virtual methods from CurlResponseHandler */
void OnHeaders(unsigned status,
std::multimap<std::string, std::string> &&headers) override;
void OnData(ConstBuffer<void> data) override;
void OnEnd() override;
void OnError(std::exception_ptr e) noexcept override; void OnError(std::exception_ptr e) noexcept override;
public: public: