File Coverage

blib/lib/WebService/Auth0/Management/Base.pm
Criterion Covered Total %
statement 12 46 26.0
branch 0 10 0.0
condition 0 4 0.0
subroutine 4 16 25.0
pod 8 11 72.7
total 24 87 27.5


line stmt bran cond sub pod time code
1             package WebService::Auth0::Management::Base;
2              
3 3     3   1595 use Moo;
  3         7  
  3         16  
4 3     3   2515 use URI;
  3         14081  
  3         87  
5 3     3   1514 use HTTP::Request::Common ();
  3         54947  
  3         79  
6 3     3   1362 use JSON::MaybeXS ();
  3         16749  
  3         1874  
7              
8             has domain => (
9             is=>'ro',
10             required=>1 );
11              
12             has token => (
13             is=>'ro',
14             required=>1 );
15              
16             has ua => (
17             is=>'ro',
18             required=>1 );
19              
20             has mgmt_path_parts => (
21             is=>'ro',
22             required=>1,
23             isa=>sub { ref($_) eq 'ARRAY' },
24             default=>sub {['api', 'v2']} );
25              
26             sub request {
27 0     0 1   my ($self, $request) = @_;
28 0           $request->push_header(Authorization => "Bearer ${\$self->token}");
  0            
29 0           return $self->ua->request($request);
30             }
31              
32 0     0 1   sub GET { shift->request(HTTP::Request::Common::GET @_) }
33 0     0 1   sub POST { shift->request(HTTP::Request::Common::POST @_) }
34 0     0 0   sub PUT { shift->request(HTTP::Request::Common::PUT @_) }
35 0     0     sub DELETE { shift->request(HTTP::Request::Common::DELETE @_) }
36 0     0 1   sub PATCH { shift->request(HTTP::Request::Common::request_type_with_data('PATCH', @_)) }
37              
38 0     0 0   sub encode_json { shift; JSON::MaybeXS::encode_json(shift) }
  0            
39              
40             sub POST_JSON {
41 0     0 1   my ($self, $uri, $json) = @_;
42 0           return $self->POST( $uri,
43             'content-type' => 'application/json',
44             Content => $self->encode_json($json));
45             }
46              
47             sub PUT_JSON {
48 0     0 0   my ($self, $uri, $json) = @_;
49 0           return $self->PUT( $uri,
50             'content-type' => 'application/json',
51             Content => $self->encode_json($json));
52             }
53              
54             sub PATCH_JSON {
55 0     0 1   my ($self, $uri, $json) = @_;
56 0           return $self->PATCH( $uri,
57             'content-type' => 'application/json',
58             Content => $self->encode_json($json));
59             }
60              
61             sub uri_for {
62 0     0 1   my $self = shift;
63 0           my @query = ();
64 0 0 0       if(my $type = ref($_[-1]||'')) {
65 0 0         if($type eq 'ARRAY') {
    0          
66 0           @query = @{pop(@_)};
  0            
67             } elsif($type eq 'HASH') {
68 0           @query = %{pop(@_)};
  0            
69             }
70             }
71 0 0 0       @query = map { ref($_)||'' eq 'ARRAY' ? join(',', @$_): $_ } @query;
  0            
72 0           my $uri = URI->new("https://${\$self->domain}/");
  0            
73 0           $uri->path_segments(@{$self->mgmt_path_parts}, $self->path_suffix, @_);
  0            
74 0 0         $uri->query_form(@query) if @query;
75 0           return $uri;
76             }
77              
78 0     0 1   sub path_suffix { die "Extending class must override" }
79              
80             =head1 NAME
81              
82             WebService::Auth0::Management::Base - Base class for the Management API
83              
84             =head1 SYNOPSIS
85              
86             package WebService::Auth0::Management::Users;
87              
88             use Moo;
89             extends 'WebService::Auth0::Management::Base';
90             with 'WebService::Auth0::Management::Role::All',
91             'WebService::Auth0::Management::Role::Search',
92             'WebService::Auth0::Management::Role::Create',
93             'WebService::Auth0::Management::Role::Get',
94             'WebService::Auth0::Management::Role::Update',
95             'WebService::Auth0::Management::Role::Delete'; # Optional helpers
96              
97             sub path_suffix { 'users' } # You need to override this
98              
99             # Other custom methods for the endpoint.
100              
101             1;
102              
103             =head1 DESCRIPTION
104              
105             A Base class for managment API endpoints
106              
107             =head1 METHODS
108              
109             This class defines the following methods:
110              
111             =head2 request
112              
113             =head2 GET
114              
115             =head2 POST
116              
117             =head2 POST_JSON
118              
119             =head2 DELETE
120              
121             =head2 PATCH
122              
123             =head2 PATCH_JSON
124              
125             =head2 uri_for
126              
127             =head2 path_suffix
128              
129             =head1 ATTRIBUTES
130              
131             This class defines the following attributes:
132              
133             =head2 domain
134              
135             =head2 token
136              
137             =head2 ua
138              
139             =head2 mgmt_path_parts
140              
141             =head1 SEE ALSO
142            
143             L, L.
144              
145             =head1 AUTHOR
146            
147             See L
148            
149             =head1 COPYRIGHT & LICENSE
150            
151             See L
152              
153             =cut
154              
155             1;