Try to manage error responses
I can't get the header of the error response because of this https://github.com/jgaa/restc-cpp/issues/102 so I'm just hardcoding some timeout and code will keep trying.
This commit is contained in:
parent
a0bb97e8b7
commit
d66af7779d
2 changed files with 79 additions and 50 deletions
|
@ -16,6 +16,85 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "timer_oro_api.hpp"
|
#include "timer_oro_api.hpp"
|
||||||
|
#include "oro/api.hpp"
|
||||||
|
#include <restc-cpp/error.h>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
namespace duck {
|
namespace duck {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
template <oro::DBOperation Op> auto invoke_api_func (oro::Api& api);
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline auto invoke_api_func<oro::DBOperation::Icons> (oro::Api& api) {
|
||||||
|
return api.items_icons();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline auto invoke_api_func<oro::DBOperation::Items> (oro::Api& api) {
|
||||||
|
return api.items_list();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline auto invoke_api_func<oro::DBOperation::Creators> (oro::Api& api) {
|
||||||
|
return api.fame_list();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline auto invoke_api_func<oro::DBOperation::Shops> (oro::Api& api) {
|
||||||
|
return api.market_list();
|
||||||
|
}
|
||||||
|
} //unnamed namespace
|
||||||
|
|
||||||
|
template<oro::DBOperation Op>
|
||||||
|
inline TimerOroApi<Op>::TimerOroApi (
|
||||||
|
const eve::Context& ctx,
|
||||||
|
const TimerSettings& settings,
|
||||||
|
roar11::ThreadPool* pool,
|
||||||
|
oro::Api* oro_api,
|
||||||
|
oro::OriginsDB* db
|
||||||
|
) :
|
||||||
|
TimerBase(ctx, Op, settings, pool, oro_api, db)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<oro::DBOperation Op>
|
||||||
|
inline void TimerOroApi<Op>::fetch_data() {
|
||||||
|
int status_code = 200;
|
||||||
|
try {
|
||||||
|
auto results = invoke_api_func<Op>(oro_api());
|
||||||
|
set_next_timer(results.first);
|
||||||
|
this->update_db(results.second, results.first);
|
||||||
|
}
|
||||||
|
catch (const restc_cpp::RequestFailedWithErrorException& err) {
|
||||||
|
status_code = err.http_response.status_code;
|
||||||
|
if (429 == err.http_response.status_code) {
|
||||||
|
//server received too many requests, give up for now
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
this->set_exception(std::current_exception());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (429 == status_code) {
|
||||||
|
oro::Header head;
|
||||||
|
head.date.ts = std::chrono::time_point_cast<oro::timestamp_t::duration>(std::chrono::system_clock::now());
|
||||||
|
head.rate_limit = 1;
|
||||||
|
head.rate_limit_remaining = 0;
|
||||||
|
head.rate_limit_reset = 600;
|
||||||
|
head.retry_after = 600;
|
||||||
|
std::string server = "forged_header";
|
||||||
|
set_next_timer(head);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template class TimerOroApi<oro::DBOperation::Shops>;
|
||||||
|
template class TimerOroApi<oro::DBOperation::Icons>;
|
||||||
|
template class TimerOroApi<oro::DBOperation::Items>;
|
||||||
|
template class TimerOroApi<oro::DBOperation::Creators>;
|
||||||
|
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
|
@ -19,8 +19,6 @@
|
||||||
|
|
||||||
#include "timer_base.hpp"
|
#include "timer_base.hpp"
|
||||||
#include "oro/dboperation.hpp"
|
#include "oro/dboperation.hpp"
|
||||||
#include "oro/api.hpp"
|
|
||||||
#include <exception>
|
|
||||||
|
|
||||||
namespace oro {
|
namespace oro {
|
||||||
class Api;
|
class Api;
|
||||||
|
@ -43,53 +41,5 @@ private:
|
||||||
virtual void fetch_data() override;
|
virtual void fetch_data() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
template <oro::DBOperation Op> auto invoke_api_func (oro::Api& api);
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline auto invoke_api_func<oro::DBOperation::Icons> (oro::Api& api) {
|
|
||||||
return api.items_icons();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline auto invoke_api_func<oro::DBOperation::Items> (oro::Api& api) {
|
|
||||||
return api.items_list();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline auto invoke_api_func<oro::DBOperation::Creators> (oro::Api& api) {
|
|
||||||
return api.fame_list();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline auto invoke_api_func<oro::DBOperation::Shops> (oro::Api& api) {
|
|
||||||
return api.market_list();
|
|
||||||
}
|
|
||||||
} //namespace detail
|
|
||||||
|
|
||||||
template<oro::DBOperation Op>
|
|
||||||
inline TimerOroApi<Op>::TimerOroApi (
|
|
||||||
const eve::Context& ctx,
|
|
||||||
const TimerSettings& settings,
|
|
||||||
roar11::ThreadPool* pool,
|
|
||||||
oro::Api* oro_api,
|
|
||||||
oro::OriginsDB* db
|
|
||||||
) :
|
|
||||||
TimerBase(ctx, Op, settings, pool, oro_api, db)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
template<oro::DBOperation Op>
|
|
||||||
inline void TimerOroApi<Op>::fetch_data() {
|
|
||||||
try {
|
|
||||||
auto results = detail::invoke_api_func<Op>(oro_api());
|
|
||||||
set_next_timer(results.first);
|
|
||||||
this->update_db(results.second, results.first);
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
this->set_exception(std::current_exception());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue