OneShopAPI.pm
From Developer's API
(Difference between revisions)
m (Updated PERL OneShopAPI Module) |
m (Modified a Regular Expression in api_parameter_build to strip trailing whitespace.) |
||
(4 intermediate revisions by one user not shown) | |||
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 | ||
+ | _api_parameters => {Key => $current_merchant_key}, | ||
_ua => undef, | _ua => undef, | ||
_api_request => undef, | _api_request => undef, | ||
Line 19: | Line 20: | ||
$self->{_ua} = LWP::UserAgent->new(); | $self->{_ua} = LWP::UserAgent->new(); | ||
return(bless($self, $class)); | return(bless($self, $class)); | ||
+ | } | ||
+ | |||
+ | sub api_parameter_add | ||
+ | { | ||
+ | my ($self,$current_key,$current_value) = @_; | ||
+ | # add parameter | ||
+ | $self->{_api_parameters}->{$current_key} = $current_value; | ||
+ | } | ||
+ | |||
+ | sub api_parameter_delete | ||
+ | { | ||
+ | my ($self,$current_key) = @_; | ||
+ | delete($self->{_api_parameters}->{$current_key}); | ||
+ | } | ||
+ | |||
+ | sub api_parameter_clear | ||
+ | { | ||
+ | my ($self) = @_; | ||
+ | $self->{_api_parameters} = (); | ||
+ | $self->{_api_parameters}->{Key} = $self->{_merchant_key}; | ||
+ | } | ||
+ | |||
+ | sub api_parameter_build | ||
+ | { | ||
+ | my ($self,$current_response_body) = @_; | ||
+ | my @current_results = (); | ||
+ | # locate <NextRecordSet> within response body | ||
+ | if(@current_results = ($current_response_body =~ /\s*<NextRecordSet>(?:\s*<\w+>[^<>]*<\/\w+>\s*)*<\/NextRecordSet>/g)) | ||
+ | { | ||
+ | $self->api_parameter_clear(); | ||
+ | my $current_buffer = @current_results[0]; | ||
+ | # strip <[\/]?NextRecordSet> from buffer | ||
+ | $current_buffer =~ s/\s*<[\/]?NextRecordSet>\s*//g; | ||
+ | # retrieve child keys and values | ||
+ | if(@current_results = ($current_buffer =~ /\s*<(\w+)>\s*([^<>]*)<\/\1>\s*/g)) | ||
+ | { | ||
+ | # for each result | ||
+ | for(my $current_index = 0;$current_index < scalar(@current_results);$current_index += 2) | ||
+ | { | ||
+ | # trim trailing whitespace from value | ||
+ | @current_results[$current_index + 1] =~ s/\s+$//g; | ||
+ | # add parameter key and value pair | ||
+ | $self->api_parameter_add(@current_results[$current_index],@current_results[$current_index + 1]); | ||
+ | } | ||
+ | } | ||
+ | } | ||
} | } | ||
Line 33: | Line 80: | ||
sub api_request | sub api_request | ||
{ | { | ||
− | my ($self, $current_method | + | my ($self, $current_method) = @_; |
my $current_uri = $self->{_api_uri} . '/API/' . $self->{_merchant_id} . $current_method; | my $current_uri = $self->{_api_uri} . '/API/' . $self->{_merchant_id} . $current_method; | ||
− | my $current_request_body = $self->create_request_xml(); | + | my $current_request_body = $self->create_request_xml($self->{_api_parameters}); |
my $current_response = $self->send_https_request($current_uri,$current_request_body); | my $current_response = $self->send_https_request($current_uri,$current_request_body); | ||
return($current_response); | return($current_response); | ||
Line 43: | Line 90: | ||
{ | { | ||
my ($self, $current_parameters) = @_; | my ($self, $current_parameters) = @_; | ||
− | my $current_request_body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Request | + | my $current_request_body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Request>\n" . $self->parse_api_parameters($current_parameters) . "</Request>"; |
return($current_request_body); | return($current_request_body); | ||
} | } | ||
Line 62: | Line 109: | ||
{ | { | ||
my ($self,$current_xlink,$current_parameters) = @_; | my ($self,$current_xlink,$current_parameters) = @_; | ||
− | my $current_request_body = $self->create_request_xml(); | + | my $current_request_body = $self->create_request_xml($current_parameters); |
my $current_response = $self->send_https_request($current_xlink,$current_request_body); | my $current_response = $self->send_https_request($current_xlink,$current_request_body); | ||
return($current_response); | return($current_response); | ||
Line 116: | Line 163: | ||
my $VALID_PERL_MODULE = 1; | my $VALID_PERL_MODULE = 1; | ||
− | return $VALID_PERL_MODULE; | + | return $VALID_PERL_MODULE;</pre> |
− | </pre> | + |
Latest revision as of 13:36, 29 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 _api_parameters => {Key => $current_merchant_key}, _ua => undef, _api_request => undef, _api_response => undef }; $self->{_ua} = LWP::UserAgent->new(); return(bless($self, $class)); } sub api_parameter_add { my ($self,$current_key,$current_value) = @_; # add parameter $self->{_api_parameters}->{$current_key} = $current_value; } sub api_parameter_delete { my ($self,$current_key) = @_; delete($self->{_api_parameters}->{$current_key}); } sub api_parameter_clear { my ($self) = @_; $self->{_api_parameters} = (); $self->{_api_parameters}->{Key} = $self->{_merchant_key}; } sub api_parameter_build { my ($self,$current_response_body) = @_; my @current_results = (); # locate <NextRecordSet> within response body if(@current_results = ($current_response_body =~ /\s*<NextRecordSet>(?:\s*<\w+>[^<>]*<\/\w+>\s*)*<\/NextRecordSet>/g)) { $self->api_parameter_clear(); my $current_buffer = @current_results[0]; # strip <[\/]?NextRecordSet> from buffer $current_buffer =~ s/\s*<[\/]?NextRecordSet>\s*//g; # retrieve child keys and values if(@current_results = ($current_buffer =~ /\s*<(\w+)>\s*([^<>]*)<\/\1>\s*/g)) { # for each result for(my $current_index = 0;$current_index < scalar(@current_results);$current_index += 2) { # trim trailing whitespace from value @current_results[$current_index + 1] =~ s/\s+$//g; # add parameter key and value pair $self->api_parameter_add(@current_results[$current_index],@current_results[$current_index + 1]); } } } } 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) = @_; my $current_uri = $self->{_api_uri} . '/API/' . $self->{_merchant_id} . $current_method; my $current_request_body = $self->create_request_xml($self->{_api_parameters}); 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" . $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($current_parameters); 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;