Skip to content

Commit

Permalink
Add preserve_headers option and persistent_headers to allow sticky
Browse files Browse the repository at this point in the history
headers.
  • Loading branch information
kaare committed May 18, 2012
1 parent 3813634 commit f94fa38
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
Revision history for Role-REST-Client Revision history for Role-REST-Client


{{$NEXT}} {{$NEXT}}
- Add preserve_headers option and persistent_headers to allow sticky headers.


0.05 2012-04-02 22:57:55 Europe/Copenhagen 0.05 2012-04-02 22:57:55 Europe/Copenhagen
- Response and serializer are now easy overridable [Matt Phillips]. - Response and serializer are now easy overridable [Matt Phillips].
Expand Down
28 changes: 25 additions & 3 deletions lib/Role/REST/Client.pm
Expand Up @@ -33,11 +33,23 @@ has _ua => (
return $self->{ua}; return $self->{ua};
}, },
); );
has 'httpheaders' => ( has 'persistent_headers' => (
traits => ['Hash'], traits => ['Hash'],
is => 'ro', is => 'ro',
isa => 'HashRef[Str]', isa => 'HashRef[Str]',
default => sub { {} }, default => sub { {} },
handles => {
set_persistent_header => 'set',
get_persistent_header => 'get',
has_no_persistent_headers => 'is_empty',
clear_persistent_headers => 'clear',
},
);
has 'httpheaders' => (
traits => ['Hash'],
is => 'rw',
isa => 'HashRef[Str]',
default => sub { {} },
handles => { handles => {
set_header => 'set', set_header => 'set',
get_header => 'get', get_header => 'get',
Expand Down Expand Up @@ -87,7 +99,7 @@ sub _call {
my %options = (headers => $self->httpheaders); my %options = (headers => $self->httpheaders);
$options{content} = ref $data ? $self->_serializer->serialize($data) : $data if defined $data; $options{content} = ref $data ? $self->_serializer->serialize($data) : $data if defined $data;
my $res = $self->_ua->request($method, $uri, \%options); my $res = $self->_ua->request($method, $uri, \%options);
$self->clear_headers; $self->httpheaders($self->persistent_headers) unless $args->{preserve_headers};
# Return an error if status 5XX # Return an error if status 5XX
return $self->_new_rest_response( return $self->_new_rest_response(
code => $res->{status}, code => $res->{status},
Expand Down Expand Up @@ -232,9 +244,9 @@ args - the optional argument parameter can have these entries
deserializer - if you KNOW that the content-type of the response is incorrect, deserializer - if you KNOW that the content-type of the response is incorrect,
you can supply the correct content type, like you can supply the correct content type, like
my $res = $self->post('foo/bar/baz', {foo => 'bar'}, {deserializer => 'application/yaml'}); my $res = $self->post('foo/bar/baz', {foo => 'bar'}, {deserializer => 'application/yaml'});
preserve_headers - set this to true if you want to keep the headers between calls
All methods return a response object dictated by _rest_response_class. Set to L<Role::REST::Client::Response> by default. All methods return a response object dictated by _rest_response_class. Set to L<Role::REST::Client::Response> by default.
Expand All @@ -252,6 +264,16 @@ Mime content type,
e.g. application/json e.g. application/json
=head2 httpheaders
You can set any http header you like with set_header, e.g.
$self->set_header($key, $value) but the content-type header will be overridden.
=head2 persistent_headers
A hashref containing headers you want to use for all requests. Set individual headers with
set_persistent_header, clear the hashref with clear_persistent_header.
=head2 clientattrs =head2 clientattrs
Attributes to feed HTTP::Tiny Attributes to feed HTTP::Tiny
Expand Down

0 comments on commit f94fa38

Please sign in to comment.