NAME

WWW::Hetzner::Role::HTTP - HTTP client role for Hetzner API clients

VERSION

version 0.100

SYNOPSIS

package WWW::Hetzner::Cloud;
use Moo;

has token => ( is => 'ro' );
has base_url => ( is => 'ro', default => 'https://api.hetzner.cloud/v1' );

with 'WWW::Hetzner::Role::HTTP';

DESCRIPTION

This role provides HTTP methods (GET, POST, PUT, DELETE) for Hetzner API clients. It handles JSON encoding/decoding, authentication, and error handling.

HTTP transport is delegated to a pluggable WWW::Hetzner::Role::IO backend (default: WWW::Hetzner::LWPIO), making it possible to use async HTTP clients.

Uses Log::Any for logging HTTP requests and responses.

REQUIRED ATTRIBUTES

Classes consuming this role must provide:

  • token - API authentication token

  • base_url - Base URL for the API

io

Pluggable HTTP backend implementing WWW::Hetzner::Role::IO. Defaults to WWW::Hetzner::LWPIO.

# Use a custom IO backend
my $cloud = WWW::Hetzner::Cloud->new(
    token => $token,
    io    => My::AsyncIO->new,
);

get

my $data = $self->get('/path', params => { key => 'value' });

Perform a GET request.

post

my $data = $self->post('/path', { key => 'value' });

Perform a POST request with JSON body.

put

my $data = $self->put('/path', { key => 'value' });

Perform a PUT request with JSON body.

delete

my $data = $self->delete('/path');

Perform a DELETE request.

_set_auth

Sets authentication headers. Override for different auth mechanisms:

# Default: Bearer token
sub _set_auth {
    my ($self, $headers) = @_;
    $headers->{Authorization} = 'Bearer ' . $self->token;
}

# Basic Auth (e.g. Robot API)
sub _set_auth {
    my ($self, $headers) = @_;
    require MIME::Base64;
    $headers->{Authorization} = 'Basic ' .
        MIME::Base64::encode_base64($self->user . ':' . $self->password, '');
}

_build_request

my $req = $self->_build_request('GET', '/servers', params => { page => 1 });

Builds a WWW::Hetzner::HTTPRequest without executing it. Useful for async workflows where request creation and execution are separate steps.

_parse_response

my $data = $self->_parse_response($response, 'GET', '/servers');

Parses a WWW::Hetzner::HTTPResponse: decodes JSON, checks for errors. Useful for async workflows where response parsing happens after transport.

SEE ALSO

WWW::Hetzner::Cloud, WWW::Hetzner::Role::IO, WWW::Hetzner::LWPIO, Log::Any

SUPPORT

Issues

Please report bugs and feature requests on GitHub at https://github.com/Getty/p5-www-hetzner/issues.

IRC

Join #kubernetes on irc.perl.org or message Getty directly.

CONTRIBUTING

Contributions are welcome! Please fork the repository and submit a pull request.

AUTHOR

Torsten Raudssus <torsten@raudssus.de>

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Torsten Raudssus.

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