| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  |  | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Base class for all entity classes | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | use v5.24; | 
| 5 | 2 |  |  | 2 |  | 1126 |  | 
|  | 2 |  |  |  |  | 8 |  | 
| 6 |  |  |  |  |  |  | use strict; | 
| 7 | 2 |  |  | 2 |  | 12 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 44 |  | 
| 8 | 2 |  |  | 2 |  | 8 |  | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 50 |  | 
| 9 |  |  |  |  |  |  | use Carp; | 
| 10 | 2 |  |  | 2 |  | 8 | use Moo; | 
|  | 2 |  |  |  |  | 24 |  | 
|  | 2 |  |  |  |  | 100 |  | 
| 11 | 2 |  |  | 2 |  | 10 | use Params::ValidationCompiler qw(validation_for); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 12 |  | 
| 12 | 2 |  |  | 2 |  | 629 | use Types::Mojo qw(:all); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 77 |  | 
| 13 | 2 |  |  | 2 |  | 11 | use Types::Standard qw(Str Object Int); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 18 |  | 
| 14 | 2 |  |  | 2 |  | 6928 |  | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 14 |  | 
| 15 |  |  |  |  |  |  | use feature 'signatures'; | 
| 16 | 2 |  |  | 2 |  | 1785 | no warnings 'experimental::signatures'; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 158 |  | 
| 17 | 2 |  |  | 2 |  | 10 |  | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 757 |  | 
| 18 |  |  |  |  |  |  | our $VERSION = '1.0.1'; # VERSION | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | has api      => ( is => 'ro', isa => Object, required => 1 ); | 
| 21 |  |  |  |  |  |  | has json_rpc => ( is => 'ro', isa => Str, default => sub { '2.0' } ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | state $request_id = 1; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | my $rpc_data = { | 
| 26 | 1 |  |  | 1 |  | 3 | jsonrpc => $self->json_rpc, | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 2 |  | 
| 27 | 1 |  |  |  |  | 8 | id      => $request_id++, | 
| 28 |  |  |  |  |  |  | method  => $method, | 
| 29 |  |  |  |  |  |  | }; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | $rpc_data->{params} = $params->%* ? $params : ""; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 1 | 50 |  |  |  | 5 | my $api = $self->api; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 1 |  |  |  |  | 5 | if ( $opts->{needs_auth} && !$api->token ) { | 
| 36 |  |  |  |  |  |  | my $auth_result = $api->base->auth( | 
| 37 | 1 | 50 | 33 |  |  | 4 | user => $api->user, | 
| 38 | 0 |  |  |  |  | 0 | pass => $api->password, | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | my $token = ref $auth_result ? | 
| 42 |  |  |  |  |  |  | $auth_result->{session} : | 
| 43 |  |  |  |  |  |  | croak 'Could not login: ' . $auth_result; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 | 0 |  |  |  | 0 | $api->_set_token( $token ); | 
| 46 |  |  |  |  |  |  | } | 
| 47 | 0 |  |  |  |  | 0 |  | 
| 48 |  |  |  |  |  |  | my %header = ( 'Content-Type' => 'application/json' ); | 
| 49 |  |  |  |  |  |  | $header{'HPLS-AUTH'} = $api->token if $api->token; | 
| 50 | 1 |  |  |  |  | 4 |  | 
| 51 | 1 | 50 |  |  |  | 6 | my $uri = join '/', | 
| 52 |  |  |  |  |  |  | $api->host, | 
| 53 | 1 |  |  |  |  | 8 | $api->base_uri; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | my $tx = $api->client->post( | 
| 56 |  |  |  |  |  |  | $uri, | 
| 57 | 1 |  |  |  |  | 427 | \%header, | 
| 58 |  |  |  |  |  |  | json => $rpc_data, | 
| 59 |  |  |  |  |  |  | ); | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | my $response = $tx->res; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 1 |  |  |  |  | 1858449 | if ( $tx->error ) { | 
| 64 |  |  |  |  |  |  | carp $tx->error->{message}; | 
| 65 | 1 | 50 |  |  |  | 6 | return; | 
| 66 | 0 |  |  |  |  | 0 | } | 
| 67 | 0 |  |  |  |  | 0 |  | 
| 68 |  |  |  |  |  |  | my $data = $response->json; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 1 |  |  |  |  | 20 | if ( $data->{error} ) { | 
| 71 |  |  |  |  |  |  | carp $data->{error}->{message}; | 
| 72 | 1 | 50 |  |  |  | 238 | return; | 
| 73 | 0 |  |  |  |  | 0 | } | 
| 74 | 0 |  |  |  |  | 0 |  | 
| 75 |  |  |  |  |  |  | return $data->{result}; | 
| 76 |  |  |  |  |  |  | } | 
| 77 | 1 |  |  |  |  | 10 |  | 
| 78 |  |  |  |  |  |  | 1; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =pod | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =encoding UTF-8 | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =head1 NAME | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | API::MailboxOrg::APIBase - Base class for all entity classes | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head1 VERSION | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | version 1.0.1 | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =over 4 | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =item * json_rpc | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | I<(optional)> The version of JSON-RPC used. Defaults to C<2.0>. | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =item * api | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | I<mandatory> An L<API::MailboxOrg> object. | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =back | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =head1 METHODS | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =head2 request | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | This method builds the API request. If a method is called that needs authentification | 
| 112 |  |  |  |  |  |  | and there's no session, then the C<auth> method is called. | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =head1 AUTHOR | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | Renee Baecker <reneeb@cpan.org> | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | This software is Copyright (c) 2022 by Renee Baecker. | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | This is free software, licensed under: | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | The Artistic License 2.0 (GPL Compatible) | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =cut |