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): github api #28

Merged
merged 4 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 47 additions & 1 deletion 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.hh>
#include <Infrastructure/Network/Api/Github.hh>
#include <Infrastructure/Utils/Debug.h>
#include <boost/url/param.hpp>
#include <boost/url/params_view.hpp>
Expand All @@ -9,7 +10,7 @@
namespace evento {

static const std::string EVENTO_API_GATEWAY = "https://evento.sast.fun/api";
static const std::string GITHUB_API_GATEWAY = "https://api.github.com/repo";
static const std::string GITHUB_API_GATEWAY = "https://api.github.com/repos";

constexpr const char MIME_JSON[] = "application/json";
constexpr const char MIME_FORM_URL_ENCODED[] = "application/x-www-form-urlencoded";
Expand Down Expand Up @@ -141,6 +142,51 @@ urls::url NetworkClient::endpoint(std::string_view endpoint,
return r;
}

Task<Result<ContributorList>> NetworkClient::getContributors() {
auto result = co_await this->request<api::Github>(http::verb::get,
githubEndpoint(
"/NJUPT-SAST/sast-evento/contributors"));
if (result.isErr())
co_return Err(result.unwrapErr());

ContributorList 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<ReleaseEntity>> NetworkClient::getLatestRelease() {
auto result = co_await this
->request<api::Github>(http::verb::get,
githubEndpoint(
"/NJUPT-SAST/sast-evento/releases/latest"));
if (result.isErr())
co_return Err(result.unwrapErr());

ReleaseEntity 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::githubEndpoint(std::string_view endpoint) {
return urls::url(GITHUB_API_GATEWAY + endpoint.data());
}

urls::url NetworkClient::githubEndpoint(std::string_view endpoint,
std::initializer_list<urls::param> const& params) {
auto r = urls::url(GITHUB_API_GATEWAY + endpoint.data());
r.params().append(params.begin(), params.end());
return r;
}

JsonResult NetworkClient::handleResponse(http::response<http::dynamic_body> response) {
if (response.result() != http::status::ok) {
return Err(Error(Error::Network, std::to_string(response.result_int())));
Expand Down
23 changes: 21 additions & 2 deletions src/Infrastructure/Network/NetworkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace urls = boost::urls; // from <boost/url.hpp>
using JsonResult = Result<nlohmann::basic_json<>>;
using EventEntityList = std::vector<EventEntity>;
using SlideEntityList = std::vector<SlideEntity>;
using ContributorList = std::vector<ContributorEntity>;

template<typename T>
using Task = net::awaitable<T>;

Expand Down Expand Up @@ -65,6 +67,10 @@ class NetworkClient {

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

Task<Result<ContributorList>> getContributors();

Task<Result<ReleaseEntity>> getLatestRelease();

// access token
// NOTE: `AUTOMATICALLY` added to request header if exists
std::optional<std::string> tokenBytes;
Expand Down Expand Up @@ -95,18 +101,31 @@ class NetworkClient {
Task<JsonResult> request(http::verb verb,
urls::url_view url,
std::initializer_list<urls::param> const& params = {}) {
auto req = Api::makeRequest(verb, url, params);
auto req = Api::makeRequest(verb, url, std::nullopt, params);
auto reply = co_await _manager->makeReply(url.host(), req);
if (reply.isErr())
co_return reply.unwrapErr();
co_return reply.unwrap();

nlohmann::basic_json<> res;
try {
res = nlohmann::json::parse(beast::buffers_to_string(reply.unwrap().body().data()));
debug(), res.dump();
} catch (const nlohmann::json::parse_error& e) {
co_return Err(Error(Error::JsonDes, e.what()));
}

co_return res;
}

// url builder
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 for evento backend
static urls::url githubEndpoint(std::string_view endpoint);
static urls::url githubEndpoint(std::string_view endpoint,
std::initializer_list<urls::param> const& queryParams);
//response handler for github api
static JsonResult handleResponse(http::response<http::dynamic_body> response);

private:
Expand Down
19 changes: 19 additions & 0 deletions src/Infrastructure/Network/Response/ContributorEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// IWYU pragma: private, include <Infrastructure/Network/ResponseStruct.h>
#pragma once

#include <nlohmann/json.hpp>
#include <string>

namespace evento {

struct ContributorEntity {
std::string login;
std::string avatar_url;
std::string html_url;
int contributions;

NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(
ContributorEntity, login, avatar_url, html_url, contributions);
};

} // namespace evento
20 changes: 20 additions & 0 deletions src/Infrastructure/Network/Response/ReleaseEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// IWYU pragma: private, include <Infrastructure/Network/ResponseStruct.h>
#pragma once

#include <nlohmann/json.hpp>
#include <string>

namespace evento {

struct ReleaseEntity {
std::string tag_name;
std::string name;
std::string body;
std::string html_url;
std::string published_at;

NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(
ReleaseEntity, tag_name, name, body, html_url, published_at);
};

} // namespace evento
14 changes: 8 additions & 6 deletions src/Infrastructure/Network/ResponseStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#pragma once

#include "Response/AttachmentEntity.h" // IWYU pragma: export
#include "Response/EventEntity.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
#include "Response/UserInfoEntity.h" // IWYU pragma: export
#include "Response/AttachmentEntity.h" // IWYU pragma: export
#include "Response/ContributorEntity.h" // IWYU pragma: export
#include "Response/EventEntity.h" // IWYU pragma: export
#include "Response/FeedbackEntity.h" // IWYU pragma: export
#include "Response/LoginResEntity.h" // IWYU pragma: export
#include "Response/ReleaseEntity.h" // IWYU pragma: export
#include "Response/SlideEntity.h" // IWYU pragma: export
#include "Response/UserInfoEntity.h" // IWYU pragma: export