Skip to content

Instantly share code, notes, and snippets.

@fuba
Created April 10, 2023 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuba/dface8c40b9ed96d8e1a5f99cb1c6479 to your computer and use it in GitHub Desktop.
Save fuba/dface8c40b9ed96d8e1a5f99cb1c6479 to your computer and use it in GitHub Desktop.
a module generated by chatgpt and copilot
package MastodonLike::Client;
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use HTTP::Request::Common;
sub new {
my ($class, %args) = @_;
my $instance = $args{instance};
my $access_token = $args{access_token};
my $ua = $args{ua} || LWP::UserAgent->new();
my $json = $args{json} || JSON->new();
my $self = {
instance => $instance,
access_token => $access_token,
ua => $ua,
json => $json,
};
return bless $self, $class;
}
sub request {
my ($self, $endpoint, $method, $params) = @_;
my $url = $self->{instance} . $endpoint;
my $req;
if ($method eq 'GET') {
$req = GET($url);
} elsif ($method eq 'POST') {
$req = POST($url, $params);
} else {
die "Invalid method: $method";
}
$req->header('Authorization', "Bearer " . $self->{access_token});
$req->header('Content-Type', 'application/json');
my $res = $self->{ua}->request($req);
die "Request failed: " . $res->status_line unless $res->is_success;
return $self->{json}->decode($res->content);
}
sub verify_credentials {
my ($self) = @_;
return $self->request('/api/v1/accounts/verify_credentials', 'GET');
}
sub followers {
my ($self, $id) = @_;
return $self->request("/api/v1/accounts/$id/followers", 'GET');
}
sub following {
my ($self, $id) = @_;
return $self->request("/api/v1/accounts/$id/following", 'GET');
}
sub home_timeline {
my ($self, $params) = @_;
return $self->request('/api/v1/timelines/home', 'GET', $params);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment