@@ -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+
58142data_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 ())) {
0 commit comments