Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(net): evento api #26

Merged
merged 15 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 174 additions & 33 deletions src/Infrastructure/Network/NetworkClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <boost/url/params_view.hpp>
#include <memory>
#include <nlohmann/json.hpp>
#include <string>

namespace evento {

Expand Down Expand Up @@ -57,77 +58,217 @@ Task<Result<UserInfoEntity>> NetworkClient::getUserInfo() {

co_return Ok(entity);
}
Task<Result<void>> NetworkClient::refreshAccessToken(std::string const& refreshToken) {
// TODO: implement me!
co_return Error(Error::Unknown);
Task<Result<std::string>> NetworkClient::refreshAccessToken(std::string const& refreshToken) {
auto result = co_await this->request<api::Evento>(http::verb::post,
endpoint("/refresh-token"),
{{"refreshtoken", refreshToken}});
if (result.isErr())
co_return Err(result.unwrapErr());

auto unwarpresult = result.unwrap();
std::string token = unwarpresult["data"]["accessToken"].get<std::string>();

co_return Ok(token);
}
Serein207 marked this conversation as resolved.
Show resolved Hide resolved

Task<Result<EventEntityList>> NetworkClient::getActiveEventList() {
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/v2/client/event/active"));
if (result.isErr())
co_return Err(result.unwrapErr());

EventEntityList entity;
try {
nlohmann::from_json(result.unwrap(), entity);
} catch (const nlohmann::json::exception& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return Ok(entity);
}

Task<Result<EventEntityList>> NetworkClient::getLatestEventList() {
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/v2/client/event/latest"));
if (result.isErr())
co_return Err(result.unwrapErr());

EventEntityList entity;
try {
nlohmann::from_json(result.unwrap(), entity);
} catch (const nlohmann::json::exception& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return Ok(entity);
}

Task<Result<EventEntityList>> NetworkClient::getHistoryEventList(int page, int size) {
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/v2/client/event/history",
{{"page", std::to_string(page)},
{"size", std::to_string(size)}}));
if (result.isErr())
co_return Err(result.unwrapErr());

EventEntityList entity;
try {
nlohmann::from_json(result.unwrap(), entity);
} catch (const nlohmann::json::exception& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return Ok(entity);
}

Task<Result<AttachmentEntity>> NetworkClient::getAttachment(int eventId) {
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this->request<api::Evento>(
http::verb::get, endpoint(std::format("api/v2/client/event/{}/attachments", eventId)));
if (result.isErr())
co_return Err(result.unwrapErr());

AttachmentEntity entity;
try {
nlohmann::from_json(result.unwrap(), entity);
} catch (const nlohmann::json::exception& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return Ok(entity);
}

Task<Result<FeedbackEntity>> NetworkClient::getUserFeedback(int eventId) {
Serein207 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this
->request<api::Evento>(http::verb::get,
endpoint(std::format("/v2/client/event/{}/feedback",
eventId)));
if (result.isErr())
co_return Err(result.unwrapErr());

FeedbackEntity entity;
Serein207 marked this conversation as resolved.
Show resolved Hide resolved
try {
nlohmann::from_json(result.unwrap(), entity);
} catch (const nlohmann::json::exception& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return Ok(entity);
}

Task<Result<void>> NetworkClient::addUserFeedback(int eventId,
Task<Result<bool>> NetworkClient::addUserFeedback(int eventId,
int rating,
std::string const& content) {
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this->request<api::Evento>(
http::verb::post,
endpoint(std::format("/v2/client/event/{}/feedback", eventId),
{{"rating", std::to_string(rating)}, {"content", content}}));
if (result.isErr())
co_return Err(result.unwrapErr());

co_return Ok(true);
}

Task<Result<void>> NetworkClient::checkInEvent(int eventId, std::string const& code) {
// TODO: implement me!
co_return Error(Error::Unknown);
Task<Result<bool>> NetworkClient::checkInEvent(int eventId, std::string const& code) {
auto result = co_await this->request<api::Evento>(
http::verb::post,
endpoint(std::format("/v2/client/event/{}/check-in", eventId), {{"code", code}}));
if (result.isErr())
co_return Err(result.unwrapErr());

co_return Ok(true);
}

Task<Result<void>> NetworkClient::subscribeEvent(int eventId, bool subscribe) {
// TODO: implement me!
co_return Error(Error::Unknown);
Task<Result<bool>> NetworkClient::subscribeEvent(int eventId, bool subscribe) {
std::string subscribeStr = subscribe ? "true" : "false";
auto result = co_await this
->request<api::Evento>(http::verb::post,
endpoint(std::format("/v2/client/event/{}/subscribe",
eventId),
{{"subscribe", subscribeStr}}));
if (result.isErr())
co_return Err(result.unwrapErr());

co_return Ok(true);
}
Serein207 marked this conversation as resolved.
Show resolved Hide resolved

Task<Result<void>> NetworkClient::subscribeDepartment(std::string const& larkDepartment,
Task<Result<bool>> NetworkClient::subscribeDepartment(std::string const& larkDepartment,
bool subscribe) {
// TODO: implement me!
co_return Error(Error::Unknown);
std::string subscribeStr = subscribe ? "true" : "false";
auto result = co_await this
->request<api::Evento>(http::verb::post,
endpoint(std::format("/v2/client/event/{}/subscribe",
larkDepartment),
{{"subscribe", subscribeStr}}));
if (result.isErr())
co_return Err(result.unwrapErr());

co_return Ok(true);
}

Task<Result<EventEntityList>> NetworkClient::getParticipatedEvent() {
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/v2/client/event/participated"));
if (result.isErr())
co_return Err(result.unwrapErr());

EventEntityList entity;
try {
nlohmann::from_json(result.unwrap(), entity);
} catch (const nlohmann::json::exception& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return Ok(entity);
}

Task<Result<EventEntityList>> NetworkClient::getSubscribedEvent() {
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/v2/client/event/subscribed"));
if (result.isErr())
co_return Err(result.unwrapErr());

EventEntityList entity;
try {
nlohmann::from_json(result.unwrap(), entity);
} catch (const nlohmann::json::exception& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return Ok(entity);
}

Task<Result<SlideEntityList>> NetworkClient::getHomeSlide() {
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/v2/client/event/slide"));
if (result.isErr())
co_return Err(result.unwrapErr());

SlideEntityList entity;
try {
nlohmann::from_json(result.unwrap(), entity);
} catch (const nlohmann::json::exception& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return Ok(entity);
}

Task<Result<SlideEntityList>> NetworkClient::getEventSlide(int eventId) {
// TODO: implement me!
co_return Error(Error::Unknown);
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint(
std::format("/v2/client/event/{}/slide",
eventId)));
if (result.isErr())
co_return Err(result.unwrapErr());

SlideEntityList entity;
try {
nlohmann::from_json(result.unwrap(), entity);
} catch (const nlohmann::json::exception& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return Ok(entity);
}

urls::url NetworkClient::endpoint(std::string_view endpoint) {
Expand Down
10 changes: 5 additions & 5 deletions src/Infrastructure/Network/NetworkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class NetworkClient {

Task<Result<UserInfoEntity>> getUserInfo();

Task<Result<void>> refreshAccessToken(std::string const& refreshToken);
Task<Result<std::string>> refreshAccessToken(std::string const& refreshToken);

Task<Result<EventEntityList>> getActiveEventList();

Expand All @@ -49,13 +49,13 @@ class NetworkClient {

Task<Result<FeedbackEntity>> getUserFeedback(int eventId);
Serein207 marked this conversation as resolved.
Show resolved Hide resolved

Task<Result<void>> addUserFeedback(int eventId, int rating, std::string const& content);
Task<Result<bool>> addUserFeedback(int eventId, int rating, std::string const& content);

Task<Result<void>> checkInEvent(int eventId, std::string const& code);
Task<Result<bool>> checkInEvent(int eventId, std::string const& code);

Task<Result<void>> subscribeEvent(int eventId, bool subscribe);
Task<Result<bool>> subscribeEvent(int eventId, bool subscribe);

Task<Result<void>> subscribeDepartment(std::string const& larkDepartment, bool subscribe);
Task<Result<bool>> subscribeDepartment(std::string const& larkDepartment, bool subscribe);

Task<Result<EventEntityList>> getParticipatedEvent();

Expand Down