Skip to content

Commit

Permalink
feat(network): empty network api functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wyq777x authored and Serein207 committed Aug 7, 2024
1 parent 82524a5 commit 61c3e94
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 37 deletions.
78 changes: 75 additions & 3 deletions src/Infrastructure/Network/NetworkClient.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "NetworkClient.h"
#include "Infrastructure/Network/Api/Evento.hpp"
#include "Infrastructure/Utils/Error.h"
#include <Infrastructure/Utils/Debug.h>
#include <boost/url/param.hpp>
#include <boost/url/params_view.hpp>
Expand Down Expand Up @@ -37,9 +38,8 @@ Task<Result<LoginResEntity>> NetworkClient::loginViaSastLink(const std::string&
co_return Ok(entity);
}

Task<Result<UserInfoEntity>> NetworkClient::getUserInfo(const std::string& userId) {
auto result = co_await this->request<api::Evento>(http::verb::get,
endpoint("/user/info", {{"userId", userId}}));
Task<Result<UserInfoEntity>> NetworkClient::getUserInfo() {
auto result = co_await this->request<api::Evento>(http::verb::get, endpoint("/v2/user/profile"));
if (result.isErr())
co_return Err(result.unwrapErr());

Expand All @@ -52,6 +52,78 @@ Task<Result<UserInfoEntity>> NetworkClient::getUserInfo(const std::string& userI

co_return Ok(entity);
}
Task<Result<std::string>> NetworkClient::refreshAccessToken(std::string const& refreshToken) {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<EventEntityList>> NetworkClient::getActiveEventList() {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<EventEntityList>> NetworkClient::getLatestEventList() {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<EventEntityList>> NetworkClient::getHistoryEventList(int page, int size) {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<AttachmentEntity>> NetworkClient::getAttachment(int eventId) {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<FeedbackEntity>> NetworkClient::getUserFeedback(int eventId) {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<void>> NetworkClient::addUserFeedback(int eventId,
int rating,
std::string const& content) {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<void>> NetworkClient::checkInEvent(int eventId, std::string const& code) {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<void>> NetworkClient::subscribeEvent(int eventId, bool subscribe) {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<void>> NetworkClient::subscribeDepartment(std::string const& larkDepartment,
bool subscribe) {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<EventEntityList>> NetworkClient::getParticipatedEvent() {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<EventEntityList>> NetworkClient::getSubscribedEvent() {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<SlideEntityList>> NetworkClient::getHomeSlide() {
// TODO: implement me!
co_return Error(Error::Unknown);
}

Task<Result<SlideEntityList>> NetworkClient::getEventSlide(int eventId) {
// TODO: implement me!
co_return Error(Error::Unknown);
}

urls::url NetworkClient::endpoint(std::string_view endpoint) {
return urls::url(EVENTO_API_GATEWAY + endpoint.data());
Expand Down
45 changes: 40 additions & 5 deletions src/Infrastructure/Network/NetworkClient.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include "Infrastructure/Utils/Debug.h"
#include "ResponseStruct.h"
#include <Infrastructure/Network/Api/Evento.hpp>
#include <Infrastructure/Network/Api/Github.hpp>
#include <Infrastructure/Network/HttpsAccessManager.h>
#include <Infrastructure/Network/ResponseStruct.h>
#include <Infrastructure/Utils/Debug.h>
#include <Infrastructure/Utils/Result.h>
#include <boost/asio/awaitable.hpp>
#include <boost/beast/http.hpp>
Expand All @@ -23,21 +24,55 @@ namespace net = boost::asio; // from <boost/asio.hpp>
namespace urls = boost::urls; // from <boost/url.hpp>

using JsonResult = Result<nlohmann::basic_json<>>;
using EventEntityList = std::vector<EventEntity>;
using SlideEntityList = std::vector<SlideEntity>;
template<typename T>
using Task = net::awaitable<T>;

class NetworkClient {
public:
NetworkClient(net::ssl::context& ctx);

Task<Result<LoginResEntity>> loginViaSastLink(const std::string& code);
Task<Result<LoginResEntity>> loginViaSastLink(std::string const& code);

Task<Result<UserInfoEntity>> getUserInfo(const std::string& userId);
Task<Result<UserInfoEntity>> getUserInfo();

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

Task<Result<EventEntityList>> getActiveEventList();

Task<Result<EventEntityList>> getLatestEventList();

Task<Result<EventEntityList>> getHistoryEventList(int page, int size = 10);

Task<Result<AttachmentEntity>> getAttachment(int eventId);

Task<Result<FeedbackEntity>> getUserFeedback(int eventId);

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

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

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

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

Task<Result<EventEntityList>> getParticipatedEvent();

Task<Result<EventEntityList>> getSubscribedEvent();

Task<Result<SlideEntityList>> getHomeSlide();

Task<Result<SlideEntityList>> getEventSlide(int eventId);

// access token
// NOTE: `AUTOMATICALLY` added to request header if exists
std::optional<std::string> tokenBytes;

private:
// http request
// - success => return the `data` field from response json
// maybe json object or json array
// - error => return error message
template<std::same_as<api::Evento> Api>
Task<JsonResult> request(http::verb verb,
urls::url_view url,
Expand Down Expand Up @@ -68,7 +103,7 @@ class NetworkClient {
static urls::url endpoint(std::string_view endpoint); // url has no query params
static urls::url endpoint(std::string_view endpoint, // url has query params
std::initializer_list<urls::param> const& queryParams);
// response handler
// response handler for evento backend
static JsonResult handleResponse(http::response<http::dynamic_body> response);

private:
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Network/Response/AttachmentEntity.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// IWYU pragma: private, include "ResponseStructure.h"
// IWYU pragma: private, include "ResponseStruct.h"
#pragma once

#include <nlohmann/json.hpp>
Expand Down
10 changes: 4 additions & 6 deletions src/Infrastructure/Network/Response/EventEntity.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// IWYU pragma: private, include "ResponseStructure.h"
// IWYU pragma: private, include "ResponseStruct.h"
#pragma once

#include "EventState.h"
#include <nlohmann/json.hpp>
#include <string>

namespace evento {

struct EventEntity {
Expand All @@ -14,10 +14,9 @@ struct EventEntity {
std::string end;
std::string location;
std::string tag;
std::string larkMeetingRoomId;
std::string larkMeetingRoomName;
std::string larkDepartmentId;
std::string larkDepartmentName;
State state;
bool isSubscribed; // 已订阅
bool isChecked; // 已签到

Expand All @@ -29,10 +28,9 @@ struct EventEntity {
end,
location,
tag,
larkMeetingRoomId,
larkMeetingRoomName,
larkDepartmentId,
larkDepartmentName,
state,
isSubscribed,
isChecked);
};
Expand Down
16 changes: 0 additions & 16 deletions src/Infrastructure/Network/Response/EventListEntity.h

This file was deleted.

22 changes: 22 additions & 0 deletions src/Infrastructure/Network/Response/EventState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// IWYU pragma: private, include "ResponseStruct.h"
#pragma once

#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>

namespace evento {

enum class State {
SigningUp, // 报名中
Active, // 正在进行
Completed, // 已结束
Cancelled, // 已取消
};

NLOHMANN_JSON_SERIALIZE_ENUM(State,
{{State::SigningUp, "SIGNING_UP"},
{State::Active, "ACTIVE"},
{State::Completed, "COMPLETED"},
{State::Cancelled, "CANCELLED"}})

} // namespace evento
2 changes: 1 addition & 1 deletion src/Infrastructure/Network/Response/FeedbackEntity.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// IWYU pragma: private, include "ResponseStructure.h"
// IWYU pragma: private, include "ResponseStruct.h"
#pragma once

#include "StdOptional.h" // IWYU pragma: keep
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Network/Response/LoginResEntity.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// IWYU pragma: private, include "ResponseStructure.h"
// IWYU pragma: private, include "ResponseStruct.h"
#pragma once

#include "UserInfoEntity.h"
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Network/Response/SlideEntity.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

// IWYU pragma: private, include "ResponseStructure.h"
// IWYU pragma: private, include "ResponseStruct.h"

#include <nlohmann/json.hpp>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Network/Response/StdOptional.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// IWYU pragma: private, include "ResponseStructure.h"
// IWYU pragma: private, include "ResponseStruct.h"
#pragma once

#include <nlohmann/json.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Network/Response/UserInfoEntity.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// IWYU pragma: private, include "ResponseStructure.h"
// IWYU pragma: private, include "ResponseStruct.h"

#pragma once

Expand Down
1 change: 0 additions & 1 deletion src/Infrastructure/Network/ResponseStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "Response/AttachmentEntity.h" // IWYU pragma: export
#include "Response/EventEntity.h" // IWYU pragma: export
#include "Response/EventListEntity.h" // IWYU pragma: export
#include "Response/FeedbackEntity.h" // IWYU pragma: export
#include "Response/LoginResEntity.h" // IWYU pragma: export
#include "Response/SlideEntity.h" // IWYU pragma: export
Expand Down

0 comments on commit 61c3e94

Please sign in to comment.