API-Medium
view release on metacpan or search on metacpan
lib/API/Medium.pm view on Meta::CPAN
has 'access_token' => (
isa => 'Str',
is => 'rw',
required => 1,
);
has 'refresh_token' => (
isa => 'Str',
is => 'ro',
);
has '_client' => (
isa => 'HTTP::Tiny',
is => 'ro',
lazy_build => 1,
);
sub _build__client {
my $self = shift;
return HTTP::Tiny->new(
agent => join( '/', __PACKAGE__, $VERSION ),
default_headers => {
'Authorization' => 'Bearer ' . $self->access_token,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
}
);
}
sub get_current_user {
my $self = shift;
my $res = $self->_request( 'GET', 'me' );
return $res->{data};
}
sub create_post {
my ( $self, $user_id, $post ) = @_;
$post->{publishStatus} ||= 'draft';
my $res = $self->_request( 'POST', 'users/' . $user_id . '/posts', $post );
return $res->{data}{url};
}
sub create_publication_post {
my ( $self, $publication_id, $post ) = @_;
$post->{publishStatus} ||= 'draft';
my $res =
$self->_request( 'POST', 'publications/' . $publication_id . '/posts',
$post );
return $res->{data}{url};
}
sub _request {
my ( $self, $method, $endpoint, $data ) = @_;
my $url = join( '/', $self->server, $endpoint );
my $res;
if ($data) {
$res = $self->_client->request( $method, $url,
{ content => encode_json($data) } );
}
else {
$res = $self->_client->request( $method, $url );
}
if ( $res->{success} ) {
return decode_json( $res->{content} );
}
else {
$log->errorf( "Could not talk to medium: %i %s",
$res->{status}, $res->{reason} );
die join( ' ', $res->{status}, $res->{reason} );
}
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
API::Medium - Talk with medium.com using their REST API
=head1 VERSION
version 0.902
=head1 SYNOPSIS
use API::Medium;
my $m = new({
access_token=>'your_token',
});
my $hash = $m->get_current_user;
say $hash->{id};
my $url = $m->create_post( $user_id, $post );
my $other_url = $m->create_publication_post( $publication_id, $post );
=head1 DESCRIPTION
It's probably a good idea to read L<the Medium API
docs|https://github.com/Medium/medium-api-docs> first, especially as
the various data structures you have to send (or might get back) are
B<not> documented here.
See F<example/hello_medium.pl> for a complete script.
=head2 Authentication
( run in 0.636 second using v1.01-cache-2.11-cpan-1edf4fed603 )