動作
使用 Perl 操作 REST API¶
以下 Perl 腳本展示了如何使用 Redmine REST API。它們會從地區環境變數中猜測檔案編碼。詳情請參閱 open。
GET¶
#!/usr/bin/perl
# restget : GET a representation of a REST resource
use strict;
use warnings;
use Getopt::Std;
use LWP::UserAgent;
use JSON;
use open ':locale'; # probe the locale environment variables like LANG
sub HELP_MESSAGE {
print STDERR <<"EOM";
usage : $0 [-p] [-k API_KEY] url
-p: pretty print
-k API_KEY: API Access Key
EOM
exit 0;
}
our ($opt_p, $opt_k);
getopts('pk:') or HELP_MESSAGE();
my $url = shift;
HELP_MESSAGE() unless $url;
#################################################
my $ua = new LWP::UserAgent;
$ua->timeout(10); # default: 180sec
$ua->ssl_opts( verify_hostname => 0 ); # skip hostname verification
$ua->default_header('X-Redmine-API-Key' => $opt_k) if $opt_k;
my $res = $ua->get($url);
die $res->message if $res->is_error;
my $content = $res->content;
utf8::decode($content); # convert UTF-8 binary to text
if ($opt_p) { # pretty print
my $perl_ref = from_json($content);
print to_json($perl_ref, {pretty=>1});
} else {
print $content, "\n";
}
exit 0;
備註
my $perl_ref = from_json($content);
這裡的 Perl 參考" $perl_ref
是指涉整個已解析資料結構的參考。
使用範例
./restget https://redmine.dev.org.tw/issues/22097.json
./restget -k 57acc39afc967564aa79a6bd329897d7040a9bc8 http://demo.redmine.org/issues/117594.json
以下指令會將 Textile 表格放入剪貼簿。
./restget https://redmine.dev.org.tw/projects/redmine/versions.json | perl -MJSON -nle 'for $v (sort{$a->{name} cmp $b->{name}}@{decode_json($_)->{versions}}){$d=$v->{description};print qq/|version:"$v->{name}"|$d|/ if $d}' > /dev/clipboard
您可以按照以下步驟將其貼上到您的 Wiki 頁面。
0.7.1 | 錯誤修正版本 |
0.7.2 | 錯誤修正版本 |
0.7.3 | 安全性+錯誤修正版本 |
0.9.0 | 0.9 候選版本 1 |
0.9.6 | 安全性版本 |
1.0.1 | 1.0.0 RC 的錯誤修正 - (預計發布日期) |
1.2.3 | 1.2.x 系列的錯誤修正版本 |
1.3.1 | 維護版本 |
1.4.3 | 維護版本 |
1.4.7 | 安全性版本 |
2.0.2 | 維護版本 |
2.6.10 | 適用於 Rails 3.2,因為 JRuby Nokogiri/Loofah 不支援 Rails 4.2 |
下一個主要版本的候選版本 | 貢獻者希望在下一個主要版本中新增的功能 |
下一個次要版本的候選版本 | 貢獻者希望在下一個次要版本中新增的修正 |
版本:「未規劃」 | Redmine 的核心貢獻者希望在未來版本中新增的功能 |
PUT¶
#!/usr/bin/perl
# restput : PUT a representation of a REST resource
use strict;
use warnings;
use Getopt::Std;
use LWP::UserAgent;
use open ':locale'; # probe the locale environment variables like LANG
sub HELP_MESSAGE {
print STDERR <<"EOM";
usage : $0 [-p API_KEY] url [files...]
-k API_KEY: API Access Key
EOM
exit 0;
}
our ($opt_k);
getopts('k:') or HELP_MESSAGE();
my $url = shift;
HELP_MESSAGE() unless $url;
#################################################
my $ua = new LWP::UserAgent;
$ua->timeout(10); # default: 180sec
$ua->ssl_opts( verify_hostname => 0 ); # skip hostname verification
$ua->default_header('X-Redmine-API-Key' => $opt_k) if $opt_k;
my $content;
{
local $/;
$content = <>; # gets whole input
}
utf8::encode($content); # convert UTF-8 binary to text
my $res = $ua->put($url,
'Content-Type' => 'application/json;charset=utf-8',
'Content' => $content);
die $res->message if $res->is_error;
print $res->content();
exit 0;
使用範例
# change my name
echo '{"user":{"firstname":"James","lastname":"Bond"}}' | ./restput -k 57acc39afc967564aa79a6bd329897d7040a9bc8 http://demo.redmine.org/users/current.json
以下腳本會將 Wiki 內容轉換為 JSON 格式。
#!/usr/bin/perl
# restwiki : convert a Wiki content to JSON format
use strict;
use warnings;
use Getopt::Std;
use JSON;
use open ':locale';
sub HELP_MESSAGE {
print STDERR <<"EOM";
usage : $0 [-c comment] [files...]
-c : comment
EOM
exit 0;
}
our ($opt_c);
getopts('c:') or HELP_MESSAGE();
#################################################
my $ref;
{
local $/; # enable slurp mode
$ref->{wiki_page}->{text} = <>;
}
$ref->{wiki_page}->{comments} = $opt_c if $opt_c;
print to_json($ref);
exit 0;
以下指令會建立或更新名為「測試頁面」的 Wiki 頁面。
./restwiki foo | ./restput -k 57acc39afc967564aa79a6bd329897d7040a9bc8 http://my.redmine.local/projects/test-project/wiki/Test_Page.json
您可能想要使用單行指令來完成以下操作:
perl -MJSON -e 'local $/;$ref->{wiki_page}->{text} = <>;print to_json($ref)' foo | ./restput -k 57acc39afc967564aa79a6bd329897d7040a9bc8 http://my.redmine.local/projects/test-project/wiki/Test_Page.json
資源¶
由 Hiroo Hayashi 更新於 8 年前 · 2 個版本