| 1 | package Business::FedEx::ShipAPI; | ||
| 2 | |||
| 3 | #use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our @ISA = qw(Business::FedEx); | ||
| 7 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | ||
| 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 9 | our @EXPORT = qw(); | ||
| 10 | |||
| 11 | our $VERSION = '0.01'; | ||
| 12 | use Business::FedEx; | ||
| 13 | use Win32::API (); | ||
| 14 | use Business::FedEx::Constants qw(:all); | ||
| 15 | |||
| 16 | #Must use a realy large buffer for gif files to flow through nicely | ||
| 17 | use constant RECEIVE_BUFFER_LENGTH => 32767; | ||
| 18 | |||
| 19 | our $errstr = ""; | ||
| 20 | our $err = 0; | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $proto = shift; | ||
| 24 | my $class = ref($proto) || $proto; | ||
| 25 | my %args = (host => '127.0.0.1', | ||
| 26 | port => 6970, | ||
| 27 | username => '', | ||
| 28 | password => '', | ||
| 29 | Errstr => \$errstr, | ||
| 30 | Err => \$err, | ||
| 31 | @_); | ||
| 32 | |||
| 33 | my $self = $class->SUPER::new(%args); | ||
| 34 | return undef unless _init($self); | ||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub DESTROY { | ||
| 39 | # warn "DESTROYING: ", ref(shift), "\n"; | ||
| 40 | } | ||
| 41 | |||
| 42 | { | ||
| 43 | my $_WEBAPIConnect; | ||
| 44 | my $_WEBAPIDisconnect; | ||
| 45 | my $_WEBAPITransaction; | ||
| 46 | my $_WEBAPISetLogFile; | ||
| 47 | my $_WEBAPISetLogMode; | ||
| 48 | my $_WEBAPISetTraceFile; | ||
| 49 | my $_WEBAPISetTraceMode; | ||
| 50 | my $_WEBAPISend; | ||
| 51 | my $_WEBAPIReceive; | ||
| 52 | my $_WEBAPISetReadTimeout; | ||
| 53 | |||
| 54 | sub _init { | ||
| 55 | # initialize the API DLL calls | ||
| 56 | # shift self for the error handling routines | ||
| 57 | my $self = shift; | ||
| 58 | eval { | ||
| 59 | $_WEBAPIConnect = Win32::API->new(WEBAPICLIENT, WEBAPIConnect, ['P', 'N', 'P', 'P'], 'N') or die $^E; | ||
| 60 | $_WEBAPIDisconnect = Win32::API->new(WEBAPICLIENT, WEBAPIDisconnect, [], 'N') or die $^E; | ||
| 61 | $_WEBAPITransaction = Win32::API->new(WEBAPICLIENT, WEBAPITransaction, ['P', 'N', 'P', 'N', 'N'], 'N') or die $^E; | ||
| 62 | $_WEBAPISetLogFile = Win32::API->new(WEBAPICLIENT, WEBAPISetLogFile, ['P'], 'N') or die $^E; | ||
| 63 | $_WEBAPISetLogMode = Win32::API->new(WEBAPICLIENT, WEBAPISetLogMode, ['N'], 'N') or die $^E; | ||
| 64 | $_WEBAPISetTraceFile = Win32::API->new(WEBAPICLIENT, WEBAPISetTraceFile, ['P'], 'N') or die $^E; | ||
| 65 | $_WEBAPISetTraceMode = Win32::API->new(WEBAPICLIENT, WEBAPISetTraceMode, ['N'], 'N') or die $^E; | ||
| 66 | $_WEBAPISetReadTimeout = Win32::API->new(WEBAPICLIENT, WEBAPISetReadTimeout, ['N'], 'N') or die $^E; | ||
| 67 | }; | ||
| 68 | if ($@) { | ||
| 69 | print "DIED: " . $@; | ||
| 70 | $self->SUPER::set_err(-1, "Error in _init: $@"); | ||
| 71 | return 0; | ||
| 72 | } | ||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub connect { | ||
| 77 | my $self = shift; | ||
| 78 | my %args = (host => $self->{'host'}, | ||
| 79 | port => $self->{'port'}, | ||
| 80 | username => $self->{'username'}, | ||
| 81 | password => $self->{'password'}, | ||
| 82 | @_ | ||
| 83 | ); | ||
| 84 | |||
| 85 | my $ret = $_WEBAPIConnect->Call($args{'host'}, | ||
| 86 | $args{'port'}, | ||
| 87 | $args{'username'}, | ||
| 88 | $args{'password'} | ||
| 89 | ); | ||
| 90 | |||
| 91 | if ($ret != WEBAPI_OK) { | ||
| 92 | $self->SUPER::set_err($ret, "Error in connect ($ret): " . FedEx::Constants::lookup_errstr($ret)); | ||
| 93 | return 0 | ||
| 94 | } | ||
| 95 | |||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub disconnect { | ||
| 100 | $_WEBAPIDisconnect->Call(); | ||
| 101 | return 1; | ||
| 102 | } | ||
| 103 | |||
| 104 | # int WEBAPITransaction(char *sBuf, int sBufLen, char *rBuf, int rBufLen, int *actualRecvBufLen); | ||
| 105 | sub transaction { | ||
| 106 | my $self = shift; | ||
| 107 | |||
| 108 | # do we need to manipulate this in any way? add \0 at end? | ||
| 109 | my $buf = (shift); # . "\0"; #null terminate? | ||
| 110 | my $len_buf = length($buf); | ||
| 111 | |||
| 112 | my $rbuf = " " x RECEIVE_BUFFER_LENGTH; # return buffer, need it have a size? | ||
| 113 | my $len_rbuf = length($rbuf); | ||
| 114 | my $actual_rbuf; # the actual bytes read in the recieve buffer | ||
| 115 | |||
| 116 | # not sure what needs to be passed by reference, but this seems to work :) | ||
| 117 | my $ret = $_WEBAPITransaction->Call($buf, | ||
| 118 | $len_buf, | ||
| 119 | $rbuf, | ||
| 120 | $len_rbuf, | ||
| 121 | \$actual_rbuf); | ||
| 122 | |||
| 123 | #print "FH: $openfile\n"; | ||
| 124 | # not exactly, we need to parse the response code | ||
| 125 | # here and then set an error accordingly...the error | ||
| 126 | # will be sent back by FedEx | ||
| 127 | if ($ret != WEBAPI_OK) { | ||
| 128 | $self->SUPER::set_err($ret, "Error in transaction ($ret): " . Business::FedEx::Constants::lookup_errstr($ret)); | ||
| 129 | return "" | ||
| 130 | } | ||
| 131 | |||
| 132 | return $rbuf; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | 1; | ||
| 138 | __END__ |