| 1 | =begin comment | ||
| 2 | |||
| 3 | QueryBuilderApi | ||
| 4 | |||
| 5 | Active Query Builder Web API lets create, analyze and modify SQL queries for different database servers using RESTful HTTP requests to a cloud-based service. It requires SQL execution context (information about database schema and used database server) to be stored under the registered account at https://webapi.activequerybuilder.com/. | ||
| 6 | |||
| 7 | OpenAPI spec version: 1.1.8 | ||
| 8 | Contact: support@activedbsoft.com | ||
| 9 | Generated by: https://github.com/swagger-api/swagger-codegen.git | ||
| 10 | |||
| 11 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 12 | you may not use this file except in compliance with the License. | ||
| 13 | You may obtain a copy of the License at | ||
| 14 | |||
| 15 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 16 | |||
| 17 | Unless required by applicable law or agreed to in writing, software | ||
| 18 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 20 | See the License for the specific language governing permissions and | ||
| 21 | limitations under the License. | ||
| 22 | |||
| 23 | =end comment | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | # | ||
| 28 | # NOTE: This class is auto generated by the swagger code generator program. | ||
| 29 | # Do not edit the class manually. | ||
| 30 | # Ref: https://github.com/swagger-api/swagger-codegen | ||
| 31 | # | ||
| 32 | package SQL::QueryBuilder::WebApi::ApiClient; | ||
| 33 | |||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | use utf8; | ||
| 37 | |||
| 38 | use MIME::Base64; | ||
| 39 | use LWP::UserAgent; | ||
| 40 | use HTTP::Headers; | ||
| 41 | use HTTP::Response; | ||
| 42 | use HTTP::Request::Common qw(DELETE POST GET HEAD PUT); | ||
| 43 | use HTTP::Status; | ||
| 44 | use URI::Query; | ||
| 45 | use JSON; | ||
| 46 | use URI::Escape; | ||
| 47 | use Scalar::Util; | ||
| 48 | use Log::Any qw($log); | ||
| 49 | use Carp; | ||
| 50 | use Module::Runtime qw(use_module); | ||
| 51 | |||
| 52 | use SQL::QueryBuilder::WebApi::Configuration; | ||
| 53 | |||
| 54 | use base 'Class::Singleton'; | ||
| 55 | |||
| 56 | sub _new_instance | ||
| 57 | { | ||
| 58 | my $class = shift; | ||
| 59 | my (%args) = ( | ||
| 60 | 'ua' => LWP::UserAgent->new, | ||
| 61 | 'base_url' => 'https://webapi.activequerybuilder.com', | ||
| 62 | @_ | ||
| 63 | ); | ||
| 64 | |||
| 65 | return bless \%args, $class; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _cfg {'SQL::QueryBuilder::WebApi::Configuration'} | ||
| 69 | |||
| 70 | # Set the user agent of the API client | ||
| 71 | # | ||
| 72 | # @param string $user_agent The user agent of the API client | ||
| 73 | # | ||
| 74 | sub set_user_agent { | ||
| 75 | my ($self, $user_agent) = @_; | ||
| 76 | $self->{http_user_agent}= $user_agent; | ||
| 77 | } | ||
| 78 | |||
| 79 | # Set timeout | ||
| 80 | # | ||
| 81 | # @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] | ||
| 82 | # | ||
| 83 | sub set_timeout { | ||
| 84 | my ($self, $seconds) = @_; | ||
| 85 | if (!looks_like_number($seconds)) { | ||
| 86 | croak('Timeout variable must be numeric.'); | ||
| 87 | } | ||
| 88 | $self->{http_timeout} = $seconds; | ||
| 89 | } | ||
| 90 | |||
| 91 | # make the HTTP request | ||
| 92 | # @param string $resourcePath path to method endpoint | ||
| 93 | # @param string $method method to call | ||
| 94 | # @param array $queryParams parameters to be place in query URL | ||
| 95 | # @param array $postData parameters to be placed in POST body | ||
| 96 | # @param array $headerParams parameters to be place in request header | ||
| 97 | # @return mixed | ||
| 98 | sub call_api { | ||
| 99 | my $self = shift; | ||
| 100 | my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings) = @_; | ||
| 101 | |||
| 102 | # update parameters based on authentication settings | ||
| 103 | $self->update_params_for_auth($header_params, $query_params, $auth_settings); | ||
| 104 | |||
| 105 | |||
| 106 | my $_url = $self->{base_url} . $resource_path; | ||
| 107 | |||
| 108 | # build query | ||
| 109 | if (%$query_params) { | ||
| 110 | $_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify }); | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | # body data | ||
| 115 | $body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string | ||
| 116 | my $_body_data = %$post_params ? $post_params : $body_data; | ||
| 117 | |||
| 118 | # Make the HTTP request | ||
| 119 | my $_request; | ||
| 120 | if ($method eq 'POST') { | ||
| 121 | # multipart | ||
| 122 | $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? | ||
| 123 | 'form-data' : $header_params->{'Content-Type'}; | ||
| 124 | |||
| 125 | $_request = POST($_url, %$header_params, Content => $_body_data); | ||
| 126 | |||
| 127 | } | ||
| 128 | elsif ($method eq 'PUT') { | ||
| 129 | # multipart | ||
| 130 | $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? | ||
| 131 | 'form-data' : $header_params->{'Content-Type'}; | ||
| 132 | |||
| 133 | $_request = PUT($_url, %$header_params, Content => $_body_data); | ||
| 134 | |||
| 135 | } | ||
| 136 | elsif ($method eq 'GET') { | ||
| 137 | my $headers = HTTP::Headers->new(%$header_params); | ||
| 138 | $_request = GET($_url, %$header_params); | ||
| 139 | } | ||
| 140 | elsif ($method eq 'HEAD') { | ||
| 141 | my $headers = HTTP::Headers->new(%$header_params); | ||
| 142 | $_request = HEAD($_url,%$header_params); | ||
| 143 | } | ||
| 144 | elsif ($method eq 'DELETE') { #TODO support form data | ||
| 145 | my $headers = HTTP::Headers->new(%$header_params); | ||
| 146 | $_request = DELETE($_url, %$headers); | ||
| 147 | } | ||
| 148 | elsif ($method eq 'PATCH') { #TODO | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | } | ||
| 152 | |||
| 153 | $self->{ua}->timeout($self->{http_timeout} || $SQL::QueryBuilder::WebApi::Configuration::http_timeout); | ||
| 154 | $self->{ua}->agent($self->{http_user_agent} || $SQL::QueryBuilder::WebApi::Configuration::http_user_agent); | ||
| 155 | |||
| 156 | $log->debugf("REQUEST: %s", $_request->as_string); | ||
| 157 | my $_response = $self->{ua}->request($_request); | ||
| 158 | $log->debugf("RESPONSE: %s", $_response->as_string); | ||
| 159 | |||
| 160 | unless ($_response->is_success) { | ||
| 161 | croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content); | ||
| 162 | } | ||
| 163 | |||
| 164 | return $_response->content; | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | # Take value and turn it into a string suitable for inclusion in | ||
| 169 | # the path, by url-encoding. | ||
| 170 | # @param string $value a string which will be part of the path | ||
| 171 | # @return string the serialized object | ||
| 172 | sub to_path_value { | ||
| 173 | my ($self, $value) = @_; | ||
| 174 | return uri_escape($self->to_string($value)); | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | # Take value and turn it into a string suitable for inclusion in | ||
| 179 | # the query, by imploding comma-separated if it's an object. | ||
| 180 | # If it's a string, pass through unchanged. It will be url-encoded | ||
| 181 | # later. | ||
| 182 | # @param object $object an object to be serialized to a string | ||
| 183 | # @return string the serialized object | ||
| 184 | sub to_query_value { | ||
| 185 | my ($self, $object) = @_; | ||
| 186 | if (ref($object) eq 'ARRAY') { | ||
| 187 | return join(',', @$object); | ||
| 188 | } else { | ||
| 189 | return $self->to_string($object); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | # Take value and turn it into a string suitable for inclusion in | ||
| 195 | # the header. If it's a string, pass through unchanged | ||
| 196 | # If it's a datetime object, format it in ISO8601 | ||
| 197 | # @param string $value a string which will be part of the header | ||
| 198 | # @return string the header string | ||
| 199 | sub to_header_value { | ||
| 200 | my ($self, $value) = @_; | ||
| 201 | return $self->to_string($value); | ||
| 202 | } | ||
| 203 | |||
| 204 | # Take value and turn it into a string suitable for inclusion in | ||
| 205 | # the http body (form parameter). If it's a string, pass through unchanged | ||
| 206 | # If it's a datetime object, format it in ISO8601 | ||
| 207 | # @param string $value the value of the form parameter | ||
| 208 | # @return string the form string | ||
| 209 | sub to_form_value { | ||
| 210 | my ($self, $value) = @_; | ||
| 211 | return $self->to_string($value); | ||
| 212 | } | ||
| 213 | |||
| 214 | # Take value and turn it into a string suitable for inclusion in | ||
| 215 | # the parameter. If it's a string, pass through unchanged | ||
| 216 | # If it's a datetime object, format it in ISO8601 | ||
| 217 | # @param string $value the value of the parameter | ||
| 218 | # @return string the header string | ||
| 219 | sub to_string { | ||
| 220 | my ($self, $value) = @_; | ||
| 221 | if (ref($value) eq "DateTime") { # datetime in ISO8601 format | ||
| 222 | return $value->datetime(); | ||
| 223 | } | ||
| 224 | else { | ||
| 225 | return $value; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | # Deserialize a JSON string into an object | ||
| 230 | # | ||
| 231 | # @param string $class class name is passed as a string | ||
| 232 | # @param string $data data of the body | ||
| 233 | # @return object an instance of $class | ||
| 234 | sub deserialize | ||
| 235 | { | ||
| 236 | my ($self, $class, $data) = @_; | ||
| 237 | $log->debugf("deserializing %s for %s", $data, $class); | ||
| 238 | |||
| 239 | if (not defined $data) { | ||
| 240 | return undef; | ||
| 241 | } elsif ( (substr($class, 0, 5)) eq 'HASH[') { #hash | ||
| 242 | if ($class =~ /^HASH\[(.*),(.*)\]$/) { | ||
| 243 | my ($key_type, $type) = ($1, $2); | ||
| 244 | my %hash; | ||
| 245 | my $decoded_data = decode_json $data; | ||
| 246 | foreach my $key (keys %$decoded_data) { | ||
| 247 | if (ref $decoded_data->{$key} eq 'HASH') { | ||
| 248 | $hash{$key} = $self->deserialize($type, encode_json $decoded_data->{$key}); | ||
| 249 | } else { | ||
| 250 | $hash{$key} = $self->deserialize($type, $decoded_data->{$key}); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | return \%hash; | ||
| 254 | } else { | ||
| 255 | #TODO log error | ||
| 256 | } | ||
| 257 | |||
| 258 | } elsif ( (substr($class, 0, 6)) eq 'ARRAY[' ) { # array of data | ||
| 259 | return $data if $data eq '[]'; # return if empty array | ||
| 260 | |||
| 261 | my $_sub_class = substr($class, 6, -1); | ||
| 262 | my $_json_data = decode_json $data; | ||
| 263 | my @_values = (); | ||
| 264 | foreach my $_value (@$_json_data) { | ||
| 265 | if (ref $_value eq 'ARRAY') { | ||
| 266 | push @_values, $self->deserialize($_sub_class, encode_json $_value); | ||
| 267 | } else { | ||
| 268 | push @_values, $self->deserialize($_sub_class, $_value); | ||
| 269 | } | ||
| 270 | } | ||
| 271 | return \@_values; | ||
| 272 | } elsif ($class eq 'DateTime') { | ||
| 273 | return DateTime->from_epoch(epoch => str2time($data)); | ||
| 274 | } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) { | ||
| 275 | return $data; | ||
| 276 | } else { # model | ||
| 277 | my $_instance = use_module("SQL::QueryBuilder::WebApi::Object::$class")->new; | ||
| 278 | if (ref $data eq "HASH") { | ||
| 279 | return $_instance->from_hash($data); | ||
| 280 | } else { # string, need to json decode first | ||
| 281 | return $_instance->from_hash(decode_json $data); | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | } | ||
| 286 | |||
| 287 | # return 'Accept' based on an array of accept provided | ||
| 288 | # @param [Array] header_accept_array Array fo 'Accept' | ||
| 289 | # @return String Accept (e.g. application/json) | ||
| 290 | sub select_header_accept | ||
| 291 | { | ||
| 292 | my ($self, @header) = @_; | ||
| 293 | |||
| 294 | if (@header == 0 || (@header == 1 && $header[0] eq '')) { | ||
| 295 | return undef; | ||
| 296 | } elsif (grep(/^application\/json$/i, @header)) { | ||
| 297 | return 'application/json'; | ||
| 298 | } else { | ||
| 299 | return join(',', @header); | ||
| 300 | } | ||
| 301 | |||
| 302 | } | ||
| 303 | |||
| 304 | # return the content type based on an array of content-type provided | ||
| 305 | # @param [Array] content_type_array Array fo content-type | ||
| 306 | # @return String Content-Type (e.g. application/json) | ||
| 307 | sub select_header_content_type | ||
| 308 | { | ||
| 309 | my ($self, @header) = @_; | ||
| 310 | |||
| 311 | if (@header == 0 || (@header == 1 && $header[0] eq '')) { | ||
| 312 | return 'application/json'; # default to application/json | ||
| 313 | } elsif (grep(/^application\/json$/i, @header)) { | ||
| 314 | return 'application/json'; | ||
| 315 | } else { | ||
| 316 | return join(',', @header); | ||
| 317 | } | ||
| 318 | |||
| 319 | } | ||
| 320 | |||
| 321 | # Get API key (with prefix if set) | ||
| 322 | # @param string key name | ||
| 323 | # @return string API key with the prefix | ||
| 324 | sub get_api_key_with_prefix | ||
| 325 | { | ||
| 326 | my ($self, $key_name) = @_; | ||
| 327 | |||
| 328 | my $api_key = $SQL::QueryBuilder::WebApi::Configuration::api_key->{$key_name}; | ||
| 329 | |||
| 330 | return unless $api_key; | ||
| 331 | |||
| 332 | my $prefix = $SQL::QueryBuilder::WebApi::Configuration::api_key_prefix->{$key_name}; | ||
| 333 | return $prefix ? "$prefix $api_key" : $api_key; | ||
| 334 | } | ||
| 335 | |||
| 336 | # update header and query param based on authentication setting | ||
| 337 | # | ||
| 338 | # @param array $headerParams header parameters (by ref) | ||
| 339 | # @param array $queryParams query parameters (by ref) | ||
| 340 | # @param array $authSettings array of authentication scheme (e.g ['api_key']) | ||
| 341 | sub update_params_for_auth { | ||
| 342 | my ($self, $header_params, $query_params, $auth_settings) = @_; | ||
| 343 | |||
| 344 | return $self->_global_auth_setup($header_params, $query_params) | ||
| 345 | unless $auth_settings && @$auth_settings; | ||
| 346 | |||
| 347 | # one endpoint can have more than 1 auth settings | ||
| 348 | foreach my $auth (@$auth_settings) { | ||
| 349 | # determine which one to use | ||
| 350 | if (!defined($auth)) { | ||
| 351 | # TODO show warning about auth setting not defined | ||
| 352 | } | ||
| 353 | else { | ||
| 354 | # TODO show warning about security definition not found | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | # The endpoint API class has not found any settings for auth. This may be deliberate, | ||
| 360 | # in which case update_params_for_auth() will be a no-op. But it may also be that the | ||
| 361 | # OpenAPI Spec does not describe the intended authorization. So we check in the config for any | ||
| 362 | # auth tokens and if we find any, we use them for all endpoints; | ||
| 363 | sub _global_auth_setup { | ||
| 364 | my ($self, $header_params, $query_params) = @_; | ||
| 365 | |||
| 366 | my $tokens = $self->_cfg->get_tokens; | ||
| 367 | return unless keys %$tokens; | ||
| 368 | |||
| 369 | # basic | ||
| 370 | if (my $uname = delete $tokens->{username}) { | ||
| 371 | my $pword = delete $tokens->{password}; | ||
| 372 | $header_params->{'Authorization'} = 'Basic '.encode_base64($uname.":".$pword); | ||
| 373 | } | ||
| 374 | |||
| 375 | # oauth | ||
| 376 | if (my $access_token = delete $tokens->{access_token}) { | ||
| 377 | $header_params->{'Authorization'} = 'Bearer ' . $access_token; | ||
| 378 | } | ||
| 379 | |||
| 380 | # other keys | ||
| 381 | foreach my $token_name (keys %$tokens) { | ||
| 382 | my $in = $tokens->{$token_name}->{in}; | ||
| 383 | my $token = $self->get_api_key_with_prefix($token_name); | ||
| 384 | if ($in eq 'head') { | ||
| 385 | $header_params->{$token_name} = $token; | ||
| 386 | } | ||
| 387 | elsif ($in eq 'query') { | ||
| 388 | $query_params->{$token_name} = $token; | ||
| 389 | } | ||
| 390 | else { | ||
| 391 | die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')"; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | |||
| 397 | 1; |