Commit d2783942 authored by Mike Jones's avatar Mike Jones 🌶

Merge branch 'dev' into 'master'

Fix file, add debug, link files to posts

See merge request !36
parents 88d78669 d6693178
......@@ -37,6 +37,7 @@ use WebService::Mattermost;
my $mattermost = WebService::Mattermost->new({
authenticate => 1, # Log into Mattermost
debug => 1, # Output some debug-level information via Mojo::Log
username => 'email@address.com',
password => 'hunter2',
base_url => 'https://my.mattermost.server.com/api/v4/',
......
......@@ -12,7 +12,7 @@ with 'WebService::Mattermost::Role::Logger';
has [ qw(base_url username password) ] => (is => 'ro', isa => Str, required => 1);
has api_version => (is => 'ro', isa => Int, default => 4);
has authenticate => (is => 'rw', isa => Bool, default => 0);
has [ qw(authenticate debug) ] => (is => 'rw', isa => Bool, default => 0);
has [ qw(auth_token user_id) ] => (is => 'rw', isa => Str, default => '');
has api => (is => 'ro', isa => Object, lazy => 1, builder => 1);
......@@ -75,6 +75,7 @@ sub _build_api {
my $args = {
base_url => $self->base_url,
auth_token => $self->auth_token,
debug => $self->debug,
};
my $ver = 'WebService::Mattermost::V4::API';
......@@ -109,7 +110,8 @@ See C<WebService::Mattermost::V4::API> for all available API integrations.
base_url => 'https://my.mattermost.server.com/api/v4/',
# Optional
authenticate => 1, # trigger a "login" to the Mattermost server
authenticate => 1, # Trigger a "login" to the Mattermost server
debug => 1, # Debug via Mojo::Log
username => 'MyUsername', # Login credentials for the server
password => 'MyPassword',
});
......@@ -118,6 +120,15 @@ See C<WebService::Mattermost::V4::API> for all available API integrations.
my $emojis = $mm->api->emoji->custom;
my $user = $mm->api->users->search_by_email('someone@somewhere.com');
Where appropriate, a response object or list of objects may be returned. You can
access these via (using the custom emoji search above as an example):
# First item only
my $item = $emojis->item;
# All items
my $items = $emoji->items;
=head2 METHODS
This class has no public methods.
......
......@@ -48,8 +48,8 @@ use WebService::Mattermost::Helper::Alias 'v4';
has auth_token => (is => 'ro', isa => Str, required => 1);
has base_url => (is => 'ro', isa => Str, required => 1);
has authenticate => (is => 'ro', isa => Bool, default => 0);
has resources => (is => 'rw', isa => ArrayRef, default => sub { [] },
has [ qw(authenticate debug) ] => (is => 'ro', isa => Bool, default => 0);
has resources => (is => 'rw', isa => ArrayRef, default => sub { [] },
handles_via => 'Array',
handles => {
add_resource => 'push',
......@@ -122,6 +122,7 @@ sub _new_resource {
auth_token => $self->auth_token,
base_url => $self->base_url,
resource => $alt_name,
debug => $self->debug,
});
$self->add_resource($resource);
......@@ -152,7 +153,7 @@ sub _build_elasticsearch { shift->_new_resource('ElasticSearch')
sub _build_email { shift->_new_resource('Email') }
sub _build_emoji { shift->_new_resource('Emoji') }
sub _build_files { shift->_new_resource('Files', 'files') }
sub _build_file { shift->_new_resource('File') }
sub _build_file { shift->_new_resource('File', 'files') }
sub _build_jobs { shift->_new_resource('Jobs') }
sub _build_ldap { shift->_new_resource('LDAP') }
sub _build_logs { shift->_new_resource('Logs') }
......@@ -171,7 +172,7 @@ sub _build_team { shift->_new_resource('Team', 'teams')
sub _build_teams { shift->_new_resource('Teams') }
sub _build_user { shift->_new_resource('User', 'users') }
sub _build_users { shift->_new_resource('Users') }
sub _build_webhooks { shift->_new_resource('Webhook', 'hooks') }
sub _build_webhooks { shift->_new_resource('Webhook', 'hooks') }
sub _build_webrtc { shift->_new_resource('WebRTC') }
################################################################################
......
package WebService::Mattermost::V4::API::Object::Binary;
use Moo;
use Types::Standard qw(Maybe Str);
extends 'WebService::Mattermost::V4::API::Object';
################################################################################
has content => (is => 'ro', isa => Maybe[Str], lazy => 1, builder => 1);
################################################################################
sub _build_content { shift->raw_content }
################################################################################
1;
__END__
=head1 NAME
WebService::Mattermost::V4::API::Object::Binary
=head1 DESCRIPTION
Contains raw binary for a file returned from Mattermost.
=head2 ATTRIBUTES
=over 4
=item C<content>
Raw binary content.
=back
=head1 AUTHOR
Mike Jones L<email:mike@netsplit.org.uk>
......@@ -37,6 +37,8 @@ has [ qw(
file_ids
) ] => (is => 'ro', isa => Maybe[ArrayRef], lazy => 1, builder => 1);
has files => (is => 'ro', lazy => 1, builder => 1);
################################################################################
sub _get_related_post {
......@@ -81,6 +83,13 @@ sub _build_root_post {
return $self->_get_related_post($self->root_id);
}
sub _build_files {
my $self = shift;
return [] unless $self->file_ids;
return [ map { $self->api->file->get($_) } @{$self->file_ids} ];
}
################################################################################
1;
......
......@@ -3,7 +3,9 @@ package WebService::Mattermost::V4::API::Request;
use Mojo::URL;
use Mojo::Util 'url_escape';
use Moo;
use Types::Standard qw(Any ArrayRef Enum InstanceOf Str);
use Types::Standard qw(Any ArrayRef Bool Enum InstanceOf Str);
with 'WebService::Mattermost::Role::Logger';
################################################################################
......@@ -13,8 +15,9 @@ has method => (is => 'ro', isa => Enum [ qw(DELETE GET POST PUT) ], required =
has resource => (is => 'ro', isa => Str, required => 1);
# Some endpoints require parameters as a HashRef, some as an ArrayRef
has parameters => (is => 'ro', isa => Any, default => sub { {} });
has debug => (is => 'ro', isa => Bool, default => 0);
has ids => (is => 'ro', isa => ArrayRef, default => sub { [] });
has parameters => (is => 'ro', isa => Any, default => sub { {} });
has url => (is => 'ro', isa => InstanceOf['Mojo::URL'], lazy => 1, builder => 1);
......@@ -35,7 +38,8 @@ sub _build_url {
$endpoint = sprintf($endpoint, @ids);
my $url = sprintf('%s%s%s', $base_url, $resource, $endpoint);
p $url;
$self->logger->debug($url) if $self->debug;
return Mojo::URL->new($url);
}
......@@ -43,4 +47,49 @@ sub _build_url {
################################################################################
1;
__END__
=head1 NAME
WebService::Mattermost::V4::API::Request
=head1 DESCRIPTION
A request to be sent to the Mattermost API.
=head2 USAGE
See C<WebService::Mattermost::V4::API::Resource::_call()>.
=head2 ATTRIBUTES
=over 4
=item C<base_url>
=item C<endpoint>
=item C<method>
HTTP method.
=item C<resource>
The API endpoint's namespace.
=item C<parameters>
Data to be sent to the API.
=item C<ids>
IDs to replace into the URL with C<sprintf>.
=item C<url>
=back
=head1 AUTHOR
Mike Jones L<email:mike@netsplit.org.uk>
......@@ -2,7 +2,7 @@ package WebService::Mattermost::V4::API::Resource;
use List::MoreUtils 'all';
use Moo;
use Types::Standard qw(HashRef Str);
use Types::Standard qw(Bool HashRef Str);
use WebService::Mattermost::Helper::Alias qw(v4 view);
use WebService::Mattermost::V4::API::Object::Channel;
......@@ -34,6 +34,7 @@ has GET => (is => 'ro', isa => Str, default => 'GET');
has headers => (is => 'ro', isa => HashRef, default => sub { {} });
has POST => (is => 'ro', isa => Str, default => 'POST');
has PUT => (is => 'ro', isa => Str, default => 'PUT');
has debug => (is => 'ro', isa => Bool, default => 0);
################################################################################
......@@ -154,6 +155,7 @@ sub _as_request {
$args->{auth_token} = $self->auth_token;
$args->{base_url} = $self->base_url;
$args->{resource} = $self->resource;
$args->{debug} = $self->debug;
$args->{endpoint} ||= '';
$args->{parameters} ||= {};
......
......@@ -13,6 +13,7 @@ sub get {
return $self->_get({
endpoint => '%s',
ids => [ $file_id ],
view => 'Binary',
});
}
......
......@@ -3,7 +3,6 @@
use strict;
use warnings;
use DDP;
use FindBin;
use Test::Most;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment