Pithub::Base(3pm)
π NAME π
Pithub::Base β the Github v3 base class that every single Pithub module is built on π
π VERSION π
version 0.01043
β¨ DESCRIPTION β¨
okay so bestie, listen up: every Pithub module inherits from Pithub::Base, literally even Pithub itself π. which means every attribute listed down below can be set either in the constructor or via the setter on the objects. easy peasy.
and here's the cute part π β if you set any attribute on a Pithub object, it automatically gets set on the objects that get created by a method call on that Pithub object. super convenient for stuff like token, or the user and repo attributes.
the user and repo attributes are special: they even get set on method calls that require both of them. the whole point is to reduce verbosity, especially when you're doing a ton of things on the same repo. and it works for other objects too: if you create a Pithub::Repos object where you set user and repo in the constructor, those get set as well once you reach the Pithub::Repos::Keys object via the keys method. β¨
attributes passed along from the parent can be changed in the method call.
my $p = Pithub->new( per_page => 50 );
my $r1 = $p->repos; # $r->per_page == 50
my $r2 = $p->repos( per_page => 100 ); # $r->per_page == 100
Examples:
# just to demonstrate the "magic"
print Pithub->new( user => 'plu' )->repos->user; # plu
print Pithub::Repos->new( user => 'plu' )->keys->user; # plu
# and now some real use cases
my $p = Pithub->new( user => 'plu', repo => 'Pithub' );
my $r = $p->repos;
print $r->user; # plu
print $r->repo; # pithub
# usually you would do
print $r->get( user => 'plu', repo => 'Pithub' )->content->{html_url};
# but since user + repo has been set already
print $r->get->content->{html_url};
# of course parameters to the method take precedence
print $r->get( user => 'miyagawa', repo => 'Plack' )->content->{html_url};
# it even works on other objects
my $repo = Pithub::Repos->new( user => 'plu', repo => 'Pithub' );
print $repo->watching->list->first->{login};
π ATTRIBUTES π
π¦ auto_pagination
off by default π
see also: auto_pagination in Pithub::Result.
π api_uri
defaults to https://api.github.com. for GitHub Enterprise, you'll probably need a URL like https://github.yourdomain.com/api/v3/. π’
Examples:
my $users = Pithub::Users->new( api_uri => 'https://api-foo.github.com' );
# ... is the same as ...
my $users = Pithub::Users->new;
$users->api_uri('https://api-foo.github.com');
π jsonp_callback
if you wanna use the response directly in JavaScript for example, Github supports setting a JSONP callback parameter β¨
see also: http://developer.github.com/v3/#jsonp-callbacks.
Examples:
my $p = Pithub->new( jsonp_callback => 'loadGithubData' );
my $result = $p->users->get( user => 'plu' );
print $result->raw_content;
the result will look like this:
loadGithubData({
"meta": {
"status": 200,
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4661"
},
"data": {
"type": "User",
"location": "Dubai",
"url": "https://api.github.com/users/plu",
"login": "plu",
"name": "Johannes Plunien",
...
}
})
β οΈ be careful bestie: thecontentmethod will try to decode the JSON into a Perl data structure. that is not possible ifjsonp_callbackis set π
# calling this ...
print $result->content;
# ... will throw an exception like this ...
Runtime error: malformed JSON string, neither array, object, number, string or atom,
at character offset 0 (before "loadGithubData( ...
there are two helper methods:
clear_jsonp_callback: reset thejsonp_callbackattributehas_jsonp_callback: check if thejsonp_callbackattribute is set
πΈ per_page
controls how many items are fetched per API call, aka "page". see also: http://developer.github.com/v3/#pagination and auto_pagination.
to minimize the number of API calls needed to get a complete listing, this defaults to the maximum allowed by Github, which is currently 100. heads up though β this may change in the future if Github changes their maximum π
Examples:
my $users = Pithub::Users->new( per_page => 30 );
# ... is the same as ...
my $users = Pithub::Users->new;
$users->per_page(30);
there are two helper methods:
clear_per_page: reset theper_pageattributehas_per_page: check if theper_pageattribute is set
π§ prepare_request
this one is a CodeRef and you can use it to modify the HTTP::Request object on a global basis, before it gets sent to the Github API. it's useful for setting MIME types for example. see also: http://developer.github.com/v3/mimes/. this is the right way to go if you want to modify the HTTP request of all API calls. if you only want to change a few, consider sending the prepare_request parameter on any method call instead.
let's use this example from the Github docs:
Html
"application/vnd.github-issue.html+json"
return html rendered from the body's markdown. the response will include body_html.
Examples:
my $p = Pithub::Issues->new(
prepare_request => sub {
my ($request) = @_;
$request->header( Accept => 'application/vnd.github-issue.html+json' );
}
);
my $result = $p->get(
user => 'miyagawa',
repo => 'Plack',
issue_id => 209,
);
print $result->content->{body_html};
please compare to the solution where you set the custom HTTP header on the method call, instead of globally on the object:
my $p = Pithub::Issues->new;
my $result = $p->get(
user => 'miyagawa',
repo => 'Plack',
issue_id => 209,
options => {
prepare_request => sub {
my ($request) = @_;
$request->header( Accept => 'application/vnd.github-issue.html+json' );
},
}
);
print $result->content->{body_html};
π repo
this can be set as a default repo to use for API calls that require the repo parameter to be set. there are sooo many of them, and it gets kind of verbose to include the repo and the user on all of the calls, especially when you want to do a bunch of operations on the same user/repo π
Examples:
my $c = Pithub::Repos::Collaborators->new( repo => 'Pithub' );
my $result = $c->list( user => 'plu' );
there are two helper methods:
clear_repo: reset therepoattributehas_repo: check if therepoattribute is set
π₯ token
if the OAuth token is set, Pithub will send it via an HTTP header on each API request. currently the basic authentication method is not supported. π«
see also: http://developer.github.com/v3/oauth/
β ua
by default this is an LWP::UserAgent object, but honestly it can be anything that implements the same interface π for example, you could also use WWW::Mechanize::Cached to cache requests on disk, so that subsequent runs of your app can run faster and be less likely to exceed rate limits. slay.
π«Ά user
this can be set as a default user to use for API calls that require the user parameter to be set.
Examples:
my $c = Pithub::Repos::Collaborators->new( user => 'plu' );
my $result = $c->list( repo => 'Pithub' );
there are two helper methods:
clear_user: reset theuserattributehas_user: check if theuserattribute is set
it might make sense to use this together with the repo attribute:
my $c = Pithub::Repos::Commits->new( user => 'plu', repo => 'Pithub' );
my $result = $c->list;
my $result = $c->list_comments;
my $result = $c->get('6b6127383666e8ecb41ec20a669e4f0552772363');
π¦ utf8
this can set the utf8 flag β¨
Examples:
my $p = Pithub->new(utf8 => 0); # disable utf8 en/decoding
my $p = Pithub->new(utf8 => 1); # enable utf8 en/decoding (default)
β¨ METHODS β¨
π request
okay so bestie, THIS is the main character of the whole module. every single Pithub class calls this bad boy whenever it needs to talk to GitHub. and here's the tea: if GitHub drops a shiny new API call that Pithub hasn't caught up with yet, you can just call request yourself. it takes a hash with these keys π
methodβ mandatory, bestie, you can't skip it. it's a string and it has to be one of these five:DELETEGETPATCHPOSTPUT
pathβ mandatory string, the relative path you're hitting for the API call.dataβ optional data reference, usually an arrayref or hashref. it must be JSON-serializable, no cap. this becomes the HTTP request body.optionsβ optional hashref for extra request options. right now the only one supported isprepare_request. more on that down in the examples π«Ά. and the cute part? you can use this on every method that maps straight to an API call.paramsβ optional hashref for extraGETparameters. you could do this withprepare_requestinside theoptionshashref too, but this way is shorter. it's literally whatlistin Pithub::Issues uses.
real talk though: most of the time you should never need this method at all. it's here for when Pithub is missing something from the Github v3 API. that said, here are some examples so you can see the vibe β¨
- same as
listin Pithub::Issues:
my $p = Pithub->new;
my $result = $p->request(
method => 'GET',
path => '/repos/plu/Pithub/issues',
params => {
state => 'closed',
direction => 'asc',
}
);
- same as
getin Pithub::Users:
my $p = Pithub->new;
my $result = $p->request(
method => 'GET',
path => '/users/plu',
);
- same as
createin Pithub::Gists:
my $p = Pithub->new;
my $method = 'POST';
my $path = '/gists';
my $data = {
description => 'the description for this gist',
public => 1,
files => { 'file1.txt' => { content => 'String file content' } }
};
my $result = $p->request(
method => $method,
path => $path,
data => $data,
);
- same as
getin Pithub::GitData::Trees:
my $p = Pithub->new;
my $method = 'GET';
my $path = '/repos/miyagawa/Plack/issues/209';
my $data = undef;
my $options = {
prepare_request => sub {
my ($request) = @_;
$request->header( Accept => 'application/vnd.github-issue.html+json' );
},
};
my $result = $p->request(
method => $method,
path => $path,
data => $data,
options => $options,
);
and the promise that always comes through: this method always returns a Pithub::Result object. periodt π
π has_token (?$request)
this one checks whether a token has been set. and if it hasn't, but you passed in a request object? it goes looking for an Authorization header in that request instead. detective mode fr π
π rate_limit
queries the rate limit for the current object and whatever authentication method you're rolling with. know your limits, queen π
π AUTHOR
Johannes Plunien <plu@cpan.org>
π COPYRIGHT AND LICENSE
this software is copyright (c) 2011 by Johannes Plunien.
it's free software, bestie β you can redistribute it and/or modify it under the exact same terms as the Perl 5 programming language system itself. π
and that's a wrap on Pithub::Base(3pm) πΈ this page came from perl v5.40.1 and was dated 2025-06-08 β go forth and slay your API calls, you absolute icon πβ¨π¦
π¦ where this page comes from
- debian trixie: libpithub-perl 0.01043-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libpithub-perl 0.01043-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libpithub-perl 0.01043-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms