| 1 | =begin comment | ||
| 2 | |||
| 3 | Fastly API | ||
| 4 | |||
| 5 | Via the Fastly API you can perform any of the operations that are possible within the management console, including creating services, domains, and backends, configuring rules or uploading your own application code, as well as account operations such as user administration and billing reports. The API is organized into collections of endpoints that allow manipulation of objects related to Fastly services and accounts. For the most accurate and up-to-date API reference content, visit our [Developer Hub](https://www.fastly.com/documentation/reference/api/) | ||
| 6 | |||
| 7 | The version of the API Spec document: 1.0.0 | ||
| 8 | Contact: oss@fastly.com | ||
| 9 | |||
| 10 | =end comment | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | # | ||
| 15 | # NOTE: This class is auto generated. | ||
| 16 | # Do not edit the class manually. | ||
| 17 | # | ||
| 18 | package WebService::Fastly::Role; | ||
| 19 | use utf8; | ||
| 20 | |||
| 21 | use Moose::Role; | ||
| 22 | use namespace::autoclean; | ||
| 23 | use Class::Inspector; | ||
| 24 | use Log::Any qw($log); | ||
| 25 | use WebService::Fastly::ApiFactory; | ||
| 26 | |||
| 27 | has base_url => ( is => 'ro', | ||
| 28 | required => 0, | ||
| 29 | isa => 'Str', | ||
| 30 | documentation => 'Root of the server that requests are sent to', | ||
| 31 | ); | ||
| 32 | |||
| 33 | has api_factory => ( is => 'ro', | ||
| 34 | isa => 'WebService::Fastly::ApiFactory', | ||
| 35 | builder => '_build_af', | ||
| 36 | lazy => 1, | ||
| 37 | documentation => 'Builds an instance of the endpoint API class', | ||
| 38 | ); | ||
| 39 | |||
| 40 | has tokens => ( is => 'ro', | ||
| 41 | isa => 'HashRef', | ||
| 42 | required => 0, | ||
| 43 | default => sub { {} }, | ||
| 44 | documentation => 'The auth tokens required by the application - basic, OAuth and/or API key(s)', | ||
| 45 | ); | ||
| 46 | |||
| 47 | has _cfg => ( is => 'ro', | ||
| 48 | isa => 'WebService::Fastly::Configuration', | ||
| 49 | default => sub { WebService::Fastly::Configuration->new() }, | ||
| 50 | ); | ||
| 51 | |||
| 52 | has version_info => ( is => 'ro', | ||
| 53 | isa => 'HashRef', | ||
| 54 | default => sub { { | ||
| 55 | app_name => 'Fastly API', | ||
| 56 | app_version => '1.0.0', | ||
| 57 | generator_class => 'org.openapitools.codegen.languages.PerlClientCodegen', | ||
| 58 | } }, | ||
| 59 | documentation => 'Information about the application version and the codegen codebase version' | ||
| 60 | ); | ||
| 61 | |||
| 62 | sub BUILD { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | $self->_cfg->accept_tokens( $self->tokens ) if keys %{$self->tokens}; | ||
| 66 | |||
| 67 | # ignore these symbols imported into API namespaces | ||
| 68 | my %outsiders = map {$_ => 1} qw( croak ); | ||
| 69 | |||
| 70 | my %delegates; | ||
| 71 | |||
| 72 | # collect the methods callable on each API | ||
| 73 | foreach my $api_name ($self->api_factory->apis_available) { | ||
| 74 | my $api_class = $self->api_factory->classname_for($api_name); | ||
| 75 | my $methods = Class::Inspector->methods($api_class, 'expanded'); # not Moose, so use CI instead | ||
| 76 | my @local_methods = grep {! /^_/} grep {! $outsiders{$_}} map {$_->[2]} grep {$_->[1] eq $api_class} @$methods; | ||
| 77 | push( @{$delegates{$_}}, {api_name => $api_name, api_class => $api_class} ) for @local_methods; | ||
| 78 | } | ||
| 79 | |||
| 80 | # remove clashes | ||
| 81 | foreach my $method (keys %delegates) { | ||
| 82 | if ( @{$delegates{$method}} > 1 ) { | ||
| 83 | my ($apis) = delete $delegates{$method}; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | # build the flattened API | ||
| 88 | foreach my $api_name ($self->api_factory->apis_available) { | ||
| 89 | my $att_name = sprintf "%s_api", lc($api_name); | ||
| 90 | my $api_class = $self->api_factory->classname_for($api_name); | ||
| 91 | my @delegated = grep { $delegates{$_}->[0]->{api_name} eq $api_name } keys %delegates; | ||
| 92 | $log->debugf("Adding API: '%s' handles %s", $att_name, join ', ', @delegated); | ||
| 93 | $self->meta->add_attribute( $att_name => ( | ||
| 94 | is => 'ro', | ||
| 95 | isa => $api_class, | ||
| 96 | default => sub {$self->api_factory->get_api($api_name)}, | ||
| 97 | lazy => 1, | ||
| 98 | handles => \@delegated, | ||
| 99 | ) ); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _build_af { | ||
| 104 | my $self = shift; | ||
| 105 | my %args; | ||
| 106 | $args{base_url} = $self->base_url if $self->base_url; | ||
| 107 | return WebService::Fastly::ApiFactory->new(%args); | ||
| 108 | } | ||
| 109 | |||
| 110 | =head1 NAME | ||
| 111 | |||
| 112 | WebService::Fastly::Role - a Moose role for the Fastly API | ||
| 113 | |||
| 114 | =head2 Fastly API version: 1.0.0 | ||
| 115 | |||
| 116 | =head1 VERSION | ||
| 117 | |||
| 118 | Automatically generated: | ||
| 119 | |||
| 120 | =over 4 | ||
| 121 | =item Build package: org.openapitools.codegen.languages.PerlClientCodegen | ||
| 122 | |||
| 123 | =item Codegen version: | ||
| 124 | |||
| 125 | =back | ||
| 126 | |||
| 127 | =head2 A note on Moose | ||
| 128 | |||
| 129 | This role is the only component of the library that uses Moose. See | ||
| 130 | WebService::Fastly::ApiFactory for non-Moosey usage. | ||
| 131 | |||
| 132 | =head1 SYNOPSIS | ||
| 133 | |||
| 134 | This module provides an interface to the generated library. All the classes, | ||
| 135 | objects, and methods (well, not quite *all*, see below) are flattened into this | ||
| 136 | role. | ||
| 137 | |||
| 138 | package MyApp; | ||
| 139 | use Moose; | ||
| 140 | with 'WebService::Fastly::Role'; | ||
| 141 | |||
| 142 | package main; | ||
| 143 | |||
| 144 | my $api = MyApp->new({ tokens => $tokens }); | ||
| 145 | |||
| 146 | my $pet = $api->get_pet_by_id(pet_id => $pet_id); | ||
| 147 | |||
| 148 | =head2 Structure of the library | ||
| 149 | |||
| 150 | The library consists of a set of API classes, one for each endpoint. These APIs | ||
| 151 | implement the method calls available on each endpoint. | ||
| 152 | |||
| 153 | Additionally, there is a set of "object" classes, which represent the objects | ||
| 154 | returned by and sent to the methods on the endpoints. | ||
| 155 | |||
| 156 | An API factory class is provided, which builds instances of each endpoint API. | ||
| 157 | |||
| 158 | This Moose role flattens all the methods from the endpoint APIs onto the consuming | ||
| 159 | class. It also provides methods to retrieve the endpoint API objects, and the API | ||
| 160 | factory object, should you need it. | ||
| 161 | |||
| 162 | For documentation of all these methods, see AUTOMATIC DOCUMENTATION below. | ||
| 163 | |||
| 164 | =head2 Configuring authentication | ||
| 165 | |||
| 166 | In the normal case, the API Spec will describe what parameters are | ||
| 167 | required and where to put them. You just need to supply the tokens. | ||
| 168 | |||
| 169 | my $tokens = { | ||
| 170 | # basic | ||
| 171 | username => $username, | ||
| 172 | password => $password, | ||
| 173 | |||
| 174 | # oauth | ||
| 175 | access_token => $oauth_token, | ||
| 176 | |||
| 177 | # keys | ||
| 178 | $some_key => { token => $token, | ||
| 179 | prefix => $prefix, | ||
| 180 | in => $in, # 'head||query', | ||
| 181 | }, | ||
| 182 | |||
| 183 | $another => { token => $token, | ||
| 184 | prefix => $prefix, | ||
| 185 | in => $in, # 'head||query', | ||
| 186 | }, | ||
| 187 | ..., | ||
| 188 | |||
| 189 | }; | ||
| 190 | |||
| 191 | my $api = MyApp->new({ tokens => $tokens }); | ||
| 192 | |||
| 193 | Note these are all optional, as are C<prefix> and C<in>, and depend on the API | ||
| 194 | you are accessing. Usually C<prefix> and C<in> will be determined by the code generator from | ||
| 195 | the spec and you will not need to set them at run time. If not, C<in> will | ||
| 196 | default to 'head' and C<prefix> to the empty string. | ||
| 197 | |||
| 198 | The tokens will be placed in a L<WebService::Fastly::Configuration> instance | ||
| 199 | as follows, but you don't need to know about this. | ||
| 200 | |||
| 201 | =over 4 | ||
| 202 | |||
| 203 | =item C<$cfg-\>{username}> | ||
| 204 | |||
| 205 | String. The username for basic auth. | ||
| 206 | |||
| 207 | =item C<$cfg-\>{password}> | ||
| 208 | |||
| 209 | String. The password for basic auth. | ||
| 210 | |||
| 211 | =item C<$cfg-\>{api_key}> | ||
| 212 | |||
| 213 | Hashref. Keyed on the name of each key (there can be multiple tokens). | ||
| 214 | |||
| 215 | $cfg->{api_key} = { | ||
| 216 | secretKey => 'aaaabbbbccccdddd', | ||
| 217 | anotherKey => '1111222233334444', | ||
| 218 | }; | ||
| 219 | |||
| 220 | =item C<$cfg->{api_key_prefix}> | ||
| 221 | |||
| 222 | Hashref. Keyed on the name of each key (there can be multiple tokens). Note not | ||
| 223 | all api keys require a prefix. | ||
| 224 | |||
| 225 | $cfg->{api_key_prefix} = { | ||
| 226 | secretKey => 'string', | ||
| 227 | anotherKey => 'same or some other string', | ||
| 228 | }; | ||
| 229 | |||
| 230 | =item C<$config-\>{access_token}> | ||
| 231 | |||
| 232 | String. The OAuth access token. | ||
| 233 | |||
| 234 | =back | ||
| 235 | |||
| 236 | =head1 METHODS | ||
| 237 | |||
| 238 | =head2 C<base_url> | ||
| 239 | |||
| 240 | The generated code has the C<base_url> already set as a default value. This method | ||
| 241 | returns the current value of C<base_url>. | ||
| 242 | |||
| 243 | =head2 C<api_factory> | ||
| 244 | |||
| 245 | Returns an API factory object. You probably won't need to call this directly. | ||
| 246 | |||
| 247 | $self->api_factory('Pet'); # returns a WebService::Fastly::PetApi instance | ||
| 248 | |||
| 249 | $self->pet_api; # the same | ||
| 250 | |||
| 251 | =head1 MISSING METHODS | ||
| 252 | |||
| 253 | Most of the methods on the API are delegated to individual endpoint API objects | ||
| 254 | (e.g. Pet API, Store API, User API etc). Where different endpoint APIs use the | ||
| 255 | same method name (e.g. C<new()>), these methods can't be delegated. So you need | ||
| 256 | to call C<$api-E<gt>pet_api-E<gt>new()>. | ||
| 257 | |||
| 258 | In principle, every API is susceptible to the presence of a few, random, undelegatable | ||
| 259 | method names. In practice, because of the way method names are constructed, it's | ||
| 260 | unlikely in general that any methods will be undelegatable, except for: | ||
| 261 | |||
| 262 | new() | ||
| 263 | class_documentation() | ||
| 264 | method_documentation() | ||
| 265 | |||
| 266 | To call these methods, you need to get a handle on the relevant object, either | ||
| 267 | by calling C<$api-E<gt>foo_api> or by retrieving an object, e.g. | ||
| 268 | C<$api-E<gt>get_pet_by_id(pet_id =E<gt> $pet_id)>. They are class methods, so | ||
| 269 | you could also call them on class names. | ||
| 270 | |||
| 271 | =head1 AUTOMATIC DOCUMENTATION | ||
| 272 | |||
| 273 | You can print out a summary of the generated API by running the included | ||
| 274 | C<autodoc> script in the C<bin> directory of your generated library. A few | ||
| 275 | output formats are supported: | ||
| 276 | |||
| 277 | Usage: autodoc [OPTION] | ||
| 278 | |||
| 279 | -w wide format (default) | ||
| 280 | -n narrow format | ||
| 281 | -p POD format | ||
| 282 | -H HTML format | ||
| 283 | -m Markdown format | ||
| 284 | -h print this help message | ||
| 285 | -c your application class | ||
| 286 | |||
| 287 | The C<-c> option allows you to load and inspect your own application. A dummy | ||
| 288 | namespace is used if you don't supply your own class. | ||
| 289 | |||
| 290 | =head1 DOCUMENTATION FROM THE API Spec | ||
| 291 | |||
| 292 | Additional documentation for each class and method may be provided by the API | ||
| 293 | spec. If so, this is available via the C<class_documentation()> and | ||
| 294 | C<method_documentation()> methods on each generated object class, and the | ||
| 295 | C<method_documentation()> method on the endpoint API classes: | ||
| 296 | |||
| 297 | my $cmdoc = $api->pet_api->method_documentation->{$method_name}; | ||
| 298 | |||
| 299 | my $odoc = $api->get_pet_by_id->(pet_id => $pet_id)->class_documentation; | ||
| 300 | my $omdoc = $api->get_pet_by_id->(pet_id => $pet_id)->method_documentation->{method_name}; | ||
| 301 | |||
| 302 | Each of these calls returns a hashref with various useful pieces of information. | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | 1; |