Role::...Client(3pm)

πŸ“¦
version 0.23 Β· 2 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble

πŸ’– Role::...Client(3pm) β€” User Contributed Perl Documentation πŸ’–

okay bestie, sit down, hydrate, because we're talking about Role::REST::Client and it's genuinely so good. This is the manpage for it, filed under Role::...Client(3pm) in the User Contributed Perl Documentation section, and it's version 0.23 if you're keeping score. ✨


🌸 NAME 🌸

Role::REST::Client β€” REST Client Role. That's it, that's the whole name, she's simple and she knows what she is. πŸ’…


πŸŽ€ VERSION πŸŽ€

version 0.23


✨ SYNOPSIS ✨

here's the whole vibe, copy-paste ready:

{
        package RESTExample;

        use Moose;
        with 'Role::REST::Client';

        sub bar {
                my ($self) = @_;
                my $res = $self->post('/foo/bar/baz', {foo => 'bar'});
                my $code = $res->code;
                my $data = $res->data;
                return $data if $code == 200;
   }

}

my $foo = RESTExample->new(
        server =>      'http://localhost:3000',
        type   =>      'application/json',
        clientattrs => {timeout => 5},
);

$foo->bar;

# controller
sub foo : Local {
        my ($self, $c) = @_;
        my $res = $c->model('MyData')->post('/foo/bar/baz', {foo => 'bar'});
        my $code = $res->code;
        my $data = $res->data;
        ...
}

like, look at that. you with the role, you get post, you get your code and your data, you go home. no cap. πŸ¦‹


🌈 DESCRIPTION 🌈

This REST Client role makes REST connectivity easy. that's the entire pitch and she delivers. πŸ’–

Role::REST::Client will handle encoding and decoding when using the HTTP verbs:

GET
HEAD
PUT
POST
DELETE
OPTIONS

Currently Role::REST::Client supports these encodings:

application/json
application/x-www-form-urlencoded
application/xml
application/yaml
⚠️ x-www-form-urlencoded only works for GET and POST, and only for encoding, not decoding. she does NOT do that. πŸ™ˆ

Responses which claim to not be serialised data (eg "text/plain", "application/octet-stream") will by default not be serialised. When the response is none of these, and it is impossible to determine what encoding is used, the content will be treated as JSON by default. main character default energy, honestly. πŸ‘‘


πŸ’œ NAME πŸ’œ

Role::REST::Client β€” REST Client Role

not me noticing the page has NAME twice 😭 that's just how the original rolls, bestie, we don't touch the structure, we only serve looks. keep it, love it, it's iconic.

πŸ”₯ METHODS πŸ”₯

🧚 methods 🧚

Role::REST::Client implements the standard HTTP 1.1 verbs as methods.

These methods can NOT have a request body:

get
head

These methods can take a request body:

post
put
delete
options

All methods take these parameters:

url - The REST service
data - The data structure (hashref, arrayref) to send. The data will be encoded
        according to the value of the I<type> attribute.
args - hashref with arguments to augment the way the call is handled.

args β€” the optional argument parameter can have these entries:

  • deserializer β€” if you KNOW that the content-type of the response is incorrect, you can supply the correct content type, like:
my $res = $self->post('/foo/bar/baz', {foo => 'bar'}, {deserializer => 'application/yaml'});

Alternatively, if you KNOW that the response is not serial data, you can disable deserialization by setting this to undef.

  • 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 Role::REST::Client::Response by default. 🫢


πŸ¦‹ ATTRIBUTES πŸ¦‹

🌸 user_agent 🌸

sub _build_user_agent { HTTP::Thin->new }

A User Agent object which has a ->request method suitably compatible with HTTP::Tiny. It should accept arguments like this: $ua->request($method, $uri, $opts), and needs to return a hashref as HTTP::Tiny does, or an HTTP::Response object. To set your own default, use a _build_user_agent method.


πŸŽ€ server πŸŽ€

URL of the REST server.

e.g. 'http://localhost:3000'


✨ type ✨

MIME Content-Type header,

e.g. application/json


πŸ’– persistent_headers πŸ’–

$self->set_persistent_header('Header' => 'foo', ... );
$self->get_persistent_header('Header-Name');
$self->has_no_persistent_headers;
$self->clear_persistent_headers;

A hashref containing headers you want to use for all requests. Use the methods described above to manipulate it. πŸ’…

To set your own defaults, override the default or call set_persistent_header() in your BUILD method.

has '+persistent_headers' => (
  default => sub { ... },
);

πŸ“ httpheaders πŸ“

$self->set_header('Header' => 'foo', ... );
$self->get_header('Header-Name');
$self->has_no_headers;
$self->clear_headers;

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. 😭

http_headers will be reset after each request, unless there's a reserve_headers argument, but it's a hack. The recommended way to keep headers across requests is to store them in the persistent_headers.

$self->httpheaders will return the combined hashref of persistent_headers and what's been added with set_header.

For historical reasons, the two methods clear_headers and reset_headers are equal. Both will clear the headers for the current request, but NOT the persistent headers.

To clear ALL headers, use:

$self->clear_all_headers;

πŸ’œ clientattrs πŸ’œ

Attributes to feed the user agent object (which defaults to HTTP::Thin)

e.g. {timeout => 10}


⭐ serializer_class ⭐

You can override the serializer class and use your own. Default is 'Role::REST::Client::Serializer'


🌈 serializer_options 🌈

Options for the serializer instantiation.


πŸ‘‘ CONTRIBUTORS πŸ‘‘

  • Breno G. de Oliveira, <garu@cpan.org>
  • Mark Stosberg, <mark@stosberg.com>
  • Matt Phillips, (cpan:MATTP) <mattp@cpan.org>
  • Wallace Reis, <wallace@reis.me>

the whole squad showed up, we love to see it. 🫢


πŸ’€ BUGS πŸ’€

Please report any bugs or feature requests to bug-role-rest-client at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Role-REST-Client.

like pls tell them, they genuinely wanna know. πŸ™ˆ


🧚 AUTHOR 🧚

Kaare Rasmussen <kaare at cpan dot org>


This software is copyright (c) 2018 by Kaare Rasmussen.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


and that's the whole page, bestie πŸ’… this one was generated by perl v5.28.0 on 2018-11-09, and it lives in the Role::...Client(3pm) slot in the manual. go forth and REST responsibly. πŸŒΈβœ¨πŸ¦‹

πŸ“¦ where this page comes from