| 1 | package WebService::Auth0::HTTPClient; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | use URI; | ||
| 5 | use HTTP::Request::Common (); | ||
| 6 | use JSON::MaybeXS (); | ||
| 7 | |||
| 8 | has domain => ( | ||
| 9 | is=>'ro', | ||
| 10 | required=>1 ); | ||
| 11 | |||
| 12 | has ua => ( | ||
| 13 | is=>'bare', | ||
| 14 | handles=>['request'], | ||
| 15 | required=>1 ); | ||
| 16 | |||
| 17 | sub GET { shift->request(HTTP::Request::Common::GET @_) } | ||
| 18 | sub POST { shift->request(HTTP::Request::Common::POST @_) } | ||
| 19 | sub PUT { shift->request(HTTP::Request::Common::PUT @_) } | ||
| 20 | sub DELETE { shift->request(HTTP::Request::Common::DELETE @_) } | ||
| 21 | sub PATCH { shift->request(HTTP::Request::Common::request_type_with_data('PATCH', @_)) } | ||
| 22 | |||
| 23 | sub encode_json { shift; JSON::MaybeXS::encode_json(shift) } | ||
| 24 | |||
| 25 | sub _prepare_json_req { | ||
| 26 | my ($self, $uri, $json) = @_; | ||
| 27 | return ( $uri, | ||
| 28 | 'content-type' => 'application/json', | ||
| 29 | Content => $self->encode_json($json) ); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub POST_JSON { | ||
| 33 | my $self = shift; | ||
| 34 | return $self->POST($self->_prepare_json_req(@_)); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub PUT_JSON { | ||
| 38 | my $self = shift; | ||
| 39 | return $self->PUT($self->_prepare_json_req(@_)); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub PATCH_JSON { | ||
| 43 | my $self = shift; | ||
| 44 | return $self->PATCH($self->_prepare_json_req(@_)); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub uri_for { | ||
| 48 | my $self = shift; | ||
| 49 | my @query = (); | ||
| 50 | if(my $type = ref($_[-1]||'')) { | ||
| 51 | if($type eq 'ARRAY') { | ||
| 52 | @query = @{pop(@_)}; | ||
| 53 | } elsif($type eq 'HASH') { | ||
| 54 | @query = %{pop(@_)}; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | @query = map { ref($_)||'' eq 'ARRAY' ? join(',', @$_): $_ } @query; | ||
| 58 | my $uri = URI->new("https://${\$self->domain}/"); | ||
| 59 | $uri->path_segments(@_); | ||
| 60 | $uri->query_form(@query) if @query; | ||
| 61 | return $uri; | ||
| 62 | } | ||
| 63 | |||
| 64 | =head1 NAME | ||
| 65 | |||
| 66 | WebService::Auth0::HTTPClient - Common HTTP Client methods | ||
| 67 | |||
| 68 | =head1 SYNOPSIS | ||
| 69 | |||
| 70 | N/A | ||
| 71 | |||
| 72 | =head1 DESCRIPTION | ||
| 73 | |||
| 74 | A Base class for managment API endpoints | ||
| 75 | |||
| 76 | =head1 METHODS | ||
| 77 | |||
| 78 | This class defines the following methods: | ||
| 79 | |||
| 80 | =head2 request | ||
| 81 | |||
| 82 | =head2 GET | ||
| 83 | |||
| 84 | =head2 POST | ||
| 85 | |||
| 86 | =head2 POST_JSON | ||
| 87 | |||
| 88 | =head2 DELETE | ||
| 89 | |||
| 90 | =head2 PATCH | ||
| 91 | |||
| 92 | =head2 PATCH_JSON | ||
| 93 | |||
| 94 | =head2 uri_for | ||
| 95 | |||
| 96 | =head1 ATTRIBUTES | ||
| 97 | |||
| 98 | This class defines the following attributes: | ||
| 99 | |||
| 100 | =head2 ua | ||
| 101 | |||
| 102 | =head1 SEE ALSO | ||
| 103 | |||
| 104 | L<WebService::Auth0>, L<https://auth0.com>. | ||
| 105 | |||
| 106 | =head1 AUTHOR | ||
| 107 | |||
| 108 | See L<WebService::Auth0> | ||
| 109 | |||
| 110 | =head1 COPYRIGHT & LICENSE | ||
| 111 | |||
| 112 | See L<WebService::Auth0> | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | 1; |