lib/curl/Delegate: helper class for response body parsers
Eliminate duplicate code.
This commit is contained in:
@@ -40,24 +40,18 @@ static constexpr yajl_callbacks tidal_error_parser_callbacks = {
|
||||
|
||||
TidalErrorParser::TidalErrorParser(unsigned _status,
|
||||
const std::multimap<std::string, std::string> &headers)
|
||||
:status(_status),
|
||||
parser(&tidal_error_parser_callbacks, nullptr, this)
|
||||
:YajlResponseParser(&tidal_error_parser_callbacks, nullptr, this),
|
||||
status(_status)
|
||||
{
|
||||
auto i = headers.find("content-type");
|
||||
if (i == headers.end() || i->second.find("/json") == i->second.npos)
|
||||
throw FormatRuntimeError("Status %u from Tidal", status);
|
||||
}
|
||||
|
||||
void
|
||||
TidalErrorParser::OnData(ConstBuffer<void> data)
|
||||
{
|
||||
parser.Parse((const unsigned char *)data.data, data.size);
|
||||
}
|
||||
|
||||
void
|
||||
TidalErrorParser::OnEnd()
|
||||
{
|
||||
parser.CompleteParse();
|
||||
YajlResponseParser::OnEnd();
|
||||
|
||||
if (!message.empty())
|
||||
throw FormatRuntimeError("Error from Tidal: %s",
|
||||
|
||||
@@ -21,9 +21,8 @@
|
||||
#define TIDAL_ERROR_PARSER_HXX
|
||||
|
||||
#include "check.h"
|
||||
#include "lib/yajl/Handle.hxx"
|
||||
#include "lib/yajl/ResponseParser.hxx"
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
@@ -33,11 +32,9 @@ struct StringView;
|
||||
/**
|
||||
* Parse an error JSON response.
|
||||
*/
|
||||
class TidalErrorParser {
|
||||
class TidalErrorParser final : public YajlResponseParser {
|
||||
const unsigned status;
|
||||
|
||||
Yajl::Handle parser;
|
||||
|
||||
enum class State {
|
||||
NONE,
|
||||
USER_MESSAGE,
|
||||
@@ -53,16 +50,9 @@ public:
|
||||
TidalErrorParser(unsigned status,
|
||||
const std::multimap<std::string, std::string> &headers);
|
||||
|
||||
/**
|
||||
* Feed response body data into the JSON parser.
|
||||
*/
|
||||
void OnData(ConstBuffer<void> data);
|
||||
|
||||
/**
|
||||
* Throw an exception describing the error condition. Call
|
||||
* this at the end of the response body.
|
||||
*/
|
||||
void OnEnd();
|
||||
protected:
|
||||
/* virtual methods from CurlResponseParser */
|
||||
void OnEnd() override;
|
||||
|
||||
public:
|
||||
/* yajl callbacks */
|
||||
|
||||
@@ -21,10 +21,12 @@
|
||||
#include "TidalLoginRequest.hxx"
|
||||
#include "TidalErrorParser.hxx"
|
||||
#include "lib/curl/Form.hxx"
|
||||
#include "lib/yajl/Handle.hxx"
|
||||
#include "lib/yajl/Callbacks.hxx"
|
||||
#include "lib/yajl/ResponseParser.hxx"
|
||||
#include "util/RuntimeError.hxx"
|
||||
|
||||
using Wrapper = Yajl::CallbacksWrapper<TidalLoginRequest>;
|
||||
using Wrapper = Yajl::CallbacksWrapper<TidalLoginRequest::ResponseParser>;
|
||||
static constexpr yajl_callbacks parse_callbacks = {
|
||||
nullptr,
|
||||
nullptr,
|
||||
@@ -39,6 +41,31 @@ static constexpr yajl_callbacks parse_callbacks = {
|
||||
nullptr,
|
||||
};
|
||||
|
||||
class TidalLoginRequest::ResponseParser final : public YajlResponseParser {
|
||||
enum class State {
|
||||
NONE,
|
||||
SESSION_ID,
|
||||
} state = State::NONE;
|
||||
|
||||
std::string session;
|
||||
|
||||
public:
|
||||
explicit ResponseParser() noexcept
|
||||
:YajlResponseParser(&parse_callbacks, nullptr, this) {}
|
||||
|
||||
std::string &&GetSession() {
|
||||
if (session.empty())
|
||||
throw std::runtime_error("No sessionId in login response");
|
||||
|
||||
return std::move(session);
|
||||
}
|
||||
|
||||
/* yajl callbacks */
|
||||
bool String(StringView value) noexcept;
|
||||
bool MapKey(StringView value) noexcept;
|
||||
bool EndMap() noexcept;
|
||||
};
|
||||
|
||||
static std::string
|
||||
MakeLoginUrl(const char *base_url)
|
||||
{
|
||||
@@ -66,47 +93,26 @@ TidalLoginRequest::~TidalLoginRequest() noexcept
|
||||
request.StopIndirect();
|
||||
}
|
||||
|
||||
void
|
||||
TidalLoginRequest::OnHeaders(unsigned status,
|
||||
std::multimap<std::string, std::string> &&headers)
|
||||
std::unique_ptr<CurlResponseParser>
|
||||
TidalLoginRequest::MakeParser(unsigned status,
|
||||
std::multimap<std::string, std::string> &&headers)
|
||||
{
|
||||
if (status != 200) {
|
||||
error_parser = std::make_unique<TidalErrorParser>(status, headers);
|
||||
return;
|
||||
}
|
||||
if (status != 200)
|
||||
return std::make_unique<TidalErrorParser>(status, headers);
|
||||
|
||||
auto i = headers.find("content-type");
|
||||
if (i == headers.end() || i->second.find("/json") == i->second.npos)
|
||||
throw std::runtime_error("Not a JSON response from Tidal");
|
||||
|
||||
parser = {&parse_callbacks, nullptr, this};
|
||||
return std::make_unique<ResponseParser>();
|
||||
}
|
||||
|
||||
void
|
||||
TidalLoginRequest::OnData(ConstBuffer<void> data)
|
||||
TidalLoginRequest::FinishParser(std::unique_ptr<CurlResponseParser> p)
|
||||
{
|
||||
if (error_parser) {
|
||||
error_parser->OnData(data);
|
||||
return;
|
||||
}
|
||||
|
||||
parser.Parse((const unsigned char *)data.data, data.size);
|
||||
}
|
||||
|
||||
void
|
||||
TidalLoginRequest::OnEnd()
|
||||
{
|
||||
if (error_parser) {
|
||||
error_parser->OnEnd();
|
||||
return;
|
||||
}
|
||||
|
||||
parser.CompleteParse();
|
||||
|
||||
if (session.empty())
|
||||
throw std::runtime_error("No sessionId in login response");
|
||||
|
||||
handler.OnTidalLoginSuccess(std::move(session));
|
||||
assert(dynamic_cast<ResponseParser *>(p.get()) != nullptr);
|
||||
auto &rp = (ResponseParser &)*p;
|
||||
handler.OnTidalLoginSuccess(rp.GetSession());
|
||||
}
|
||||
|
||||
void
|
||||
@@ -116,7 +122,7 @@ TidalLoginRequest::OnError(std::exception_ptr e) noexcept
|
||||
}
|
||||
|
||||
inline bool
|
||||
TidalLoginRequest::String(StringView value) noexcept
|
||||
TidalLoginRequest::ResponseParser::String(StringView value) noexcept
|
||||
{
|
||||
switch (state) {
|
||||
case State::NONE:
|
||||
@@ -131,7 +137,7 @@ TidalLoginRequest::String(StringView value) noexcept
|
||||
}
|
||||
|
||||
inline bool
|
||||
TidalLoginRequest::MapKey(StringView value) noexcept
|
||||
TidalLoginRequest::ResponseParser::MapKey(StringView value) noexcept
|
||||
{
|
||||
if (value.Equals("sessionId"))
|
||||
state = State::SESSION_ID;
|
||||
@@ -142,7 +148,7 @@ TidalLoginRequest::MapKey(StringView value) noexcept
|
||||
}
|
||||
|
||||
inline bool
|
||||
TidalLoginRequest::EndMap() noexcept
|
||||
TidalLoginRequest::ResponseParser::EndMap() noexcept
|
||||
{
|
||||
state = State::NONE;
|
||||
|
||||
|
||||
@@ -21,17 +21,9 @@
|
||||
#define TIDAL_LOGIN_REQUEST_HXX
|
||||
|
||||
#include "check.h"
|
||||
#include "lib/curl/Handler.hxx"
|
||||
#include "lib/curl/Delegate.hxx"
|
||||
#include "lib/curl/Slist.hxx"
|
||||
#include "lib/curl/Request.hxx"
|
||||
#include "lib/yajl/Handle.hxx"
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class CurlRequest;
|
||||
class TidalErrorParser;
|
||||
|
||||
/**
|
||||
* Callback class for #TidalLoginRequest.
|
||||
@@ -49,27 +41,16 @@ public:
|
||||
*
|
||||
* After construction, call Start() to initiate the request.
|
||||
*/
|
||||
class TidalLoginRequest final : CurlResponseHandler {
|
||||
class TidalLoginRequest final : DelegateCurlResponseHandler {
|
||||
CurlSlist request_headers;
|
||||
|
||||
CurlRequest request;
|
||||
|
||||
std::unique_ptr<TidalErrorParser> error_parser;
|
||||
|
||||
Yajl::Handle parser;
|
||||
|
||||
enum class State {
|
||||
NONE,
|
||||
SESSION_ID,
|
||||
} state = State::NONE;
|
||||
|
||||
std::string session;
|
||||
|
||||
std::exception_ptr error;
|
||||
|
||||
TidalLoginHandler &handler;
|
||||
|
||||
public:
|
||||
class ResponseParser;
|
||||
|
||||
TidalLoginRequest(CurlGlobal &curl,
|
||||
const char *base_url, const char *token,
|
||||
const char *username, const char *password,
|
||||
@@ -82,18 +63,13 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
/* 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;
|
||||
/* 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;
|
||||
|
||||
public:
|
||||
/* yajl callbacks */
|
||||
bool String(StringView value) noexcept;
|
||||
bool MapKey(StringView value) noexcept;
|
||||
bool EndMap() noexcept;
|
||||
/* virtual methods from CurlResponseHandler */
|
||||
void OnError(std::exception_ptr e) noexcept override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#include "lib/yajl/Callbacks.hxx"
|
||||
#include "util/RuntimeError.hxx"
|
||||
|
||||
using Wrapper = Yajl::CallbacksWrapper<TidalTrackRequest>;
|
||||
using Wrapper = Yajl::CallbacksWrapper<TidalTrackRequest::ResponseParser>;
|
||||
static constexpr yajl_callbacks parse_callbacks = {
|
||||
nullptr,
|
||||
nullptr,
|
||||
@@ -38,6 +38,31 @@ static constexpr yajl_callbacks parse_callbacks = {
|
||||
nullptr,
|
||||
};
|
||||
|
||||
class TidalTrackRequest::ResponseParser final : public YajlResponseParser {
|
||||
enum class State {
|
||||
NONE,
|
||||
URLS,
|
||||
} 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
|
||||
MakeTrackUrl(const char *base_url, const char *track_id)
|
||||
{
|
||||
@@ -68,47 +93,26 @@ TidalTrackRequest::~TidalTrackRequest() noexcept
|
||||
request.StopIndirect();
|
||||
}
|
||||
|
||||
void
|
||||
TidalTrackRequest::OnHeaders(unsigned status,
|
||||
std::multimap<std::string, std::string> &&headers)
|
||||
std::unique_ptr<CurlResponseParser>
|
||||
TidalTrackRequest::MakeParser(unsigned status,
|
||||
std::multimap<std::string, std::string> &&headers)
|
||||
{
|
||||
if (status != 200) {
|
||||
error_parser = std::make_unique<TidalErrorParser>(status, headers);
|
||||
return;
|
||||
}
|
||||
if (status != 200)
|
||||
return std::make_unique<TidalErrorParser>(status, headers);
|
||||
|
||||
auto i = headers.find("content-type");
|
||||
if (i == headers.end() || i->second.find("/json") == i->second.npos)
|
||||
throw std::runtime_error("Not a JSON response from Tidal");
|
||||
|
||||
parser = {&parse_callbacks, nullptr, this};
|
||||
return std::make_unique<ResponseParser>();
|
||||
}
|
||||
|
||||
void
|
||||
TidalTrackRequest::OnData(ConstBuffer<void> data)
|
||||
TidalTrackRequest::FinishParser(std::unique_ptr<CurlResponseParser> p)
|
||||
{
|
||||
if (error_parser) {
|
||||
error_parser->OnData(data);
|
||||
return;
|
||||
}
|
||||
|
||||
parser.Parse((const unsigned char *)data.data, data.size);
|
||||
}
|
||||
|
||||
void
|
||||
TidalTrackRequest::OnEnd()
|
||||
{
|
||||
if (error_parser) {
|
||||
error_parser->OnEnd();
|
||||
return;
|
||||
}
|
||||
|
||||
parser.CompleteParse();
|
||||
|
||||
if (url.empty())
|
||||
throw std::runtime_error("No url in track response");
|
||||
|
||||
handler.OnTidalTrackSuccess(std::move(url));
|
||||
assert(dynamic_cast<ResponseParser *>(p.get()) != nullptr);
|
||||
auto &rp = (ResponseParser &)*p;
|
||||
handler.OnTidalTrackSuccess(rp.GetUrl());
|
||||
}
|
||||
|
||||
void
|
||||
@@ -118,7 +122,7 @@ TidalTrackRequest::OnError(std::exception_ptr e) noexcept
|
||||
}
|
||||
|
||||
inline bool
|
||||
TidalTrackRequest::String(StringView value) noexcept
|
||||
TidalTrackRequest::ResponseParser::String(StringView value) noexcept
|
||||
{
|
||||
switch (state) {
|
||||
case State::NONE:
|
||||
@@ -134,7 +138,7 @@ TidalTrackRequest::String(StringView value) noexcept
|
||||
}
|
||||
|
||||
inline bool
|
||||
TidalTrackRequest::MapKey(StringView value) noexcept
|
||||
TidalTrackRequest::ResponseParser::MapKey(StringView value) noexcept
|
||||
{
|
||||
if (value.Equals("urls"))
|
||||
state = State::URLS;
|
||||
@@ -145,7 +149,7 @@ TidalTrackRequest::MapKey(StringView value) noexcept
|
||||
}
|
||||
|
||||
inline bool
|
||||
TidalTrackRequest::EndMap() noexcept
|
||||
TidalTrackRequest::ResponseParser::EndMap() noexcept
|
||||
{
|
||||
state = State::NONE;
|
||||
|
||||
|
||||
@@ -21,17 +21,9 @@
|
||||
#define TIDAL_TRACK_REQUEST_HXX
|
||||
|
||||
#include "check.h"
|
||||
#include "lib/curl/Handler.hxx"
|
||||
#include "lib/curl/Delegate.hxx"
|
||||
#include "lib/curl/Slist.hxx"
|
||||
#include "lib/curl/Request.hxx"
|
||||
#include "lib/yajl/Handle.hxx"
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class CurlRequest;
|
||||
class TidalErrorParser;
|
||||
|
||||
/**
|
||||
* Callback class for #TidalTrackRequest.
|
||||
@@ -51,27 +43,16 @@ public:
|
||||
*
|
||||
* After construction, call Start() to initiate the request.
|
||||
*/
|
||||
class TidalTrackRequest final : CurlResponseHandler {
|
||||
class TidalTrackRequest final : DelegateCurlResponseHandler {
|
||||
CurlSlist request_headers;
|
||||
|
||||
CurlRequest request;
|
||||
|
||||
std::unique_ptr<TidalErrorParser> error_parser;
|
||||
|
||||
Yajl::Handle parser;
|
||||
|
||||
enum class State {
|
||||
NONE,
|
||||
URLS,
|
||||
} state = State::NONE;
|
||||
|
||||
std::string url;
|
||||
|
||||
std::exception_ptr error;
|
||||
|
||||
TidalTrackHandler &handler;
|
||||
|
||||
public:
|
||||
class ResponseParser;
|
||||
|
||||
TidalTrackRequest(CurlGlobal &curl,
|
||||
const char *base_url, const char *token,
|
||||
const char *session,
|
||||
@@ -85,18 +66,13 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
/* 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;
|
||||
/* 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;
|
||||
|
||||
public:
|
||||
/* yajl callbacks */
|
||||
bool String(StringView value) noexcept;
|
||||
bool MapKey(StringView value) noexcept;
|
||||
bool EndMap() noexcept;
|
||||
/* virtual methods from CurlResponseHandler */
|
||||
void OnError(std::exception_ptr e) noexcept override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user