Kanboard-API

 view release on metacpan or  search on metacpan

lib/Kanboard/API.pm  view on Meta::CPAN

package Kanboard::API;    ## no critic

use v5.40;
use strict;
use warnings;
use Moo;
use Data::Dumper;
use LWP::UserAgent;
use HTTP::Request;
use MIME::Base64;
use JSON;

=head1 NAME

Kanboard::API - A Perl interface to the Kanboard API

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

Kanboard::API is a Perl interface to the Kanboard API. It provides a simple way to interact with a Kanboard instance.

    use Kanboard::API;

    my $kanboard = Kanboard::API->new(username => 'yourusername|jsonrpc', password => 'apikey', endpoint => 'https://yourkanboardinstance.com');
    ...

=head1 ATTRIBUTES

=head2 username

The username to use for authentication. Defaults to "jsonrpc".

=cut

has username => ( is => 'ro', default => 'jsonrpc' );

=head2 password

The password to use for authentication.

=cut

has password => ( is => 'ro', required => 1 );

=head2 endpoint

The JSON RPC Endpoint of the Kanboard instance.

=cut

has endpoint => ( is => 'ro', required => 1 );

=head2 ua

The LWP::UserAgent object used to make requests.

=cut

has 'ua' => (
    is      => 'lazy',
    builder => sub { LWP::UserAgent->new },
);

=head1 PRIVATE METHODS

=head2 _request

=cut

sub _request( $self, $method, $params = [] ) {
    my $auth    = encode_base64( "$self->{username}:$self->{password}", "" );
    my $headers = [
        'Content-Type'  => 'application/json',
        'Authorization' => "Basic $auth",
    ];
    my $request_json = {
        jsonrpc => "2.0",
        id      => 1,
        method  => $method,
        params  => $params // [],
    };
    my $body = encode_json($request_json);
    my $request =
      HTTP::Request->new( 'POST', $self->endpoint, $headers, $body );

    my $response = $self->ua->request($request);

    if ( $response->is_success ) {
        my $content      = $response->decoded_content;
        my $content_json = decode_json($content);
        if ( $content_json->{error} ) {
            die
              "$content_json->{error}{message} - $content_json->{error}{data}";
        }
        return $content_json->{result};
    }
    else {
        die $response->status_line;
    }
}

=head1 PUBLIC METHODS

=head2 get_version


Purpose: Get the application version
Parameters: none
Result: version (Example: 1.0.12, master)


=cut

sub get_version( $self) {
    return $self->_request('getVersion');
}

=head2 get_me


Purpose: Get the current user profile
Parameters:
    None
Result on success: user properties
Result on failure: empty list


=cut

sub get_me( $self) {
    return $self->_request('getMe');
}

=head2 create_project


Purpose: Create a new project
Parameters:
    name (string, required)
    description (string, optional)
    owner_id (integer, optional)
    identifier (string, optional)
Result on success: project ID
Result on failure: false



( run in 1.463 second using v1.01-cache-2.11-cpan-524268b4103 )