LoginSignup
2
1

More than 3 years have passed since last update.

PerlでGitHubのプレビュー版APIにアクセスする

Last updated at Posted at 2019-04-28

Preview版として提供されているGitHubのProjects APIをPerl+Net::GitHubから実行してみたので、メモ。

Net::GitHubでは多くのAPIがオブジェクトメソッドでラッピングされているけど、ProjectsのようにまだラッピングされていないAPIもある。これらを実行するときは、Net::GitHub::V3のqueryメソッドを使う。

use Net::GitHub::V3;
my $github = Net::GitHub::V3->new(login => 'fayland', pass => 'mypass');
my $result = $github->query('GET', '/repos/USERNAME/REPOSITORY/projects', {});

通常のAPIは上記の方法でアクセスできるのだけど、Projectsなどプレビュー版のAPIでは、以下に書かれている通りACCEPTヘッダーを追加する必要がある。

The Projects API is currently available for developers to preview. During the preview period, the API may change without advance notice. Please see the blog post for full details. To access the API during the preview period, you must provide a custom media type in the Accept header:
application/vnd.github.inertia-preview+json
( https://developer.github.com/v3/projects/ より)

Net::GitHubはHTTPアクセスにLWP::UserAgentを利用しており、このインスタンスにはuaメソッドでアクセスできる。このLWP::UserAgentインスタンスにデフォルトのヘッダー項目を追加してやればいい。

こんな感じになる。

use Net::GitHub::V3;
my $github = Net::GitHub::V3->new(login => 'fayland', pass => 'mypass');
$github->ua->default_headers->push_header('Accept' => "application/vnd.github.inertia-preview+json");
my $result = $github->query('GET', '/repos/USERNAME/REPOSITORY/projects', {});

これでプレビュー版のAPIにアクセスが可能。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1