OneShopAPI.pm
From Developer's API
(Difference between revisions)
(PERL Package OneShopAPI.pm) |
m (Updated PERL OneShopAPI Module) |
||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
package OneShopAPI; | package OneShopAPI; | ||
+ | use LWP::UserAgent; | ||
use strict; | use strict; | ||
− | |||
sub new | sub new | ||
Line 13: | Line 13: | ||
_merchant_key => $current_merchant_key, # constructor parameter two, optional, merchant api access key | _merchant_key => $current_merchant_key, # constructor parameter two, optional, merchant api access key | ||
_api_uri => $current_api_uri, # constructor parameter three, optional, api uri | _api_uri => $current_api_uri, # constructor parameter three, optional, api uri | ||
− | |||
_ua => undef, | _ua => undef, | ||
_api_request => undef, | _api_request => undef, | ||
Line 43: | Line 42: | ||
sub create_request_xml | sub create_request_xml | ||
{ | { | ||
− | my ($self) = @_; | + | my ($self, $current_parameters) = @_; |
− | my $current_request_body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Request>\n" . $self-> | + | my $current_request_body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Request>\n\t<Key>" . $self->{_merchant_key} . "</Key>\n" . $self->parse_api_parameters($current_parameters) . "</Request>"; |
return($current_request_body); | return($current_request_body); | ||
} | } |
Revision as of 15:54, 28 May 2008
package OneShopAPI; use LWP::UserAgent; use strict; sub new { my ($self,$current_merchant_id,$current_merchant_key,$current_api_uri) = @_; my $class = ref($self) || $self; my $self = { _merchant_id => $current_merchant_id, # constructor parameter one, optional, merchant identification number _merchant_key => $current_merchant_key, # constructor parameter two, optional, merchant api access key _api_uri => $current_api_uri, # constructor parameter three, optional, api uri _ua => undef, _api_request => undef, _api_response => undef }; $self->{_ua} = LWP::UserAgent->new(); return(bless($self, $class)); } sub send_https_request { my ($self, $current_uri, $current_request_body) = @_; $self->{_api_request} = HTTP::Request->new(POST => $current_uri); $self->{_api_request}->header("Content-Type" => "text/xml"); $self->{_api_request}->content($current_request_body); $self->{_api_response} = $self->{_ua}->request($self->{_api_request}); return($self->{_api_response}->content()); } sub api_request { my ($self, $current_method, $current_parameters) = @_; my $current_uri = $self->{_api_uri} . '/API/' . $self->{_merchant_id} . $current_method; my $current_request_body = $self->create_request_xml(); my $current_response = $self->send_https_request($current_uri,$current_request_body); return($current_response); } sub create_request_xml { my ($self, $current_parameters) = @_; my $current_request_body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Request>\n\t<Key>" . $self->{_merchant_key} . "</Key>\n" . $self->parse_api_parameters($current_parameters) . "</Request>"; return($current_request_body); } sub parse_api_parameters { my ($self,$current_parameters) = @_; my $current_xml_parameters = "\t"; while(my ($current_key,$current_value) = each(%{$current_parameters})) { $current_xml_parameters .= "<" . $current_key . ">" . $current_value . "</" . $current_key . ">\n\t" } $current_xml_parameters = substr($current_xml_parameters,0,-1); return($current_xml_parameters); } sub xlink_api_request { my ($self,$current_xlink,$current_parameters) = @_; my $current_request_body = $self->create_request_xml(); my $current_response = $self->send_https_request($current_xlink,$current_request_body); return($current_response); } sub get_orders_list { my ($self) = @_; return($self->api_request("/ORDERS/LIST")); } sub get_order_by_id { my ($self,$current_order_id) = @_; return($self->api_request("/ORDERS/" . $current_order_id . "/READ")); } sub get_products_list { my ($self) = @_; return($self->api_request("/PRODUCTS/LIST")); } sub get_product_by_id { my ($self,$current_product_id) = @_; return($self->api_request("/PRODUCTS/" . $current_product_id . "/READ")); } sub get_clients_list { my ($self) = @_; return($self->api_request("/CLIENTS/LIST")); } sub get_client_by_id { my ($self,$current_client_id) = @_; return($self->api_request("/CLIENTS/" . $current_client_id . "/READ")); } sub get_errors_list { my ($self) = @_; return($self->api_request("/ERRORS/LIST")); } sub get_available_api_methods { my ($self) = @_; return($self->api_request("")); } my $VALID_PERL_MODULE = 1; return $VALID_PERL_MODULE;