File Coverage

lib/Smartcat/Client/Configuration.pm
Criterion Covered Total %
statement 29 61 47.5
branch 0 12 0.0
condition 9 20 45.0
subroutine 7 10 70.0
pod 0 4 0.0
total 45 107 42.0


line stmt bran cond sub pod time code
1              
2             =begin comment
3              
4             Smartcat Integration API
5              
6             No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
7              
8             OpenAPI spec version: v1
9              
10             Generated by: https://github.com/swagger-api/swagger-codegen.git
11              
12             =end comment
13              
14             =cut
15              
16             #
17             # NOTE: This class is auto generated by the swagger code generator program.
18             # Do not edit the class manually.
19             # Ref: https://github.com/swagger-api/swagger-codegen
20             #
21             package Smartcat::Client::Configuration;
22              
23 11     11   80 use strict;
  11         24  
  11         356  
24 11     11   58 use warnings;
  11         23  
  11         314  
25 11     11   55 use utf8;
  11         23  
  11         91  
26              
27 11     11   283 use Log::Any qw($log);
  11         22  
  11         66  
28 11     11   2424 use Carp;
  11         21  
  11         638  
29              
30 11     11   66 use constant VERSION => '1.0.0';
  11         20  
  11         7372  
31              
32             =head1 Name
33              
34             Smartcat::Client::Configuration - holds the configuration for all Smartcat::Client Modules
35              
36             =head1 new(%parameters)
37              
38             =over 4
39              
40             =item http_timeout: (optional)
41              
42             Integer. timeout for HTTP requests in seconds
43              
44             default: 180
45              
46             =item http_user_agent: (optional)
47              
48             String. custom UserAgent header
49              
50             default: Swagger-Codegen/1.0.0/perl
51              
52             =item api_key: (optional)
53              
54             Hashref. Keyed on the name of each key (there can be multiple tokens).
55              
56             api_key => {
57             secretKey => 'aaaabbbbccccdddd',
58             anotherKey => '1111222233334444',
59             };
60              
61             =item api_key_prefix: (optional)
62              
63             Hashref. Keyed on the name of each key (there can be multiple tokens). Note not all api keys require a prefix.
64              
65             api_key_prefix => {
66             secretKey => 'string',
67             anotherKey => 'same or some other string',
68             };
69            
70             =item api_key_in: (optional)
71              
72             =item username: (optional)
73              
74             String. The username for basic auth.
75              
76             =item password: (optional)
77              
78             String. The password for basic auth.
79              
80             =item access_token: (optional)
81              
82             String. The OAuth access token.
83              
84             =item base_url: (optional)
85              
86             String. The base URL of the API
87              
88             default: https://smartcat.ai
89              
90             =back
91              
92             =cut
93              
94             sub new {
95 11     11 0 33 my ( $self, %p ) = ( shift, @_ );
96              
97             # class/static variables
98 11   50     92 $p{http_timeout} //= 180;
99 11   50     62 $p{http_user_agent} //= 'Swagger-Codegen/1.0.0/perl';
100              
101             # authentication setting
102 11   50     84 $p{api_key} //= {};
103 11   50     58 $p{api_key_prefix} //= {};
104 11   50     59 $p{api_key_in} //= {};
105              
106             # username and password for HTTP basic authentication
107 11   50     58 $p{username} //= '';
108 11   50     63 $p{password} //= '';
109              
110             # access token for OAuth
111 11   50     72 $p{access_token} //= '';
112              
113             # base_url
114 11   50     65 $p{base_url} //= 'https://smartcat.ai';
115              
116 11         41 return bless \%p => $self;
117             }
118              
119             sub get_tokens {
120 0     0 0   my $self = shift;
121              
122 0           my $tokens = {};
123 0 0         $tokens->{username} = $self->{username} if $self->{username};
124 0 0         $tokens->{password} = $self->{password} if $self->{password};
125 0 0         $tokens->{access_token} = $self->{access_token} if $self->{access_token};
126              
127 0           foreach my $token_name ( keys %{ $self->{api_key} } ) {
  0            
128 0           $tokens->{$token_name}->{token} = $self->{api_key}{$token_name};
129 0           $tokens->{$token_name}->{prefix} = $self->{api_key_prefix}{$token_name};
130 0           $tokens->{$token_name}->{in} = $self->{api_key_in}{$token_name};
131             }
132              
133 0           return $tokens;
134             }
135              
136             sub clear_tokens {
137 0     0 0   my $self = shift;
138 0           my %tokens = %{ $self->get_tokens }; # copy
  0            
139              
140 0           $self->{username} = '';
141 0           $self->{password} = '';
142 0           $self->{access_token} = '';
143              
144 0           $self->{api_key} = {};
145 0           $self->{api_key_prefix} = {};
146 0           $self->{api_key_in} = {};
147              
148 0           return \%tokens;
149             }
150              
151             sub accept_tokens {
152 0     0 0   my ( $self, $tokens ) = @_;
153              
154 0           foreach my $known_name (qw(username password access_token)) {
155 0 0         next unless $tokens->{$known_name};
156 0           $self->{$known_name} = delete $tokens->{$known_name};
157             }
158              
159 0           foreach my $token_name ( keys %$tokens ) {
160 0           $self->{api_key}{$token_name} = $tokens->{$token_name}{token};
161 0 0         if ( $tokens->{$token_name}{prefix} ) {
162             $self->{api_key_prefix}{$token_name} =
163 0           $tokens->{$token_name}{prefix};
164             }
165 0   0       my $in = $tokens->{$token_name}->{in} || 'head';
166 0 0         croak "Tokens can only go in 'head' or 'query' (not in '$in')"
167             unless $in =~ /^(?:head|query)$/;
168 0           $self->{api_key_in}{$token_name} = $in;
169             }
170             }
171              
172             1;