| 1 | package WWW::Asana::Request; | ||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | 1 | $WWW::Asana::Request::AUTHORITY = 'cpan:GETTY'; | |||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||||
| 6 | 1 | $WWW::Asana::Request::VERSION = '0.003'; | |||||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Asana Request Class | ||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use MooX qw( | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | +HTTP::Request | ||||||||||||||||||||||||||||||
| 12 | +JSON | ||||||||||||||||||||||||||||||
| 13 | +URI | ||||||||||||||||||||||||||||||
| 14 | +URI::QueryParam | ||||||||||||||||||||||||||||||
| 15 | 1 | ); | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 1 | has api_key => ( | |||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||||
| 19 | required => 1, | ||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 1 | has to => ( | |||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||
| 24 | required => 1, | ||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | 1 | has to_type => ( | |||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||||
| 29 | lazy => 1, | ||||||||||||||||||||||||||||||
| 30 | builder => 1, | ||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub _build_to_type { | ||||||||||||||||||||||||||||
|
Subroutine
_build_to_type: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 34 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ( $self->to =~ /\[(\w+)\]/ ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 36 | 0 | return $1; | |||||||||||||||||||||||||||||
| 37 | } else { | ||||||||||||||||||||||||||||||
| 38 | 0 | return $self->to; | |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | 1 | has to_multi => ( | |||||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||||
| 44 | lazy => 1, | ||||||||||||||||||||||||||||||
| 45 | builder => 1, | ||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub _build_to_multi { | ||||||||||||||||||||||||||||
|
Subroutine
_build_to_multi: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 49 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ( $self->to =~ /\[(\w+)\]/ ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 51 | 0 | return 1; | |||||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||||
| 53 | 0 | return 0; | |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||
| 57 | 1 | has uri => ( | |||||||||||||||||||||||||||||
| 58 | is => 'ro', | ||||||||||||||||||||||||||||||
| 59 | required => 1, | ||||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | 1 | has data => ( | |||||||||||||||||||||||||||||
| 63 | is => 'ro', | ||||||||||||||||||||||||||||||
| 64 | predicate => 'has_data', | ||||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | 1 | has params => ( | |||||||||||||||||||||||||||||
| 68 | is => 'ro', | ||||||||||||||||||||||||||||||
| 69 | predicate => 'has_params', | ||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | 1 | has codes => ( | |||||||||||||||||||||||||||||
| 73 | is => 'ro', | ||||||||||||||||||||||||||||||
| 74 | predicate => 'has_codes', | ||||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | has method => ( | ||||||||||||||||||||||||||||||
| 78 | is => 'ro', | ||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | default => sub { 'GET' } | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 80 | 1 | ); | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | 1 | has _http_request => ( | |||||||||||||||||||||||||||||
| 83 | is => 'ro', | ||||||||||||||||||||||||||||||
| 84 | lazy => 1, | ||||||||||||||||||||||||||||||
| 85 | builder => 1, | ||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub http_request { shift->_http_request } | ||||||||||||||||||||||||||||
|
Subroutine
http_request: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | 1 | has json => ( | |||||||||||||||||||||||||||||
| 90 | is => 'ro', | ||||||||||||||||||||||||||||||
| 91 | lazy => 1, | ||||||||||||||||||||||||||||||
| 92 | builder => 1, | ||||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub _build_json { | ||||||||||||||||||||||||||||
|
Subroutine
_build_json: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 96 | 0 | my $json = JSON->new; | |||||||||||||||||||||||||||||
| 97 | 0 | $json->allow_nonref; | |||||||||||||||||||||||||||||
| 98 | 0 | return $json; | |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _build__http_request { | ||||||||||||||||||||||||||||
|
Subroutine
_build__http_request: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||||
| 102 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||||
| 103 | 0 | my %data; | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | %data = %{$self->data} if $self->has_data; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 105 | 0 | my @params; | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | @params = @{$self->params} if $self->has_params; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ($self->to_multi) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | 0 | my $type = $self->to_type; | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($type eq 'Task') { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 110 | 0 | push @params, [ opt_fields => join(',',qw( | |||||||||||||||||||||||||||||
| 111 | assignee | ||||||||||||||||||||||||||||||
| 112 | assignee_status | ||||||||||||||||||||||||||||||
| 113 | created_at | ||||||||||||||||||||||||||||||
| 114 | completed | ||||||||||||||||||||||||||||||
| 115 | completed_at | ||||||||||||||||||||||||||||||
| 116 | due_on | ||||||||||||||||||||||||||||||
| 117 | modified_at | ||||||||||||||||||||||||||||||
| 118 | name | ||||||||||||||||||||||||||||||
| 119 | notes | ||||||||||||||||||||||||||||||
| 120 | )) ]; | ||||||||||||||||||||||||||||||
| 121 | } elsif ($type eq 'Story') { | ||||||||||||||||||||||||||||||
| 122 | 0 | push @params, [ opt_fields => join(',',qw( | |||||||||||||||||||||||||||||
| 123 | created_at | ||||||||||||||||||||||||||||||
| 124 | created_by | ||||||||||||||||||||||||||||||
| 125 | text | ||||||||||||||||||||||||||||||
| 126 | target | ||||||||||||||||||||||||||||||
| 127 | source | ||||||||||||||||||||||||||||||
| 128 | type | ||||||||||||||||||||||||||||||
| 129 | )) ]; | ||||||||||||||||||||||||||||||
| 130 | } elsif ($type eq 'Project') { | ||||||||||||||||||||||||||||||
| 131 | 0 | push @params, [ opt_fields => join(',',qw( | |||||||||||||||||||||||||||||
| 132 | created_at | ||||||||||||||||||||||||||||||
| 133 | modified_at | ||||||||||||||||||||||||||||||
| 134 | name | ||||||||||||||||||||||||||||||
| 135 | notes | ||||||||||||||||||||||||||||||
| 136 | )) ]; | ||||||||||||||||||||||||||||||
| 137 | } elsif ($type eq 'Tag') { | ||||||||||||||||||||||||||||||
| 138 | 0 | push @params, [ opt_fields => join(',',qw( | |||||||||||||||||||||||||||||
| 139 | created_at | ||||||||||||||||||||||||||||||
| 140 | name | ||||||||||||||||||||||||||||||
| 141 | notes | ||||||||||||||||||||||||||||||
| 142 | )) ]; | ||||||||||||||||||||||||||||||
| 143 | } elsif ($type eq 'User') { | ||||||||||||||||||||||||||||||
| 144 | 0 | push @params, [ opt_fields => join(',',qw( | |||||||||||||||||||||||||||||
| 145 | name | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | )) ]; | ||||||||||||||||||||||||||||||
| 148 | } elsif ($type eq 'Workspace') { | ||||||||||||||||||||||||||||||
| 149 | 0 | push @params, [ opt_fields => join(',',qw( | |||||||||||||||||||||||||||||
| 150 | name | ||||||||||||||||||||||||||||||
| 151 | )) ]; | ||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($self->has_data) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 155 | 0 | $data{$_} = $self->data->{$_} for (keys %{$self->data}); | |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||
| 157 | 0 | my @headers; | |||||||||||||||||||||||||||||
| 158 | my $uri; | ||||||||||||||||||||||||||||||
| 159 | 0 | my $body; | |||||||||||||||||||||||||||||
| 160 | 0 | my $u = URI->new($self->uri); | |||||||||||||||||||||||||||||
| 161 | 0 | $u->query_param(@{$_}) for @params; | |||||||||||||||||||||||||||||
| 162 | 0 | $uri = $u->as_string; | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if ($self->method ne 'GET') { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 164 | 0 | push @headers, ('Content-type', 'application/json'); | |||||||||||||||||||||||||||||
| 165 | 0 | $body = $self->json->encode({ data => $self->data }); | |||||||||||||||||||||||||||||
| 166 | } elsif (%data) { | ||||||||||||||||||||||||||||||
| 167 | 0 | warn 'Request includes %data but is a GET request'; | |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | my $request = HTTP::Request->new( | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 170 | $self->method, | ||||||||||||||||||||||||||||||
| 171 | $uri, | ||||||||||||||||||||||||||||||
| 172 | \@headers, | ||||||||||||||||||||||||||||||
| 173 | defined $body ? $body : (), | ||||||||||||||||||||||||||||||
| 174 | ); | ||||||||||||||||||||||||||||||
| 175 | 0 | $request->authorization_basic($self->api_key,""); | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||
| 177 | # use DDP; | ||||||||||||||||||||||||||||||
| 178 | # p($self->method); | ||||||||||||||||||||||||||||||
| 179 | # p($uri); | ||||||||||||||||||||||||||||||
| 180 | # p($body); | ||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||
| 182 | # p(%data); | ||||||||||||||||||||||||||||||
| 183 | # p(@params); | ||||||||||||||||||||||||||||||
| 184 | # p($request->uri->as_string); | ||||||||||||||||||||||||||||||
| 185 | # p($request->content); | ||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||
| 187 | 0 | return $request; | |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||
| 190 | 1 | 1; | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||||