File Coverage

blib/lib/WebService/Mattermost/V4/API/Request.pm
Criterion Covered Total %
statement 25 25 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 5 5 100.0
pod n/a
total 37 39 94.8


line stmt bran cond sub pod time code
1             package WebService::Mattermost::V4::API::Request;
2:

3: # ABSTRACT: A request to be sent to the Mattermost API. 4:
5: use Mojo::URL;
6: use Mojo::Util 'url_escape';
7: use Moo;
8: use Types::Standard qw(Any ArrayRef Bool Enum InstanceOf Str);
9:
10: with 'WebService::Mattermost::Role::Logger';
11:
12: ################################################################################
13:
14: has base_url => (is => 'ro', isa => Str, required => 1);
15: has endpoint => (is => 'ro', isa => Str, required => 1);
16: has method => (is => 'ro', isa => Enum [ qw(DELETE GET POST PUT) ], required => 1);
17: has resource => (is => 'ro', isa => Str, required => 1);
18:
19: # Some endpoints require parameters as a HashRef, some as an ArrayRef
20: has debug => (is => 'ro', isa => Bool, default => 0);
21: has ids => (is => 'ro', isa => ArrayRef, default => sub { [] });
22: has parameters => (is => 'ro', isa => Any, default => sub { {} });
23:
24: has url => (is => 'ro', isa => InstanceOf['Mojo::URL'], lazy => 1, builder => 1);
25:
26: ################################################################################
27:
28: sub _build_url {
29: my $self = shift;
30:
31: my $base_url = $self->base_url;
32: my $resource = $self->resource;
33: my $endpoint = $self->endpoint;
34:
35: $base_url .= '/' if $base_url !~ /\/$/;
36: $resource .= '/' if $self->endpoint ne '' && $resource !~ /\/$/;
37:
38: my @ids = map { url_escape($_) } @{$self->ids};
39:
40: $endpoint = sprintf($endpoint, @ids);
41:
42: my $url = sprintf('%s%s%s', $base_url, $resource, $endpoint);
43:
44: $self->logger->debug($url) if $self->debug;
45:
46: return Mojo::URL->new($url);
47: }
48:
49: ################################################################################
50:
51: 1;
52:
53: __END__
54:
55: =pod
56:
57: =encoding UTF-8
58:
59: =head1 NAME
60:
61: WebService::Mattermost::V4::API::Request - A request to be sent to the Mattermost API.
62:
63: =head1 VERSION
64:
65: version 0.28
66:
67: =head1 DESCRIPTION
68:
69: A request to be sent to the Mattermost API.
70:
71: =head2 USAGE
72:
73: See L<WebService::Mattermost::V4::API::Resource::_call()>.
74:
75: =head2 ATTRIBUTES
76:
77: =over 4
78:
79: =item C<base_url>
80:
81: =item C<endpoint>
82:
83: =item C<method>
84:
85: HTTP method.
86:
87: =item C<resource>
88:
89: The API endpoint's namespace.
90:
91: =item C<parameters>
92:
93: Data to be sent to the API.
94:
95: =item C<ids>
96:
97: IDs to replace into the URL with C<sprintf>.
98:
99: =item C<url>
100:
101: =back
102:
103: =head1 AUTHOR
104:
105: Mike Jones <mike@netsplit.org.uk>
106:
107: =head1 COPYRIGHT AND LICENSE
108:
109: This software is Copyright (c) 2020 by Mike Jones.
110:
111: This is free software, licensed under:
112:
113: The MIT (X11) License
114:
115: =cut
116: