This repository was archived by the owner on Jun 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAPI.pm
More file actions
112 lines (80 loc) · 2.34 KB
/
Copy pathAPI.pm
File metadata and controls
112 lines (80 loc) · 2.34 KB
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
package OpenSourceOrg::API;
use strict;
use warnings;
use utf8;
# VERSION
# ABSTRACT: Perl API Bindings to the OSI License API
=head1 SYNOPSIS
use OpenSourceOrg::API;
my $client = OpenSourceOrg::API->new();
my $all_licenses = $client->all();
my $permisive_licenses = $client->tagged('permissive');
my $mit = $client->get('MIT');
my $Mozilla_2_0 = $client->get_by_scheme('SPDX', 'MPL-2.0');
=head1 DESCRIPTION
OpenSOurceOrg::API is an API Wrapper that allows you to query the Open Source License API with Perl.
L<https://github.com/OpenSourceOrg/api/blob/master/doc/endpoints.md>
=cut
use Moo;
use REST::Client;
use Const::Fast;
use JSON;
use Carp;
const my $base_url => 'https://api.opensource.org';
has _api_client => (
is => 'lazy'
);
sub _build__api_client {
my $client = REST::Client->new();
$client->setHost($base_url);
return $client;
}
=method all
Get a list of all known licenses.
The response is the perl equivalent of the json returned by the api,
documented in L<https://github.com/OpenSourceOrg/api/blob/master/doc/endpoints.md#schema>
=cut
sub all {
my $self = shift();
my $client = $self->_api_client;
return $self->_handle_response( $client->GET('/licenses/') );
}
=method tagged
Find all licenses tagged with a C<keyword> as defined in
L<https://github.com/OpenSourceOrg/api/blob/master/doc/endpoints.md#keywords>
=cut
sub tagged {
my $self = shift();
my $keyword = shift();
my $client = $self->_api_client;
return $self->_handle_response( $client->GET('/licenses/' . $keyword) );
}
=method get
Get a license by its OSI ID
=cut
sub get {
my $self = shift();
my $osi_id = shift();
my $client = $self->_api_client;
return $self->_handle_response( $client->GET('/license/' . $osi_id) );
}
=method get_by_scheme
Get a license by its Scheme ID
=cut
sub get_by_scheme {
my $self = shift();
my $scheme = shift();
my $id = shift();
my $client = $self->_api_client;
return $self->_handle_response( $client->GET('/license/' . join( '/', $scheme, $id ) ) );
}
sub _handle_response {
my $self = shift();
my $response = shift();
if ($response->responseCode() == 200) {
return from_json($response->responseContent);
} else {
croak 'Error: ' . $response->responseCode() . ". Content: " . $response->responseContent ;
}
}
1;