| 1 | package Net::Curl::Parallel::Types; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Net::Curl::Parallel::Types - type constraints for Net::Curl::Parallel | ||||||||||
| 6 | |||||||||||
| 7 | =cut | ||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Type::Utils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Types::Standard -types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use URI::Fast qw(uri); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | use Type::Library | ||||||||||
| 15 | 1 | ▶ | -base, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | -declare => qw( | ||||||||||
| 17 | Positive | ||||||||||
| 18 | Natural | ||||||||||
| 19 | Agent | ||||||||||
| 20 | Method | ||||||||||
| 21 | Uri | ||||||||||
| 22 | Headers | ||||||||||
| 23 | Content | ||||||||||
| 24 | Request | ||||||||||
| 25 | 1 | ); | |||||||||
| 26 | |||||||||||
| 27 | =head1 EXPORTED TYPES | ||||||||||
| 28 | |||||||||||
| 29 | Declared types may be exported individually or as a set with C<-all>. | ||||||||||
| 30 | |||||||||||
| 31 | use Net::Curl::Parallel::Types -all; | ||||||||||
| 32 | use Net::Curl::Parallel::Types qw(Agent Method); | ||||||||||
| 33 | |||||||||||
| 34 | =head2 Positive | ||||||||||
| 35 | |||||||||||
| 36 | A positive integer. | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | 3 | ▶ | declare Positive, as Int & sub{ $_ > 0 }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | =head2 Natural | ||||||||||
| 43 | |||||||||||
| 44 | A positive integer or zero. | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | 3 | ▶ | declare Natural, as Int & sub{ $_ >= 0 }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 49 | |||||||||||
| 50 | =head2 Agent | ||||||||||
| 51 | |||||||||||
| 52 | An HTTP client agent string in the form C<Name/v1.23>. | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 1 | declare Agent, as StrMatch[qr{^\S+/v\d+(?:\.\d+)$}]; | |||||||||
| 57 | |||||||||||
| 58 | =head2 Method | ||||||||||
| 59 | |||||||||||
| 60 | A supported HTTP method. Coercible from a matching lowercase name. | ||||||||||
| 61 | |||||||||||
| 62 | =cut | ||||||||||
| 63 | |||||||||||
| 64 | 1 | declare Method, as Enum[qw(GET POST HEAD PUT DELETE)]; | |||||||||
| 65 | |||||||||||
| 66 | coerce Method, | ||||||||||
| 67 | from Enum[qw(get post head put delete)], | ||||||||||
| 68 | 0 | ▶ | via{ uc $_ }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 69 | |||||||||||
| 70 | =head2 Uri | ||||||||||
| 71 | |||||||||||
| 72 | A valid URI string. Automatically coerced from L<URI> and L<URI::Fast> objects. | ||||||||||
| 73 | |||||||||||
| 74 | =cut | ||||||||||
| 75 | |||||||||||
| 76 | declare Uri, as Str, | ||||||||||
| 77 | 19 | ▶ | where{ | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 78 | 19 | my $uri = uri $_; | |||||||||
| 79 | 19 | ▶ | return unless $uri->host; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 16 | return 1; | |||||||||
| 81 | 1 | }; | |||||||||
| 82 | |||||||||||
| 83 | coerce Uri, | ||||||||||
| 84 | from InstanceOf['URI'], | ||||||||||
| 85 | 1 | ▶ | via{ $_->as_string }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 86 | |||||||||||
| 87 | coerce Uri, | ||||||||||
| 88 | from InstanceOf['URI::Fast'], | ||||||||||
| 89 | 0 | ▶ | via{ $_->to_string }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 90 | |||||||||||
| 91 | =head2 Headers | ||||||||||
| 92 | |||||||||||
| 93 | An array of strings, where each string is a header key/value pair separated by | ||||||||||
| 94 | a colon. Coercible from undef (to empty header set), array of string tuples | ||||||||||
| 95 | containing key/value pairs, a hash of key/value pairs, or an instance of | ||||||||||
| 96 | L<HTTP::Headers>. | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||
| 99 | |||||||||||
| 100 | 1 | declare Headers, as ArrayRef[StrMatch[qr/^\S+: .+$/]]; | |||||||||
| 101 | |||||||||||
| 102 | coerce Headers, | ||||||||||
| 103 | from Undef, | ||||||||||
| 104 | 1 | ▶ | via{ [] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 105 | |||||||||||
| 106 | coerce Headers, | ||||||||||
| 107 | from Str, | ||||||||||
| 108 | 1 | ▶ | via{ [split qr/[\r\n]+/, $_] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 109 | |||||||||||
| 110 | coerce Headers, | ||||||||||
| 111 | from ArrayRef[Tuple[Str, Str]], | ||||||||||
| 112 | 3 | ▶ | via{ [map{ join ': ', @$_ } @$_] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 113 | |||||||||||
| 114 | coerce Headers, | ||||||||||
| 115 | from HashRef[Str], | ||||||||||
| 116 | 2 | ▶ | via{ my $h = $_; [map{ $_ . ': ' . $h->{$_} } keys %$h] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 117 | |||||||||||
| 118 | coerce Headers, | ||||||||||
| 119 | from InstanceOf['HTTP::Headers'], | ||||||||||
| 120 | 2 | ▶ | via{ | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 121 | 2 | my $headers = $_; | |||||||||
| 122 | Headers->assert_coerce( | ||||||||||
| 123 | [ | ||||||||||
| 124 | 2 | map{ [$_, $headers->header($_)] } | |||||||||
| 125 | $headers->header_field_names | ||||||||||
| 126 | ] | ||||||||||
| 127 | ); | ||||||||||
| 128 | 1 | }; | |||||||||
| 129 | |||||||||||
| 130 | =head2 Content | ||||||||||
| 131 | |||||||||||
| 132 | An optional string (C<Maybe[Str]>). Coercible from a hash ref of query | ||||||||||
| 133 | parameters or an array ref of key/value tuples to a url-encoded string. There | ||||||||||
| 134 | are no coercions or automatic url-encoding of binary data or other formats. | ||||||||||
| 135 | |||||||||||
| 136 | {foo => 'bar bat'} -> foo=bar%20bat | ||||||||||
| 137 | {foo => ['bar', 'bat']} -> foo=bar&foo=bat | ||||||||||
| 138 | [['foo', 'bar bat']] -> foo=bar%20bat | ||||||||||
| 139 | [['foo', 'bar'], ['foo', 'bat']] -> foo=bar&foo=bat | ||||||||||
| 140 | |||||||||||
| 141 | =cut | ||||||||||
| 142 | |||||||||||
| 143 | 7 | ▶ | declare Content, as Maybe[Str & sub{ length $_ }]; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 144 | |||||||||||
| 145 | coerce Content, | ||||||||||
| 146 | from HashRef[Str | ArrayRef[Str]], | ||||||||||
| 147 | 2 | ▶ | via{ | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 148 | 2 | my $uri = uri; | |||||||||
| 149 | 2 | $uri->query($_); | |||||||||
| 150 | 2 | scalar $uri->query; | |||||||||
| 151 | 1 | }; | |||||||||
| 152 | |||||||||||
| 153 | coerce Content, | ||||||||||
| 154 | from ArrayRef[Tuple[Str, Str]], | ||||||||||
| 155 | 2 | ▶ | via{ | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 156 | 2 | my $uri = uri; | |||||||||
| 157 | 2 | foreach (@$_) { | |||||||||
| 158 | 3 | my ($k, $v) = @$_; | |||||||||
| 159 | 3 | $uri->add_param($k, $v); | |||||||||
| 160 | } | ||||||||||
| 161 | 2 | scalar $uri->query; | |||||||||
| 162 | 1 | }; | |||||||||
| 163 | |||||||||||
| 164 | coerce Content, | ||||||||||
| 165 | from Enum[''], | ||||||||||
| 166 | 1 | ▶ | via{ undef }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 167 | |||||||||||
| 168 | =head2 Request | ||||||||||
| 169 | |||||||||||
| 170 | The supported request parameters accepted by L<Net::Curl::Parallel/add>, matching | ||||||||||
| 171 | the arguments to the constructor of L<HTTP::Request>. Coercible from an | ||||||||||
| 172 | instance of L<HTTP::Request>. | ||||||||||
| 173 | |||||||||||
| 174 | =cut | ||||||||||
| 175 | |||||||||||
| 176 | 1 | declare Request, as Tuple[Method, Uri, Headers, Content], coercion => 1; | |||||||||
| 177 | |||||||||||
| 178 | coerce Request, | ||||||||||
| 179 | from InstanceOf['HTTP::Request'], | ||||||||||
| 180 | 1 | ▶ | via { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 181 | return [ | ||||||||||
| 182 | 1 | Method->assert_coerce($_->method), | |||||||||
| 183 | Uri->assert_coerce($_->uri), | ||||||||||
| 184 | Headers->assert_coerce($_->headers), | ||||||||||
| 185 | Content->assert_coerce($_->content), | ||||||||||
| 186 | ]; | ||||||||||
| 187 | 1 | }; | |||||||||
| 188 | |||||||||||
| 189 | coerce Request, | ||||||||||
| 190 | from ((~Request) & ArrayRef), | ||||||||||
| 191 | 0 | ▶ | via { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 192 | return [ | ||||||||||
| 193 | 0 | Method->assert_coerce($_->[0]), | |||||||||
| 194 | Uri->assert_coerce($_->[1]), | ||||||||||
| 195 | Headers->assert_coerce($_->[2]), | ||||||||||
| 196 | Content->assert_coerce($_->[3]), | ||||||||||
| 197 | ]; | ||||||||||
| 198 | 1 | }; | |||||||||
| 199 | |||||||||||
| 200 | 1 | 1; | |||||||||