Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
tyanmahou committed Dec 26, 2023
1 parent faa5317 commit 6ec101a
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Re-Abyss/app/utils/Network/GItHub__/Issue/Issue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <abyss/utils/JSON/JSONUtil.hpp>

namespace abyss::Network::GitHub::Issue
{
/// <summary>
/// GitHub Issue
/// </summary>
struct Issue
{
[[JSON_BIND(id)]]
s3d::int32 id;

[[JSON_BIND(number)]]
s3d::int32 number;

[[JSON_BIND(title)]]
s3d::String title;

[[JSON_BIND(url)]]
s3d::String url;
};

using IssueList = s3d::Array<Issue>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <abyss/utils/Network/GitHub/Issue/ListRepositoryIssuesApi.hpp>

namespace abyss::Network::GitHub::Issue
{
ApiResponse<ListRepositoryIssuesApi::Response> ListRepositoryIssuesApi::Request(
const s3d::String& owner,
const s3d::String& repo,
const s3d::String& token
)
{
return BaseApi::GetTo<Response>(s3d::Fmt(Url)(owner, repo), token);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <abyss/utils/Network/GitHub/base/BaseApi.hpp>
#include <abyss/utils/Network/GitHub/Issue/Issue.hpp>

namespace abyss::Network::GitHub::Issue
{
class ListRepositoryIssuesApi
{
public:
static constexpr auto* Url = U"https://api.github.com/repos/{}/{}/issues";
using Response = IssueList;
public:

static ApiResponse<Response> Request(
const s3d::String& owner,
const s3d::String& repo,
const s3d::String& token
);
};
}
20 changes: 20 additions & 0 deletions Re-Abyss/app/utils/Network/GitHub_/GitHub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <abyss/utils/Network/GitHub/GitHub.hpp>
#include <abyss/utils/Network/GitHub/Issue/ListRepositoryIssuesApi.hpp>
#include <abyss/utils/Network/GitHub/GraphQL/GraphQLApi.hpp>
#include <Siv3D.hpp>

namespace abyss::Network::GitHub
{
s3d::JSON GitHub::GraphQL(const s3d::String& query, const s3d::String& token)
{
return GraphQL::GraphQLApi::Request(query, token);
}
ApiResponse<Issue::ListRepositoryIssuesApi::Response> GitHub::ListRepositoryIssues(const s3d::String& owner, const s3d::String& repo, const s3d::String& token)
{
return Issue::ListRepositoryIssuesApi::Request(owner, repo, token);
}
bool GitHub::OpenIssueByBrowser(const s3d::String& owner, const s3d::String& repo, const s3d::int32 number)
{
return System::LaunchBrowser(U"https://github.com/{}/{}/issues/{}"_fmt(owner, repo, number));
}
}
37 changes: 37 additions & 0 deletions Re-Abyss/app/utils/Network/GitHub_/GitHub.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
#include <Siv3D/String.hpp>
#include <abyss/utils/Network/GitHub/Issue/ListRepositoryIssuesApi.hpp>

namespace abyss::Network::GitHub
{
class GitHub
{
public:
/// <summary>
/// グラフクエリをリクエスト
/// </summary>
/// <param name="query"></param>
/// <param name="token"></param>
/// <returns></returns>
static s3d::JSON GraphQL(const s3d::String& query, const s3d::String& token);

static ApiResponse<Issue::ListRepositoryIssuesApi::Response> ListRepositoryIssues(
const s3d::String& owner,
const s3d::String& repo,
const s3d::String& token
);

/// <summary>
/// ブラウザでイシューを開く
/// </summary>
/// <param name="owner"></param>
/// <param name="repo"></param>
/// <param name="number"></param>
/// <returns></returns>
static bool OpenIssueByBrowser(
const s3d::String& owner,
const s3d::String& repo,
const s3d::int32 number
);
};
}
13 changes: 13 additions & 0 deletions Re-Abyss/app/utils/Network/GitHub_/GraphQL/GraphQLApi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <abyss/utils/Network/GitHub/GraphQL/GraphQLApi.hpp>
#include <abyss/utils/Network/GitHub/base/BaseApi.hpp>

namespace abyss::Network::GitHub::GraphQL
{
s3d::JSON GraphQLApi::Request(const s3d::String& query, const s3d::String& token)
{
JSON request;
request[U"query"] = query;
return BaseApi::Post(Url, token, request).value_or(s3d::JSON::Invalid());
}
}

14 changes: 14 additions & 0 deletions Re-Abyss/app/utils/Network/GitHub_/GraphQL/GraphQLApi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <Siv3D/JSON.hpp>

namespace abyss::Network::GitHub::GraphQL
{
class GraphQLApi
{
public:
static constexpr auto* Url = U"https://api.github.com/graphql";
public:

static s3d::JSON Request(const s3d::String& query, const s3d::String& token);
};
}
25 changes: 25 additions & 0 deletions Re-Abyss/app/utils/Network/GitHub_/Issue/Issue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <abyss/utils/JSON/JSONUtil.hpp>

namespace abyss::Network::GitHub::Issue
{
/// <summary>
/// GitHub Issue
/// </summary>
struct Issue
{
[[JSON_BIND(id)]]
s3d::int32 id;

[[JSON_BIND(number)]]
s3d::int32 number;

[[JSON_BIND(title)]]
s3d::String title;

[[JSON_BIND(url)]]
s3d::String url;
};

using IssueList = s3d::Array<Issue>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <abyss/utils/Network/GitHub/Issue/ListRepositoryIssuesApi.hpp>

namespace abyss::Network::GitHub::Issue
{
ApiResponse<ListRepositoryIssuesApi::Response> ListRepositoryIssuesApi::Request(
const s3d::String& owner,
const s3d::String& repo,
const s3d::String& token
)
{
return BaseApi::GetTo<Response>(s3d::Fmt(Url)(owner, repo), token);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <abyss/utils/Network/GitHub/base/BaseApi.hpp>
#include <abyss/utils/Network/GitHub/Issue/Issue.hpp>

namespace abyss::Network::GitHub::Issue
{
class ListRepositoryIssuesApi
{
public:
static constexpr auto* Url = U"https://api.github.com/repos/{}/{}/issues";
using Response = IssueList;
public:

static ApiResponse<Response> Request(
const s3d::String& owner,
const s3d::String& repo,
const s3d::String& token
);
};
}
9 changes: 9 additions & 0 deletions Re-Abyss/app/utils/Network/GitHub_/base/ApiResponse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <expected>
#include <Siv3D/Types.hpp>

namespace abyss::Network::GitHub
{
template<class T>
using ApiResponse = std::expected<T, s3d::int32>;
}
44 changes: 44 additions & 0 deletions Re-Abyss/app/utils/Network/GitHub_/base/BaseApi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <abyss/utils/Network/GitHub/base/BaseApi.hpp>

namespace abyss::Network::GitHub
{
ApiResponse<s3d::JSON> BaseApi::Get(const s3d::String& url, const s3d::String& token)
{
MemoryWriter writer;
auto result = s3d::SimpleHTTP::Get(
url,
{
{U"User-Agent", U"Awesome-Octocat-App"},
{U"Accept", U"application/vnd.github+json"},
{U"Authorization", U"Bearer {}"_fmt(token)},
{U"X-GitHub-Api-Version", U"2022-11-28"}
}, writer);
if (result.isOK()) {
return s3d::JSON::Load(MemoryReader(writer.getBlob()));
} else {
return std::unexpected(static_cast<s3d::int32>(result.getStatusCode()));
}
}
ApiResponse<s3d::JSON> BaseApi::Post(const s3d::String& url, const s3d::String& token, const s3d::JSON& param)
{
std::string src = Format(param).narrow();
MemoryWriter writer;
auto result = s3d::SimpleHTTP::Post(
url,
{
{U"User-Agent", U"Awesome-Octocat-App"},
{U"Accept", U"application/vnd.github+json"},
{U"Authorization", U"Bearer {}"_fmt(token)},
{U"X-GitHub-Api-Version", U"2022-11-28"}
},
src.c_str(),
src.length(),
writer
);
if (result.isOK()) {
return s3d::JSON::Load(MemoryReader(writer.getBlob()));
} else {
return std::unexpected(static_cast<s3d::int32>(result.getStatusCode()));
}
}
}
31 changes: 31 additions & 0 deletions Re-Abyss/app/utils/Network/GitHub_/base/BaseApi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <abyss/utils/Network/GitHub/base/ApiResponse.hpp>
#include <abyss/utils/JSON/JSONUtil.hpp>

namespace abyss::Network::GitHub
{
class BaseApi
{
public:
static ApiResponse<s3d::JSON> Get(
const s3d::String& url,
const s3d::String& token
);
static ApiResponse<s3d::JSON> Post(
const s3d::String& url,
const s3d::String& token,
const s3d::JSON& param
);
template<class T>
static ApiResponse<T> GetTo(
const s3d::String& url,
const s3d::String& token
) {
auto result = Get(url, token);
if (!result.has_value()) {
return std::unexpected(result.error());
}
return JSONUtil::FromJSON<T>(result.value());
}
};
}

0 comments on commit 6ec101a

Please sign in to comment.