NAME

WebService::HMRC::Request - Base class for accessing the UK HMRC MTD API

VERSION

Version 0.02

SYNOPSIS

    use WebService::HMRC::Request;
    my $r = WebService::HMRC::Request->new({
        base_url    => 'https://test-api.service.hmrc.gov.uk/',
        api_version => '1.0',
    });

    # get from open endpoint
    my $response = $r->get_endpoint({
        endpoint => '/hello/world',
    });
    print $response->data->{message} if $response->is_success;

    # get from application-restricted endpoint
    $r->auth->server_token('MY_SERVER_TOKEN');
    my $response = $r->get_endpoint({
        endpoint => '/hello/application',
        auth_type => 'application',
    });
    print $response->data->{message} if $response->is_success;
   
    # get from user-restricted endpoint
    $r->auth->access_token('MY_ACCESS_TOKEN');
    my $response = $r->get_endpoint({
        endpoint => '/hello/user',
        auth_type => 'user',
    });
    print $response->data->{message} if $response->is_success;
    
    # post data as json to an application-restricted endpoint
    my $data = {serviceNames => ['mtd-vat']};
    my $response = $r->post_endpoint_json({
        endpoint => '/create-test-user/organisations',
        data => $data,
        auth_type => 'application',
    });
    print $response->data->{userId} if $response->is_success;

DESCRIPTION

This is part of the WebService::HMRC suite of Perl modules for interacting with the UK's HMRC Making Tax Digital APIs.

This is a base class for making requests to the UK's HMRC Making Tax Digital API. It is usually inherited by other higher-level classes, but can be used directly.

This module provides methods for calling api endpoints, mapping their response into a standard class and decoding their JSON payload. It also provides a LWP::UserAgent with appropriate headers set and a lower-level method for constructing api endpoint urls.

Note that access to restricted api endpoints requires application or user credentials issued by HMRC.

INSTALLATION AND TESTING

See documentation for WebService::HMRC.

PROPERTIES

auth

A WebService::HMRC::Authenticate object reference providing credentials and tokens required to access protected endpoints. If not specified, an empty WebService::HMRC::Authenticate will be created by default.

base_url

Base url used for calls to the HMRC "Making Tax Digital" API. Defaults to test url `https://test-api.service.hmrc.gov.uk/` if not specified.

See: https://developer.service.hmrc.gov.uk/api-documentation/docs/reference-guide

api_version

Read-only property which defines the API version in use by this module as a string. Defaults to `1.0` if not specified.

See: https://developer.service.hmrc.gov.uk/api-documentation/docs/reference-guide#versioning

ua

Read-only property representing a LWP::UserAgent object used to perform the api calls. This will be created by default, but an alternative LWP::UserAgent object may be provided instead, providing an appropriate default 'Accept' header is configured.

METHODS

endpoint_url($endpoint)

Combine the given api endpoint (for example `/hello/world`) with the api's base_url, returning a complete endpoint url.

Returns a URI object, which evaluates to a plain url in string context.

get_endpoint({ endpoint => $endpoint, [auth_type => $auth_type,] [parameters => \%params,] [headers => \@headers] })

Retrieve a response from an HMRC Making Tax Digital api endpoint, using http get. Authorisation headers appropriate to the specified authorisation type are added to the request.

Returns a WebService::HMRC::Response object reference.

Parameters:

endpoint

Mandatory parameter specifying the endpoint to be accessed, for example '/hello/world'. Combined with base_url to build a fully-qualified url.

auth_type

Optional parameter. If specified, must be one of 'open', 'user', or 'application', corresponding to the different types of authentication used by HMRC MTD apis. If not specified, or undef, defaults to 'open'.

parameters

Optional parameter. A hashref containing query parameters and their associated value to be appended to the endpoint url.

headers

Optional parameter. An array of key/value pairs send as request headers in addition to the default `Accept` header.

post_endpoint_json({ endpoint => $endpoint, data => $ref, [auth_type => $auth_type,] [headers => \@headers] })

Post data encoded as json to an HMRC Making Tax Digital api endpoint.

Authorisation headers appropriate to the specified authorisation type are added to the request.

Returns a WebService::HMRC::Response object reference.

Parameters:

endpoint

Required parameter specifying the endpoint to be accessed, for example '/hello/world'. Combined with base_url to build a fully-qualified url.

data

Required parameter. Reference to a perl data structure, either a hashref or an arrayref, which is encoded to json for submission.

auth_type

Optional parameter. If specified, must be one of 'open', 'user', or 'application', corresponding to the different types of authentication used by HMRC MTD apis. If not specified, or undef, defaults to 'open'.

headers

Optional parameter. An array of key/value pairs send as request headers in addition to the default `Accept` header.

AUTHOR

Nick Prater <nick@npbroadcast.com>

BUGS

Please report any bugs or feature requests to bug-webservice-hmrc at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-HMRC. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc WebService::HMRC::Request

You can also look for information at:

ACKNOWLEDGEMENTS

This module was originally developed for use as part of the LedgerSMB open source accounting software.

LICENSE AND COPYRIGHT

Copyright 2018 Nick Prater, NP Broadcast Limited.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.