| 1 | =begin comment | ||
| 2 | |||
| 3 | Copyright (c) 2026 Aspose.Cells Cloud | ||
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 5 | of this software and associated documentation files (the "Software"), to deal | ||
| 6 | in the Software without restriction, including without limitation the rights | ||
| 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 8 | copies of the Software, and to permit persons to whom the Software is | ||
| 9 | furnished to do so, subject to the following conditions: | ||
| 10 | |||
| 11 | The above copyright notice and this permission notice shall be included in all | ||
| 12 | copies or substantial portions of the Software. | ||
| 13 | |||
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 20 | SOFTWARE. | ||
| 21 | |||
| 22 | =end comment | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | package AsposeCellsCloud::ApiFactory; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | use utf8; | ||
| 31 | |||
| 32 | use Carp; | ||
| 33 | use Module::Find; | ||
| 34 | |||
| 35 | usesub AsposeCellsCloud::Object; | ||
| 36 | |||
| 37 | use AsposeCellsCloud::ApiClient; | ||
| 38 | |||
| 39 | =head1 Name | ||
| 40 | |||
| 41 | AsposeCellsCloud::ApiFactory - constructs APIs to retrieve AsposeCellsCloud objects | ||
| 42 | |||
| 43 | =head1 Synopsis | ||
| 44 | |||
| 45 | package My::Petstore::App; | ||
| 46 | |||
| 47 | use AsposeCellsCloud::ApiFactory; | ||
| 48 | |||
| 49 | my $api_factory = AsposeCellsCloud::ApiFactory->new( ... ); # any args for ApiClient constructor | ||
| 50 | |||
| 51 | # later... | ||
| 52 | my $pet_api = $api_factory->get_api('Pet'); | ||
| 53 | |||
| 54 | # $pet_api isa AsposeCellsCloud::PetApi | ||
| 55 | |||
| 56 | my $pet = $pet_api->get_pet_by_id(pet_id => $pet_id); | ||
| 57 | |||
| 58 | # object attributes have proper accessors: | ||
| 59 | printf "Pet's name is %s", $pet->name; | ||
| 60 | |||
| 61 | # change the value stored on the object: | ||
| 62 | $pet->name('Dave'); | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | # Load all the API classes and construct a lookup table at startup time | ||
| 67 | my %_apis = map { $_ =~ /^AsposeCellsCloud::(.*)$/; $1 => $_ } | ||
| 68 | grep {$_ =~ /Api$/} | ||
| 69 | usesub 'AsposeCellsCloud'; | ||
| 70 | |||
| 71 | =head1 new($api_client) | ||
| 72 | |||
| 73 | create a new AsposeCellsCloud::ApiFactory instance with the given AsposeCellsCloud::ApiClient instance. | ||
| 74 | |||
| 75 | =head1 new(%parameters) | ||
| 76 | |||
| 77 | Any parameters are optional, and are passed to and stored on the api_client object. | ||
| 78 | |||
| 79 | See L<AsposeCellsCloud::ApiClient> and L<AsposeCellsCloud::Configuration> for valid parameters | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub new { | ||
| 84 | my ($class) = shift; | ||
| 85 | |||
| 86 | my $api_client; | ||
| 87 | if ($_[0] && ref $_[0] && ref $_[0] eq 'AsposeCellsCloud::ApiClient' ) { | ||
| 88 | $api_client = $_[0]; | ||
| 89 | } else { | ||
| 90 | $api_client = AsposeCellsCloud::ApiClient->new(@_); | ||
| 91 | } | ||
| 92 | bless { api_client => $api_client }, $class; | ||
| 93 | } | ||
| 94 | |||
| 95 | =head1 get_api($which) | ||
| 96 | |||
| 97 | Returns an API object of the requested type. | ||
| 98 | |||
| 99 | $which is a nickname for the class: | ||
| 100 | |||
| 101 | FooBarClient::BazApi has nickname 'Baz' | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub get_api { | ||
| 106 | my ($self, $which) = @_; | ||
| 107 | croak "API not specified" unless $which; | ||
| 108 | my $api_class = $_apis{"${which}Api"} || croak "No known API for '$which'"; | ||
| 109 | return $api_class->new($self->api_client); | ||
| 110 | } | ||
| 111 | |||
| 112 | =head1 api_client() | ||
| 113 | |||
| 114 | Returns the api_client object, should you ever need it. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub api_client { $_[0]->{api_client} } | ||
| 119 | |||
| 120 | =head1 apis_available() | ||
| 121 | =cut | ||
| 122 | |||
| 123 | sub apis_available { return map { $_ =~ s/Api$//; $_ } sort keys %_apis } | ||
| 124 | |||
| 125 | =head1 classname_for() | ||
| 126 | =cut | ||
| 127 | |||
| 128 | sub classname_for { | ||
| 129 | my ($self, $api_name) = @_; | ||
| 130 | return $_apis{"${api_name}Api"}; | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | 1; |