File Coverage

blib/lib/Apigee/Edge.pm
Criterion Covered Total %
statement 24 161 14.9
branch 0 58 0.0
condition 0 7 0.0
subroutine 8 39 20.5
pod 29 30 96.6
total 61 295 20.6


line stmt bran cond sub pod time code
1             package Apigee::Edge;
2              
3 1     1   17644 use strict;
  1         2  
  1         37  
4 1     1   4 use warnings;
  1         1  
  1         36  
5             our $VERSION = '0.07';
6              
7 1     1   4 use Carp;
  1         5  
  1         72  
8 1     1   572 use Mojo::UserAgent;
  1         245797  
  1         12  
9 1     1   37 use Mojo::URL;
  1         1  
  1         3  
10 1     1   20 use Mojo::Util qw(b64_encode);
  1         2  
  1         50  
11 1     1   614 use URI::Escape qw/uri_escape/;
  1         1163  
  1         62  
12              
13 1     1   9 use vars qw/$errstr/;
  1         1  
  1         1740  
14 0     0 0   sub errstr { $errstr }
15              
16             sub new {
17 0     0 1   my $class = shift;
18 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
19              
20 0           for (qw/org usr pwd/) {
21 0 0         $args{$_} || croak "Param $_ is required.";
22             }
23              
24 0   0       $args{endpoint} ||= 'https://api.enterprise.apigee.com/v1';
25 0   0       $args{timeout} ||= 60; # for ua timeout
26              
27 0           return bless \%args, $class;
28             }
29              
30             sub __ua {
31 0     0     my $self = shift;
32              
33 0 0         return $self->{ua} if exists $self->{ua};
34              
35 0           my $ua = Mojo::UserAgent->new;
36 0           $ua->max_redirects(3);
37 0           $ua->inactivity_timeout($self->{timeout});
38 0           $ua->proxy->detect; # env proxy
39 0           $ua->cookie_jar(0);
40 0           $ua->max_connections(100);
41 0           $self->{ua} = $ua;
42              
43 0           return $ua;
44             }
45              
46             ## Apps http://apigee.com/docs/api/apps-0
47             sub get_app {
48 0     0 1   my ($self, $app_id) = @_;
49 0           $self->request('GET', "/o/" . $self->{org} . "/apps/$app_id");
50             }
51              
52             sub get_apps_by_family {
53 0     0 1   my ($self, $family) = @_;
54 0           $self->request('GET', "/o/" . $self->{org} . "/apps?appfamily=" . uri_escape($family));
55             }
56              
57             sub get_apps_by_keystatus {
58 0     0 1   my ($self, $keystatus) = @_;
59 0           $self->request('GET', "/o/" . $self->{org} . "/apps?keyStatus=" . uri_escape($keystatus));
60             }
61              
62             sub get_apps_by_type {
63 0     0 1   my ($self, $type) = @_;
64 0           $self->request('GET', "/o/" . $self->{org} . "/apps?apptype=" . uri_escape($type));
65             }
66              
67             sub get_apps {
68 0     0 1   my $self = shift;
69 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
70 0           my $url = Mojo::URL->new("/o/" . $self->{org} . "/apps");
71 0 0         $url->query(\%args) if %args;
72 0           $self->request('GET', $url->to_string);
73             }
74              
75             ## Developers http://apigee.com/docs/api/developers-0
76             sub create_developer {
77 0     0 1   my $self = shift;
78 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
79 0           $self->request('POST', "/o/" . $self->{org} . "/developers", %args);
80             }
81              
82             sub get_developer {
83 0     0 1   my $self = shift;
84 0           my ($email) = @_;
85 0           $self->request('GET', "/o/" . $self->{org} . "/developers/" . uri_escape($email));
86             }
87              
88             sub delete_developer {
89 0     0 1   my $self = shift;
90 0           my ($email) = @_;
91 0           $self->request('DELETE', "/o/" . $self->{org} . "/developers/" . uri_escape($email));
92             }
93              
94             sub get_app_developers {
95 0     0 1   my $self = shift;
96 0           my ($app) = @_;
97 0           $self->request('GET', "/o/" . $self->{org} . "/developers?app=" . uri_escape($app));
98             }
99              
100             sub get_developers {
101 0     0 1   my $self = shift;
102 0           $self->request('GET', "/o/" . $self->{org} . "/developers");
103             }
104              
105             sub set_developer_status {
106 0     0 1   my ($self, $email, $status);
107 0           $self->request('GET', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "?action=" . uri_escape($status));
108             }
109              
110             sub update_developer {
111 0     0 1   my $self = shift; my $email = shift;
  0            
112 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
113 0 0         $email or croak "email is required.";
114 0           $self->request('PUT', "/o/" . $self->{org} . "/developers/" . uri_escape($email), %args);
115             }
116              
117             ## Apps: Developer http://apigee.com/docs/api/apps-developer
118             sub change_app_status {
119 0     0 1   my ($self, $email, $app) = @_;
120 0           $self->request('GET', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app));
121             }
122              
123             sub create_developer_app {
124 0     0 1   my $self = shift; my $email = shift;
  0            
125 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
126 0           $self->request('POST', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps", %args);
127             }
128              
129             sub delete_developer_app {
130 0     0 1   my ($self, $email, $app) = @_;
131 0           $self->request('DELETE', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app));
132             }
133              
134             sub get_developer_app {
135 0     0 1   my ($self, $email, $app) = @_;
136 0           $self->request('GET', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app));
137             }
138              
139             sub get_developer_apps {
140 0     0 1   my $self = shift; my $email = shift;
  0            
141 0 0         $email or croak "email is required.";
142              
143 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
144 0           my $url = Mojo::URL->new("/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps");
145 0 0         $url->query(\%args) if %args;
146 0           $self->request('GET', $url->to_string);
147             }
148              
149             sub update_developer_app {
150 0     0 1   my $self = shift; my $email = shift; my $app = shift;
  0            
  0            
151 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
152 0 0         $email or croak "email is required.";
153 0 0         $app or croak "app is required.";
154 0           $self->request('PUT', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app), %args);
155             }
156              
157             sub get_count_of_developer_app_resource {
158 0     0 1   my ($self, $email, $app, $entity) = @_;
159 0           $self->request('GET', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app) . qq~?"query=count&entity=~ . uri_escape($entity) . qq~"~);
160             }
161              
162             sub regenerate_developer_app_key {
163 0     0 1   my $self = shift; my $email = shift; my $app = shift;
  0            
  0            
164 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
165 0 0         $email or croak "email is required.";
166 0 0         $app or croak "app is required.";
167 0           $self->request('POST', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app), %args);
168             }
169              
170             ## API Products http://apigee.com/docs/api/api-products-1
171             sub create_api_product {
172 0     0 1   my $self = shift;
173 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
174 0           $self->request('POST', "/o/" . $self->{org} . "/apiproducts", %args);
175             }
176              
177             sub update_api_product {
178 0     0 1   my $self = shift; my $product = shift;
  0            
179 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
180 0 0         $product or croak "product is required.";
181 0           $self->request('PUT', "/o/" . $self->{org} . "/apiproducts/" . uri_escape($product), %args);
182             }
183              
184             sub delete_api_product {
185 0     0 1   my ($self, $product) = @_;
186 0           $self->request('DELETE', "/o/" . $self->{org} . "/apiproducts/" . uri_escape($product));
187             }
188              
189             sub get_api_product {
190 0     0 1   my ($self, $product) = @_;
191 0           $self->request('GET', "/o/" . $self->{org} . "/apiproducts/" . uri_escape($product));
192             }
193              
194             sub get_api_products {
195 0     0 1   my $self = shift;
196 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
197 0           my $url = Mojo::URL->new("/o/" . $self->{org} . "/apiproducts");
198 0 0         $url->query(\%args) if %args;
199 0           $self->request('GET', $url->to_string);
200             }
201              
202             sub search_api_products {
203 0     0 1   (shift)->get_api_products(@_);
204             }
205              
206             sub get_api_product_details {
207 0     0 1   my $self = shift; my $product = shift;
  0            
208 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
209 0           my $url = Mojo::URL->new("/o/" . $self->{org} . "/apiproducts/" . uri_escape($product));
210 0 0         $url->query(\%args) if %args;
211 0           $self->request('GET', $url->to_string);
212             }
213              
214             sub request {
215 0     0 1   my ($self, $method, $url, %params) = @_;
216              
217 0           $errstr = ''; # reset
218              
219 0           my $ua = $self->__ua;
220 0           my $header = {
221             Authorization => 'Basic ' . b64_encode($self->{usr} . ':' . $self->{pwd}, '')
222             };
223 0 0         $header->{'Content-Type'} = 'application/json' if %params;
224 0 0         my @extra = %params ? (json => \%params) : ();
225 0           my $tx = $ua->build_tx($method => $self->{endpoint} . $url => $header => @extra);
226 0           $tx->req->headers->accept('application/json');
227              
228 0           $tx = $ua->start($tx);
229 0 0 0       if ($tx->res->headers->content_type and $tx->res->headers->content_type =~ 'application/json') {
230 0           return $tx->res->json;
231             }
232 0 0         if (! $tx->success) {
233 0           $errstr = "Failed to fetch $url: " . $tx->error->{message};
234 0           return;
235             }
236              
237 0           $errstr = "Unknown Response.";
238 0           return;
239             }
240              
241             1;
242             __END__