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

Migrate octokit.NET to the new generated SDK #2

Merged
merged 2 commits into from
Sep 17, 2024
Merged

Conversation

Cai1Hsu
Copy link
Contributor

@Cai1Hsu Cai1Hsu commented Sep 17, 2024

GitHub.Octokit.Client 利用 restful API 来构建 SDK。调用 SDK 的方式也遵循 restful 风格。例如,对于以下api

curl -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <YOUR-TOKEN>" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/OWNER/REPO/contents/PATH

使用以下代码可以获取一个 Request 对象:

githubClient.Repos[OWNER][REPO].Contents[PATH];

参数以及路径都是完全对应的,只有大小写的区别。

接下来,对该 Requset 调用 GetAsync 方法即可发起请求。方法返回 nullable 的结果对象,直接处理对象即可。

认证

虽然 Github OpenAPI 规范中对于所有 API 都要求提供 token,但并非所有 API 都需要。对于公开提供的内容,可以省略 Token。

通过以下方式配置的依赖注入,githubClient 对象可以在无需 token 的情况下访问公开资源:

var services = new ServiceCollection();

services.AddLogging()
    .AddAnonymousAuth()
    .AddGitHubClient();

如果需要使用 token,可以通过以下方式配置依赖注入:

var services = new ServiceCollection();

services.AddGitHubAuth()
    .WithToken<EnvTokenProvider>()
    .AddGitHubClient();

Token 的传入方式既可以使用环境变量,也可以通过启动参数。详细查看 LoongsonNeuq.Common/Auth 下的代码。

依赖的配置方法全部遵循 fluent create 设计模式,扩展方法可以在 LoongsonNeuq.Common/DependencyInjectionExtensions.cs 中添加

@Cai1Hsu Cai1Hsu linked an issue Sep 17, 2024 that may be closed by this pull request
4 tasks
@Cai1Hsu Cai1Hsu merged commit 43fb9ad into master Sep 17, 2024
@Cai1Hsu Cai1Hsu deleted the migrate-kiota branch September 17, 2024 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate octokit.NET to the new generated SDK
1 participant