pixwhile/routes/users/users.cpp

42 lines
1.5 KiB
C++
Raw Normal View History

2023-04-05 16:36:20 +00:00
#include "../routes.h"
#include "../../servehelper.h"
#include "../../pixivclient.h"
#include "common.h"
static inline uint64_t to_ull(const std::string& str);
void users_route(const httplib::Request& req, httplib::Response& res, const Config& config, PixivClient& pixiv_client) {
uint64_t user_id = to_ull(req.matches[1].str());
User user;
try {
user = pixiv_client.get_user(user_id);
} catch (const PixivException& e) {
if (e.status == 404) {
res.status = 404;
serve_error(req, res, config, "404: User not found", e.what());
} else {
res.status = 500;
serve_error(req, res, config, "500: Internal server error", "Failed to fetch user information", e.what());
}
return;
} catch (const std::exception& e) {
res.status = 500;
serve_error(req, res, config, "500: Internal server error", "Failed to fetch user information", e.what());
return;
}
serve(req, res, config, user.display_name + " (@" + user.username + ')', generate_user_header(user, config));
}
static inline uint64_t to_ull(const std::string& str) {
char* endptr;
errno = 0;
uint64_t res = strtoull(str.c_str(), &endptr, 10);
if (res == ULLONG_MAX && errno == ERANGE) {
throw std::overflow_error(str + " is too big");
} else if (endptr[0] != '\0') {
throw std::invalid_argument(str + " contains trailing text or is not an integer");
}
return res;
}