| 1 | # Iperntiy::API::Request | ||||||||||
| 2 | # | ||||||||||
| 3 | # Contact: doomy [at] dokuleser [dot] org | ||||||||||
| 4 | # Copyright 2008 Winfried Neessen | ||||||||||
| 5 | # | ||||||||||
| 6 | # $Id$ | ||||||||||
| 7 | # Last modified: [ 2011-01-13 12:32:58 ] | ||||||||||
| 8 | |||||||||||
| 9 | ### Module definitions {{{ | ||||||||||
| 10 | package Ipernity::API::Request; | ||||||||||
| 11 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 4 | our @ISA = qw(Ipernity::API HTTP::Request); | |||||||||
| 17 | 4 | our $VERSION = '0.09'; | |||||||||
| 18 | # }}} | ||||||||||
| 19 | |||||||||||
| 20 | ### Module constructor {{{ | ||||||||||
| 21 | sub new | ||||||||||
| 22 | 3 | ▶ | { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 23 | |||||||||||
| 24 | ### Define class and object and read arguments | ||||||||||
| 25 | 3 | my ( $class, %args ) = @_; | |||||||||
| 26 | 3 | my $self = new HTTP::Request; | |||||||||
| 27 | |||||||||||
| 28 | ## Initalize placeholder for API signature | ||||||||||
| 29 | 3 | $self->{ 'api_sig' } = {}; | |||||||||
| 30 | |||||||||||
| 31 | ### Some static definitions | ||||||||||
| 32 | 3 | $self->method( 'POST' ); | |||||||||
| 33 | 3 | $self->uri( 'http://api.ipernity.com/api/' ); | |||||||||
| 34 | 3 | $self->header( 'User-Agent' => 'Ipernity::API v' . $Ipernity::API::VERSION ); | |||||||||
| 35 | |||||||||||
| 36 | ### Assign arguments to my object | ||||||||||
| 37 | 3 | foreach my $key ( keys %args ) | |||||||||
| 38 | { | ||||||||||
| 39 | |||||||||||
| 40 | 3 | $self->{ 'args' }->{ $key } = $args{ $key }; | |||||||||
| 41 | |||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | ### We need a method to call at least! | ||||||||||
| 45 | 3 | ▶ | warn 'Please provide at least a calling method' unless( defined( $self->{ 'args' }->{ 'method' } ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | ### Reference object to class | ||||||||||
| 48 | 3 | bless $self, $class; | |||||||||
| 49 | 3 | return $self; | |||||||||
| 50 | |||||||||||
| 51 | } | ||||||||||
| 52 | # }}} | ||||||||||
| 53 | |||||||||||
| 54 | ### Encode arguements and build a HTTP request // encode() {{{ | ||||||||||
| 55 | sub encode | ||||||||||
| 56 | 3 | ▶ | { | ||||||||
|
Subroutine
encode: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 57 | ### Get objects | ||||||||||
| 58 | 3 | my $self = shift; | |||||||||
| 59 | |||||||||||
| 60 | ### Build an URI object | ||||||||||
| 61 | 3 | my $uri = URI->new( 'http:' ); | |||||||||
| 62 | |||||||||||
| 63 | ### Build an HTTP valid request URI | ||||||||||
| 64 | 3 | delete( $self->{ 'args' }->{ 'method' } ); | |||||||||
| 65 | 3 | $uri->query_form( $self->{ 'args' } ); | |||||||||
| 66 | 3 | my $content = $uri->query; | |||||||||
| 67 | 3 | my $length = length( $content ); | |||||||||
| 68 | |||||||||||
| 69 | ### Add POST fields to HTTP header | ||||||||||
| 70 | 3 | $self->header( 'Content-Type' => 'application/x-www-form-urlencoded' ); | |||||||||
| 71 | 3 | ▶ | if( defined( $content ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | { | ||||||||||
| 73 | |||||||||||
| 74 | 3 | $self->header( 'Content-Length' => $length ); | |||||||||
| 75 | 3 | $self->content( $content ); | |||||||||
| 76 | |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | } | ||||||||||
| 80 | # }}} | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | 4 | 1; | |||||||||
| 84 | __END__ | ||||||||||