| 1 | package Amazon::API::Role::Services; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Amazon::API::Constants qw($SUCCESS %SERVICE_CONTENT_TYPES); | ||
| 7 | use Amazon::API::Utils qw(slurp_json choose); | ||
| 8 | use Carp; | ||
| 9 | use Data::Dumper; | ||
| 10 | use English qw(-no_match_vars); | ||
| 11 | use File::ShareDir qw(dist_file); | ||
| 12 | use JSON qw(decode_json encode_json); | ||
| 13 | use Storable qw(nstore retrieve); | ||
| 14 | |||
| 15 | use Role::Tiny; | ||
| 16 | |||
| 17 | ######################################################################## | ||
| 18 | sub cmd_list_services { | ||
| 19 | ######################################################################## | ||
| 20 | my ($self) = @_; | ||
| 21 | |||
| 22 | my ($service) = $self->get_args; | ||
| 23 | |||
| 24 | my $boto_path = $self->get_botocore_path; | ||
| 25 | |||
| 26 | my $boto_services = choose { | ||
| 27 | if ($boto_path) { | ||
| 28 | $self->_check_botocore_path($boto_path); | ||
| 29 | |||
| 30 | return $self->fetch_boto_services; | ||
| 31 | } | ||
| 32 | |||
| 33 | return retrieve('botocore-metadata.api') | ||
| 34 | if -e 'botocore-metadata.api'; | ||
| 35 | |||
| 36 | my ($metadata_file) = grep { -e $_ } ( 'botocore-metadata.api', dist_file( 'Amazon-API', 'botocore-metadata.api' ) ); | ||
| 37 | |||
| 38 | return retrieve($metadata_file) | ||
| 39 | if $metadata_file; | ||
| 40 | |||
| 41 | croak "ERROR: usage: list-services -b botocore-path service\n" | ||
| 42 | }; | ||
| 43 | |||
| 44 | if ($service) { | ||
| 45 | if ( my $filter = $self->get_filter ) { | ||
| 46 | print {*STDOUT} $boto_services->{$service}{$filter} // '<undefined>'; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | print {*STDOUT} JSON->new->pretty->encode( $boto_services->{$service} ); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | print {*STDOUT} JSON->new->pretty->encode($boto_services); | ||
| 54 | } | ||
| 55 | |||
| 56 | return $SUCCESS; | ||
| 57 | } | ||
| 58 | |||
| 59 | ######################################################################## | ||
| 60 | sub cmd_create_services { | ||
| 61 | ######################################################################## | ||
| 62 | my ($self) = @_; | ||
| 63 | |||
| 64 | my $boto_path = $self->_check_botocore_path( $self->get_botocore_path ); | ||
| 65 | |||
| 66 | my $output_path = $self->get_output_path; | ||
| 67 | |||
| 68 | my $file = sprintf '%s/%s', $output_path, $self->get_file // 'services.api'; | ||
| 69 | |||
| 70 | my $boto_services = $self->fetch_boto_services; | ||
| 71 | |||
| 72 | my $limit = $self->get_limit // scalar keys %{$boto_services}; | ||
| 73 | |||
| 74 | my $count = 0; | ||
| 75 | |||
| 76 | my %service_protocol; | ||
| 77 | |||
| 78 | foreach my $s ( sort keys %{$boto_services} ) { | ||
| 79 | last if !$limit; | ||
| 80 | |||
| 81 | my $path = join q{/}, @{ $boto_services->{$s}->{path} }; | ||
| 82 | |||
| 83 | my $service = slurp_json($path); | ||
| 84 | my $metadata = $service->{metadata}; | ||
| 85 | |||
| 86 | my $protocol = $metadata->{protocol}; | ||
| 87 | my $protocols = $metadata->{protocols} // [$protocol]; | ||
| 88 | my $jsonVersion = $metadata->{jsonVersion}; | ||
| 89 | |||
| 90 | $limit--; | ||
| 91 | |||
| 92 | $self->get_logger->info( sprintf '%s - (%s/%s)', $path, ++$count, scalar keys %{$boto_services} ); | ||
| 93 | |||
| 94 | foreach my $protocol ( @{$protocols} ) { | ||
| 95 | next if !exists $SERVICE_CONTENT_TYPES{$protocol}; | ||
| 96 | |||
| 97 | my $content_type = $SERVICE_CONTENT_TYPES{$protocol}; | ||
| 98 | |||
| 99 | if ( $protocol eq 'json' && $jsonVersion ) { | ||
| 100 | $content_type = sprintf '%s-%s', $content_type, $jsonVersion; | ||
| 101 | } | ||
| 102 | |||
| 103 | $service_protocol{$s} = $content_type; | ||
| 104 | |||
| 105 | last; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | nstore( | ||
| 110 | { service_protocol => \%service_protocol, | ||
| 111 | content_types => \%SERVICE_CONTENT_TYPES, | ||
| 112 | }, | ||
| 113 | $file | ||
| 114 | ); | ||
| 115 | |||
| 116 | return $SUCCESS; | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; |