Skip to content

Commit 4b02b67

Browse files
committed
refactor: break up HandleFDv2PollResponse into separate functions
1 parent 112c78e commit 4b02b67

4 files changed

Lines changed: 97 additions & 92 deletions

File tree

libs/server-sdk/src/data_systems/fdv2/fdv2_polling_impl.cpp

Lines changed: 94 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,93 @@ network::HttpRequest MakeFDv2PollRequest(
5555
network::HttpRequest::BodyType{}};
5656
}
5757

58+
static FDv2SourceResult ParseFDv2PollEvents(
59+
boost::json::array const& events,
60+
FDv2ProtocolHandler* protocol_handler) {
61+
for (auto const& event_val : events) {
62+
auto const* event_obj = event_val.if_object();
63+
if (!event_obj) {
64+
continue;
65+
}
66+
67+
auto const* event_type_val = event_obj->if_contains("event");
68+
auto const* event_data_val = event_obj->if_contains("data");
69+
if (!event_type_val || !event_data_val) {
70+
continue;
71+
}
72+
73+
auto const* event_type_str = event_type_val->if_string();
74+
if (!event_type_str) {
75+
continue;
76+
}
77+
78+
auto result = protocol_handler->HandleEvent(
79+
std::string_view{event_type_str->data(), event_type_str->size()},
80+
*event_data_val);
81+
82+
if (auto* changeset = std::get_if<data_model::FDv2ChangeSet>(&result)) {
83+
return FDv2SourceResult{
84+
FDv2SourceResult::ChangeSet{std::move(*changeset), false}};
85+
}
86+
if (auto* goodbye = std::get_if<Goodbye>(&result)) {
87+
return FDv2SourceResult{
88+
FDv2SourceResult::Goodbye{goodbye->reason, false}};
89+
}
90+
if (auto* error = std::get_if<FDv2ProtocolHandler::Error>(&result)) {
91+
if (error->kind == FDv2ProtocolHandler::Error::Kind::kServerError) {
92+
auto const& id = error->server_error.value().id;
93+
std::string msg =
94+
"An issue was encountered receiving updates for "
95+
"payload '" +
96+
id.value_or("") + "' with reason: '" + error->message +
97+
"'. Automatic retry will occur.";
98+
return FDv2SourceResult{FDv2SourceResult::Interrupted{
99+
MakeError(ErrorKind::kErrorResponse, 0, std::move(msg)),
100+
false}};
101+
}
102+
return FDv2SourceResult{FDv2SourceResult::Interrupted{
103+
MakeError(ErrorKind::kInvalidData, 0, error->message), false}};
104+
}
105+
}
106+
107+
return FDv2SourceResult{FDv2SourceResult::Interrupted{
108+
MakeError(ErrorKind::kInvalidData, 0, kErrorIncompletePayload), false}};
109+
}
110+
111+
static FDv2SourceResult ParseFDv2PollResponse(
112+
std::string const& body,
113+
FDv2ProtocolHandler* protocol_handler) {
114+
boost::system::error_code ec;
115+
auto parsed = boost::json::parse(body, ec);
116+
if (ec) {
117+
return FDv2SourceResult{FDv2SourceResult::Interrupted{
118+
MakeError(ErrorKind::kInvalidData, 0, kErrorParsingBody), false}};
119+
}
120+
121+
auto const* obj = parsed.if_object();
122+
if (!obj) {
123+
return FDv2SourceResult{FDv2SourceResult::Interrupted{
124+
MakeError(ErrorKind::kInvalidData, 0, kErrorParsingBody), false}};
125+
}
126+
127+
auto const* events_val = obj->if_contains("events");
128+
if (!events_val) {
129+
return FDv2SourceResult{FDv2SourceResult::Interrupted{
130+
MakeError(ErrorKind::kInvalidData, 0, kErrorMissingEvents), false}};
131+
}
132+
133+
auto const* events_arr = events_val->if_array();
134+
if (!events_arr) {
135+
return FDv2SourceResult{FDv2SourceResult::Interrupted{
136+
MakeError(ErrorKind::kInvalidData, 0, kErrorMissingEvents), false}};
137+
}
138+
139+
return ParseFDv2PollEvents(*events_arr, protocol_handler);
140+
}
141+
58142
data_interfaces::FDv2SourceResult HandleFDv2PollResponse(
59143
network::HttpResult const& res,
60-
FDv2ProtocolHandler& protocol_handler,
144+
FDv2ProtocolHandler* protocol_handler,
61145
Logger const& logger,
62146
std::string_view identity) {
63147
if (res.IsError()) {
@@ -85,97 +169,18 @@ data_interfaces::FDv2SourceResult HandleFDv2PollResponse(
85169
false}};
86170
}
87171

88-
boost::system::error_code ec;
89-
auto parsed = boost::json::parse(*body, ec);
90-
if (ec) {
91-
LD_LOG(logger, LogLevel::kError) << kErrorParsingBody;
92-
return FDv2SourceResult{FDv2SourceResult::Interrupted{
93-
MakeError(ErrorKind::kInvalidData, 0, kErrorParsingBody),
94-
false}};
95-
}
96-
97-
auto const* obj = parsed.if_object();
98-
if (!obj) {
99-
LD_LOG(logger, LogLevel::kError) << kErrorParsingBody;
100-
return FDv2SourceResult{FDv2SourceResult::Interrupted{
101-
MakeError(ErrorKind::kInvalidData, 0, kErrorParsingBody),
102-
false}};
103-
}
104-
105-
auto const* events_val = obj->if_contains("events");
106-
if (!events_val) {
107-
LD_LOG(logger, LogLevel::kError) << kErrorMissingEvents;
108-
return FDv2SourceResult{FDv2SourceResult::Interrupted{
109-
MakeError(ErrorKind::kInvalidData, 0, kErrorMissingEvents),
110-
false}};
111-
}
112-
113-
auto const* events_arr = events_val->if_array();
114-
if (!events_arr) {
115-
LD_LOG(logger, LogLevel::kError) << kErrorMissingEvents;
116-
return FDv2SourceResult{FDv2SourceResult::Interrupted{
117-
MakeError(ErrorKind::kInvalidData, 0, kErrorMissingEvents),
118-
false}};
119-
}
120-
121-
for (auto const& event_val : *events_arr) {
122-
auto const* event_obj = event_val.if_object();
123-
if (!event_obj) {
124-
continue;
125-
}
126-
127-
auto const* event_type_val = event_obj->if_contains("event");
128-
auto const* event_data_val = event_obj->if_contains("data");
129-
if (!event_type_val || !event_data_val) {
130-
continue;
131-
}
132-
133-
auto const* event_type_str = event_type_val->if_string();
134-
if (!event_type_str) {
135-
continue;
136-
}
137-
138-
auto result = protocol_handler.HandleEvent(
139-
std::string_view{event_type_str->data(),
140-
event_type_str->size()},
141-
*event_data_val);
142-
143-
if (auto* changeset =
144-
std::get_if<data_model::FDv2ChangeSet>(&result)) {
145-
return FDv2SourceResult{
146-
FDv2SourceResult::ChangeSet{std::move(*changeset), false}};
147-
}
148-
if (auto* goodbye = std::get_if<Goodbye>(&result)) {
149-
return FDv2SourceResult{
150-
FDv2SourceResult::Goodbye{goodbye->reason, false}};
151-
}
152-
if (auto* error =
153-
std::get_if<FDv2ProtocolHandler::Error>(&result)) {
154-
if (error->kind ==
155-
FDv2ProtocolHandler::Error::Kind::kServerError) {
156-
auto const& id = error->server_error.value().id;
157-
std::string msg =
158-
"An issue was encountered receiving updates for "
159-
"payload '" +
160-
id.value_or("") + "' with reason: '" + error->message +
161-
"'. Automatic retry will occur.";
162-
LD_LOG(logger, LogLevel::kInfo) << identity << ": " << msg;
163-
return FDv2SourceResult{FDv2SourceResult::Interrupted{
164-
MakeError(ErrorKind::kErrorResponse, 0, std::move(msg)),
165-
false}};
166-
}
172+
auto result = ParseFDv2PollResponse(*body, protocol_handler);
173+
if (auto* interrupted =
174+
std::get_if<FDv2SourceResult::Interrupted>(&result.value)) {
175+
if (interrupted->error.Kind() == ErrorKind::kErrorResponse) {
176+
LD_LOG(logger, LogLevel::kInfo)
177+
<< identity << ": " << interrupted->error.Message();
178+
} else {
167179
LD_LOG(logger, LogLevel::kError)
168-
<< identity << ": " << error->message;
169-
return FDv2SourceResult{FDv2SourceResult::Interrupted{
170-
MakeError(ErrorKind::kInvalidData, 0, error->message),
171-
false}};
180+
<< identity << ": " << interrupted->error.Message();
172181
}
173182
}
174-
175-
LD_LOG(logger, LogLevel::kError) << kErrorIncompletePayload;
176-
return FDv2SourceResult{FDv2SourceResult::Interrupted{
177-
MakeError(ErrorKind::kInvalidData, 0, kErrorIncompletePayload),
178-
false}};
183+
return result;
179184
}
180185

181186
if (network::IsRecoverableStatus(res.Status())) {

libs/server-sdk/src/data_systems/fdv2/fdv2_polling_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ network::HttpRequest MakeFDv2PollRequest(
2626
// to identify the caller (e.g. "FDv2 polling initializer").
2727
data_interfaces::FDv2SourceResult HandleFDv2PollResponse(
2828
network::HttpResult const& res,
29-
FDv2ProtocolHandler& protocol_handler,
29+
FDv2ProtocolHandler* protocol_handler,
3030
Logger const& logger,
3131
std::string_view identity);
3232

libs/server-sdk/src/data_systems/fdv2/polling_initializer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ FDv2SourceResult FDv2PollingInitializer::HandlePollResult(
7575
std::shared_ptr<State> state,
7676
network::HttpResult const& res) {
7777
FDv2ProtocolHandler protocol_handler;
78-
return HandleFDv2PollResponse(res, protocol_handler, state->logger,
78+
return HandleFDv2PollResponse(res, &protocol_handler, state->logger,
7979
kIdentity);
8080
}
8181

libs/server-sdk/src/data_systems/fdv2/polling_synchronizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async::Future<network::HttpResult> FDv2PollingSynchronizer::State::Request(
5050
FDv2SourceResult FDv2PollingSynchronizer::State::HandlePollResult(
5151
network::HttpResult const& res) {
5252
FDv2ProtocolHandler protocol_handler;
53-
return HandleFDv2PollResponse(res, protocol_handler, logger_, kIdentity);
53+
return HandleFDv2PollResponse(res, &protocol_handler, logger_, kIdentity);
5454
}
5555

5656
async::Future<bool> FDv2PollingSynchronizer::State::Delay(

0 commit comments

Comments
 (0)