| 1 | package Imgur::API; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | our $VERSION = "0.1.3"; | ||
| 5 | our $ABSTRACT = "Perl Interface to Imgur API"; | ||
| 6 | use feature qw(say); | ||
| 7 | |||
| 8 | use Imgur::API::Endpoint; | ||
| 9 | use Imgur::API::Exception; | ||
| 10 | use Imgur::API::Content; | ||
| 11 | use Imgur::API::Response; | ||
| 12 | use Imgur::API::Stats; | ||
| 13 | use Data::Dumper; | ||
| 14 | use LWP::UserAgent; | ||
| 15 | use HTTP::Message; | ||
| 16 | use HTTP::Request; | ||
| 17 | use JSON::XS; | ||
| 18 | use URI::Escape; | ||
| 19 | |||
| 20 | use Moo; | ||
| 21 | |||
| 22 | has client_secret=>(is=>'ro'); | ||
| 23 | has client_id=>(is=>'ro',required=>1); | ||
| 24 | has access_token=>(is=>'rw'); | ||
| 25 | has ua=>(is=>'ro',default=>sub { LWP::UserAgent->new(); }); | ||
| 26 | has stats=>(is=>'rw',default=>sub { Imgur::API::Stats->new();}); | ||
| 27 | |||
| 28 | sub request { | ||
| 29 | my ($this,$path,$method,$params) = @_; | ||
| 30 | |||
| 31 | $params->{_format}="json"; | ||
| 32 | |||
| 33 | $this->ua->agent("Imgur::API/0.0,1"); | ||
| 34 | |||
| 35 | my $auth; | ||
| 36 | if ($this->access_token) { | ||
| 37 | $auth="Bearer ".$this->access_token; | ||
| 38 | } else { | ||
| 39 | $auth="Client-ID ".$this->client_id; | ||
| 40 | } | ||
| 41 | |||
| 42 | say STDERR $auth; | ||
| 43 | |||
| 44 | my $response; | ||
| 45 | if ($method=~/(?:post|put)/) { | ||
| 46 | $response = $this->ua->$method($path,$params,'Authorization'=>$auth); | ||
| 47 | } else { | ||
| 48 | $response = $this->ua->$method($path,'Authorization'=>$auth); | ||
| 49 | } | ||
| 50 | say Dumper($response); | ||
| 51 | if ($response->content_type eq "application/json") { | ||
| 52 | $this->stats->update($response); | ||
| 53 | my $json = JSON::XS::decode_json($response->decoded_content); | ||
| 54 | if (!$json->{success}) { | ||
| 55 | my $e = Imgur::API::Exception->new(code=>$json->{status},message=>$json->{data}->{error}); | ||
| 56 | say Dumper($json); | ||
| 57 | } | ||
| 58 | |||
| 59 | return Imgur::API::Response->new($json); | ||
| 60 | } else { | ||
| 61 | return Imgur::API::Exception->new(code=>$response->code,message=>$response->status_line); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub content { | ||
| 66 | my ($this,$what) = @_; | ||
| 67 | |||
| 68 | if ($what=~/^http/i) { | ||
| 69 | return $what; | ||
| 70 | } elsif (-f $what) { | ||
| 71 | return Imgur::API::Content->encode($what); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub account { | ||
| 76 | my ($this) = shift; | ||
| 77 | |||
| 78 | return Imgur::API::Endpoint::Account->new(dispatcher=>$this); | ||
| 79 | } | ||
| 80 | sub album { | ||
| 81 | my ($this) = shift; | ||
| 82 | |||
| 83 | return Imgur::API::Endpoint::Album->new(dispatcher=>$this); | ||
| 84 | } | ||
| 85 | sub comment { | ||
| 86 | my ($this) = shift; | ||
| 87 | |||
| 88 | return Imgur::API::Endpoint::Comment->new(dispatcher=>$this); | ||
| 89 | } | ||
| 90 | sub conversation { | ||
| 91 | my ($this) = shift; | ||
| 92 | |||
| 93 | return Imgur::API::Endpoint::Conversation->new(dispatcher=>$this); | ||
| 94 | } | ||
| 95 | sub custom_gallery { | ||
| 96 | my ($this) = shift; | ||
| 97 | |||
| 98 | return Imgur::API::Endpoint::Custom_gallery->new(dispatcher=>$this); | ||
| 99 | } | ||
| 100 | sub gallery { | ||
| 101 | my ($this) = shift; | ||
| 102 | |||
| 103 | return Imgur::API::Endpoint::Gallery->new(dispatcher=>$this); | ||
| 104 | } | ||
| 105 | sub image { | ||
| 106 | my ($this) = shift; | ||
| 107 | |||
| 108 | return Imgur::API::Endpoint::Image->new(dispatcher=>$this); | ||
| 109 | } | ||
| 110 | sub memegen { | ||
| 111 | my ($this) = shift; | ||
| 112 | |||
| 113 | return Imgur::API::Endpoint::Memegen->new(dispatcher=>$this); | ||
| 114 | } | ||
| 115 | sub notification { | ||
| 116 | my ($this) = shift; | ||
| 117 | |||
| 118 | return Imgur::API::Endpoint::Notification->new(dispatcher=>$this); | ||
| 119 | } | ||
| 120 | sub topic { | ||
| 121 | my ($this) = shift; | ||
| 122 | |||
| 123 | return Imgur::API::Endpoint::Topic->new(dispatcher=>$this); | ||
| 124 | } | ||
| 125 | sub misc { | ||
| 126 | my ($this) = shift; | ||
| 127 | |||
| 128 | return Imgur::API::Endpoint::Misc->new(dispatcher=>$this); | ||
| 129 | } | ||
| 130 | sub oauth { | ||
| 131 | my ($this) = shift; | ||
| 132 | |||
| 133 | return Imgur::API::Endpoint::OAuth->new(dispatcher=>$this); | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | 1; |