1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#!perl
use strict;
use warnings;
use Class::Load qw(try_load_class);
use Carp;
my $api = shift;
my $class = "WWW::ORCID::API::$api";
try_load_class($class)
or croak("Could not load $class: $!");
my $ops = $class->ops;
my ($major, $minor) = $api =~ /([0-9])_([0-9])/;
my ($name) = $api =~ /_([a-z]+)$/;
$name ||= 'member';
$name = "$major.$minor $name";
my $pod = <<EOF;
=pod
=head1 NAME
$class - A client for the ORCID $name API
=head1 CREATING A NEW INSTANCE
The C<new> method returns a new L<$name API client|$class>.
Arguments to new:
=head2 C<client_id>
Your ORCID client id (required).
=head2 C<client_secret>
Your ORCID client secret (required).
=head2 C<sandbox>
The client will talk to the L<ORCID sandbox API|https://api.sandbox.orcid.org/v2.0> if set to C<1>.
=head2 C<transport>
Specify the HTTP client to use. Possible values are L<LWP> or L<HTTP::Tiny>. Default is L<LWP>.
=head1 METHODS
=head2 C<client_id>
Returns the ORCID client id used by the client.
=head2 C<client_secret>
Returns the ORCID client secret used by the client.
=head2 C<sandbox>
Returns C<1> if the client is using the sandbox API, C<0> otherwise.
=head2 C<transport>
Returns what HTTP transport the client is using.
=head2 C<api_url>
Returns the base API url used by the client.
=head2 C<oauth_url>
Returns the base OAuth url used by the client.
=head2 C<access_token>
Request a new access token.
my \$token = \$client->access_token(
grant_type => 'client_credentials',
scope => '/read-public',
);
=head2 C<authorize_url>
Helper that returns an authorization url for 3-legged OAuth requests.
# in your web application
redirect(\$client->authorize_url(
show_login => 'true',
scope => '/person/update',
response_type => 'code',
redirect_uri => 'http://your.callback/url',
));
See the C</authorize> and C</authorized> routes in the included playground
application for an example.
=head2 C<record_url>
Helper that returns an orcid record url.
\$client->record_url('0000-0003-4791-9455')
# returns
# http://orcid.org/0000-0003-4791-9455
# or
# http://sandbox.orcid.org/0000-0003-4791-9455
=head2 C<read_public_token>
Return an access token with scope C</read-public>.
EOF
$pod .= <<EOF if $class->can('read_limited_token');
=head2 C<read_limited_token>
Return an access token with scope C</read-limited>.
EOF
$pod .= <<EOF if $class->can('client');
=head2 C<client>
Get details about the current client.
EOF
$pod .= <<EOF if $class->can('search');
=head2 C<search>
my \$hits = \$client->search(q => "johnson");
EOF
my $token_arg = 'token => $token';
my $orcid_arg = ', orcid => $orcid';
my $pc_arg = ", put_code => '123'";
my $pc_bulk_arg = ", put_code => ['123', '456']";
for my $op (sort keys %$ops) {
my $spec = $ops->{$op};
my $sym = $op;
$sym =~ s|[-/]|_|g;
my $base_args = $token_arg;
$base_args .= $orcid_arg if $spec->{orcid};
if ($spec->{get} || $spec->{get_pc} || $spec->{get_pc_bulk}) {
$pod .= "=head2 C<${sym}>\n\n";
if ($spec->{get} && ($spec->{get_pc} || $spec->{get_pc_bulk})) {
$pod .= " my \$recs = \$client->${sym}($base_args);\n";
}
elsif ($spec->{get}) {
$pod .= " my \$rec = \$client->${sym}($base_args);\n";
}
if ($spec->{get_pc}) {
$pod .= " my \$rec = \$client->${sym}($base_args$pc_arg);\n";
}
if ($spec->{get_pc_bulk}) {
$pod .= " my \$recs = \$client->${sym}($base_args$pc_bulk_arg);\n";
}
$pod .= "\nEquivalent to:\n\n \$client->get('${op}', \%opts)\n\n";
}
if ($spec->{add}) {
$pod .= "=head2 C<add_${sym}>\n\n";
$pod .= " \$client->add_${sym}(\$data, $base_args);\n";
$pod .= "\nEquivalent to:\n\n \$client->add('${op}', \$data, \%opts)\n\n";
}
if ($spec->{update}) {
$pod .= "=head2 C<update_${sym}>\n\n";
$pod .= " \$client->update_${sym}(\$data, $base_args$pc_arg);\n";
$pod .= "\nEquivalent to:\n\n \$client->update('${op}', \$data, \%opts)\n\n";
}
if ($spec->{delete}) {
$pod .= "=head2 C<delete_${sym}>\n\n";
$pod .= " my \$ok = \$client->delete_${sym}($base_args$pc_arg);\n";
$pod .= "\nEquivalent to:\n\n \$client->delete('${op}', \%opts)\n\n";
}
}
$pod .= <<EOF;
=head2 C<last_error>
Returns the last error returned by the ORCID API, if any.
=head2 C<log>
Returns the L<Log::Any> logger.
=cut
EOF
print $pod;
|