| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Apigee::Edge; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 14144 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 26 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 5 |  |  |  |  |  |  | our $VERSION = '0.08'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 4 | use Carp; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 71 |  | 
| 8 | 1 |  |  | 1 |  | 508 | use Mojo::UserAgent; | 
|  | 1 |  |  |  |  | 248188 |  | 
|  | 1 |  |  |  |  | 13 |  | 
| 9 | 1 |  |  | 1 |  | 54 | use Mojo::URL; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 10 | 1 |  |  | 1 |  | 27 | use Mojo::Util qw(b64_encode); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 87 |  | 
| 11 | 1 |  |  | 1 |  | 507 | use URI::Escape qw/uri_escape/; | 
|  | 1 |  |  |  |  | 1121 |  | 
|  | 1 |  |  |  |  | 49 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 1 |  |  | 1 |  | 6 | use vars qw/$errstr/; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 1535 |  | 
| 14 | 0 |  |  | 0 | 1 |  | sub errstr { return $errstr } | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub new {    ## no critic (ArgUnpacking) | 
| 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 |  |  |  |  |  | return $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 |  |  |  |  |  | return $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 |  |  |  |  |  | return $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 |  |  |  |  |  | return $self->request('GET', "/o/" . $self->{org} . "/apps?apptype=" . uri_escape($type)); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub get_apps {    ## no critic (ArgUnpacking) | 
| 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 |  |  |  |  |  | return $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 |  |  |  |  |  | return $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 |  |  |  |  |  | return $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 |  |  |  |  |  | return $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 |  |  |  |  |  | return $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 |  |  |  |  |  | return $self->request('GET', "/o/" . $self->{org} . "/developers"); | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub set_developer_status { | 
| 106 | 0 |  |  | 0 | 1 |  | my ($self, $email, $status); | 
| 107 | 0 |  |  |  |  |  | return $self->request('GET', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "?action=" . uri_escape($status)); | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub update_developer {    ## no critic (ArgUnpacking) | 
| 111 | 0 |  |  | 0 | 1 |  | my $self  = shift; | 
| 112 | 0 |  |  |  |  |  | my $email = shift; | 
| 113 | 0 | 0 |  |  |  |  | my %args  = @_ % 2 ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  |  |  | 
| 114 | 0 | 0 |  |  |  |  | $email or croak "email is required."; | 
| 115 | 0 |  |  |  |  |  | return $self->request('PUT', "/o/" . $self->{org} . "/developers/" . uri_escape($email), %args); | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | ## Apps: Developer http://apigee.com/docs/api/apps-developer | 
| 119 |  |  |  |  |  |  | sub change_app_status { | 
| 120 | 0 |  |  | 0 | 1 |  | my ($self, $email, $app) = @_; | 
| 121 | 0 |  |  |  |  |  | return $self->request('GET', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app)); | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub create_developer_app {    ## no critic (ArgUnpacking) | 
| 125 | 0 |  |  | 0 | 1 |  | my $self  = shift; | 
| 126 | 0 |  |  |  |  |  | my $email = shift; | 
| 127 | 0 | 0 |  |  |  |  | my %args  = @_ % 2 ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  |  |  | 
| 128 | 0 |  |  |  |  |  | return $self->request('POST', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps", %args); | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | sub delete_developer_app { | 
| 132 | 0 |  |  | 0 | 1 |  | my ($self, $email, $app) = @_; | 
| 133 | 0 |  |  |  |  |  | return $self->request('DELETE', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app)); | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | sub get_developer_app { | 
| 137 | 0 |  |  | 0 | 1 |  | my ($self, $email, $app) = @_; | 
| 138 | 0 |  |  |  |  |  | return $self->request('GET', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app)); | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | sub get_developer_apps {    ## no critic (ArgUnpacking) | 
| 142 | 0 |  |  | 0 | 1 |  | my $self  = shift; | 
| 143 | 0 |  |  |  |  |  | my $email = shift; | 
| 144 | 0 | 0 |  |  |  |  | $email or croak "email is required."; | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 0 | 0 |  |  |  |  | my %args = @_ % 2 ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  |  |  | 
| 147 | 0 |  |  |  |  |  | my $url = Mojo::URL->new("/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps"); | 
| 148 | 0 | 0 |  |  |  |  | $url->query(\%args) if %args; | 
| 149 | 0 |  |  |  |  |  | return $self->request('GET', $url->to_string); | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | sub update_developer_app {    ## no critic (ArgUnpacking) | 
| 153 | 0 |  |  | 0 | 1 |  | my $self  = shift; | 
| 154 | 0 |  |  |  |  |  | my $email = shift; | 
| 155 | 0 |  |  |  |  |  | my $app   = shift; | 
| 156 | 0 | 0 |  |  |  |  | my %args  = @_ % 2 ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  |  |  | 
| 157 | 0 | 0 |  |  |  |  | $email or croak "email is required."; | 
| 158 | 0 | 0 |  |  |  |  | $app   or croak "app is required."; | 
| 159 | 0 |  |  |  |  |  | return $self->request('PUT', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app), %args); | 
| 160 |  |  |  |  |  |  | } | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | sub get_count_of_developer_app_resource { | 
| 163 | 0 |  |  | 0 | 1 |  | my ($self, $email, $app, $entity) = @_; | 
| 164 |  |  |  |  |  |  | return $self->request('GET', | 
| 165 |  |  |  |  |  |  | "/o/" | 
| 166 |  |  |  |  |  |  | . $self->{org} | 
| 167 | 0 |  |  |  |  |  | . "/developers/" | 
| 168 |  |  |  |  |  |  | . uri_escape($email) | 
| 169 |  |  |  |  |  |  | . "/apps/" | 
| 170 |  |  |  |  |  |  | . uri_escape($app) | 
| 171 |  |  |  |  |  |  | . qq~?"query=count&entity=~ | 
| 172 |  |  |  |  |  |  | . uri_escape($entity) | 
| 173 |  |  |  |  |  |  | . qq~"~); | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | sub regenerate_developer_app_key {    ## no critic (ArgUnpacking) | 
| 177 | 0 |  |  | 0 | 1 |  | my $self  = shift; | 
| 178 | 0 |  |  |  |  |  | my $email = shift; | 
| 179 | 0 |  |  |  |  |  | my $app   = shift; | 
| 180 | 0 | 0 |  |  |  |  | my %args  = @_ % 2 ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  |  |  | 
| 181 | 0 | 0 |  |  |  |  | $email or croak "email is required."; | 
| 182 | 0 | 0 |  |  |  |  | $app   or croak "app is required."; | 
| 183 | 0 |  |  |  |  |  | return $self->request('POST', "/o/" . $self->{org} . "/developers/" . uri_escape($email) . "/apps/" . uri_escape($app), %args); | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | ## API Products http://apigee.com/docs/api/api-products-1 | 
| 187 |  |  |  |  |  |  | sub create_api_product {              ## no critic (ArgUnpacking) | 
| 188 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 189 | 0 | 0 |  |  |  |  | my %args = @_ % 2 ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  |  |  | 
| 190 | 0 |  |  |  |  |  | return $self->request('POST', "/o/" . $self->{org} . "/apiproducts", %args); | 
| 191 |  |  |  |  |  |  | } | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | sub update_api_product {              ## no critic (ArgUnpacking) | 
| 194 | 0 |  |  | 0 | 1 |  | my $self    = shift; | 
| 195 | 0 |  |  |  |  |  | my $product = shift; | 
| 196 | 0 | 0 |  |  |  |  | my %args    = @_ % 2 ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  |  |  | 
| 197 | 0 | 0 |  |  |  |  | $product or croak "product is required."; | 
| 198 | 0 |  |  |  |  |  | return $self->request('PUT', "/o/" . $self->{org} . "/apiproducts/" . uri_escape($product), %args); | 
| 199 |  |  |  |  |  |  | } | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | sub delete_api_product { | 
| 202 | 0 |  |  | 0 | 1 |  | my ($self, $product) = @_; | 
| 203 | 0 |  |  |  |  |  | return $self->request('DELETE', "/o/" . $self->{org} . "/apiproducts/" . uri_escape($product)); | 
| 204 |  |  |  |  |  |  | } | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | sub get_api_product { | 
| 207 | 0 |  |  | 0 | 1 |  | my ($self, $product) = @_; | 
| 208 | 0 |  |  |  |  |  | return $self->request('GET', "/o/" . $self->{org} . "/apiproducts/" . uri_escape($product)); | 
| 209 |  |  |  |  |  |  | } | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | sub get_api_products {    ## no critic (ArgUnpacking) | 
| 212 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 213 | 0 | 0 |  |  |  |  | my %args = @_ % 2 ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  |  |  | 
| 214 | 0 |  |  |  |  |  | my $url  = Mojo::URL->new("/o/" . $self->{org} . "/apiproducts"); | 
| 215 | 0 | 0 |  |  |  |  | $url->query(\%args) if %args; | 
| 216 | 0 |  |  |  |  |  | return $self->request('GET', $url->to_string); | 
| 217 |  |  |  |  |  |  | } | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | sub search_api_products { | 
| 220 | 0 |  |  | 0 | 1 |  | return (shift)->get_api_products(@_); | 
| 221 |  |  |  |  |  |  | } | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | sub get_api_product_details {    ## no critic (ArgUnpacking) | 
| 224 | 0 |  |  | 0 | 1 |  | my $self    = shift; | 
| 225 | 0 |  |  |  |  |  | my $product = shift; | 
| 226 | 0 | 0 |  |  |  |  | my %args    = @_ % 2 ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  |  |  | 
| 227 | 0 |  |  |  |  |  | my $url     = Mojo::URL->new("/o/" . $self->{org} . "/apiproducts/" . uri_escape($product)); | 
| 228 | 0 | 0 |  |  |  |  | $url->query(\%args) if %args; | 
| 229 | 0 |  |  |  |  |  | return $self->request('GET', $url->to_string); | 
| 230 |  |  |  |  |  |  | } | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | sub request { | 
| 233 | 0 |  |  | 0 | 1 |  | my ($self, $method, $url, %params) = @_; | 
| 234 |  |  |  |  |  |  |  | 
| 235 | 0 |  |  |  |  |  | $errstr = '';                # reset | 
| 236 |  |  |  |  |  |  |  | 
| 237 | 0 |  |  |  |  |  | my $ua = $self->__ua; | 
| 238 | 0 |  |  |  |  |  | my $header = {Authorization => 'Basic ' . b64_encode($self->{usr} . ':' . $self->{pwd}, '')}; | 
| 239 | 0 | 0 |  |  |  |  | $header->{'Content-Type'} = 'application/json' if %params; | 
| 240 | 0 | 0 |  |  |  |  | my @extra = %params ? (json => \%params) : (); | 
| 241 | 0 |  |  |  |  |  | my $tx = $ua->build_tx($method => $self->{endpoint} . $url => $header => @extra); | 
| 242 | 0 |  |  |  |  |  | $tx->req->headers->accept('application/json'); | 
| 243 |  |  |  |  |  |  |  | 
| 244 | 0 |  |  |  |  |  | $tx = $ua->start($tx); | 
| 245 | 0 | 0 | 0 |  |  |  | if ($tx->res->headers->content_type and $tx->res->headers->content_type =~ 'application/json') { | 
| 246 | 0 |  |  |  |  |  | return $tx->res->json; | 
| 247 |  |  |  |  |  |  | } | 
| 248 | 0 | 0 |  |  |  |  | if (!$tx->success) { | 
| 249 | 0 |  |  |  |  |  | $errstr = "Failed to fetch $url: " . $tx->error->{message}; | 
| 250 | 0 |  |  |  |  |  | return; | 
| 251 |  |  |  |  |  |  | } | 
| 252 |  |  |  |  |  |  |  | 
| 253 | 0 |  |  |  |  |  | $errstr = "Unknown Response."; | 
| 254 | 0 |  |  |  |  |  | return; | 
| 255 |  |  |  |  |  |  | } | 
| 256 |  |  |  |  |  |  |  | 
| 257 |  |  |  |  |  |  | 1; | 
| 258 |  |  |  |  |  |  | __END__ |