SOAP Despatcher

From PeformIQ Upgrade
Revision as of 11:55, 25 January 2008 by PeterHarding (talk | contribs)
Jump to navigation Jump to search

Python HTTPLIB version

$ cat desp_02.py 
#!/usr/bin/env python

import os
import binascii
import httplib
import urllib

from copy import copy

#-------------------------------------------------------------------------------

post_data = urllib.urlencode({'spam': 1, 'eggs': 2})

#   "Content-type" : "application/x-www-form-urlencoded",

#-------------------------------------------------------------------------------

post_headers = {
   'Content-type'    : 'text/xml;charset=UTF-8',
   'SOAPAction'      : '',
   'User-Agent'      : 'Jakarta Commons-HttpClient/3.0.1',
   'Host'            : 'hx403'
}


payload_constants = (13, 21110, 603010230)

# post_data = {}

# ue_post_data = urllib.urlencode(post_data)

#-------------------------------------------------------------------------------

payload_template = """<?xml version="1.0" encoding="UTF-8"?>
<PCMS xmlns="http://www.auspost.com.au/xml/pcms">
  <SendPCMSManifest>
    <header>
      <TransactionDateTime>2008-01-10T11:30:00.0Z</TransactionDateTime>
      <TransactionId>AP-0000000001</TransactionId>
      <TransactionSequence>1</TransactionSequence>
      <ApplicationId>MERCHANT</ApplicationId>
    </header>
    <body>
      <PCMSManifest>
        <MerchantLocationId>ALG</MerchantLocationId>
        <ManifestNumber>P%d</ManifestNumber>
        <DateSubmitted>2008-01-21T11:30:00.0Z</DateSubmitted>
        <DateLodged>2008-01-21T11:30:00.0Z</DateLodged>
        <PCMSConsignment>
          <ConsignmentNumber>ALG%07d</ConsignmentNumber>
          <ChargeCode>S1</ChargeCode>
          <InternalChargebackAccount>Finance</InternalChargebackAccount>
          <ReferenceNo1>Job #1</ReferenceNo1>
          <ReferenceNo2>Invoice #1</ReferenceNo2>
          <DeliveryName>Joe Bloggs</DeliveryName>
          <DeliveryCompanyName>Joe's Garage</DeliveryCompanyName>
          <DeliveryAddressLine1>Level 22</DeliveryAddressLine1>
          <DeliveryAddressLine2>150 Lonsdale St</DeliveryAddressLine2>
          <DeliveryAddressLine3>Off Russell St</DeliveryAddressLine3>
          <DeliveryAddressLine4>Corner of Lonsdale St</DeliveryAddressLine4>
          <DeliveryPhoneNumber>+61398989898</DeliveryPhoneNumber>
          <DeliveryEmailAddress>joe.bloggs@test.com.au</DeliveryEmailAddress>
          <DeliverySuburb>BUSSELTON</DeliverySuburb>
          <DeliveryStateCode>WA</DeliveryStateCode>
          <DeliveryPostcode>6280</DeliveryPostcode>
          <DeliveryCountryCode>AU</DeliveryCountryCode>
          <DeliveryInstructions>Pick up parcel.</DeliveryInstructions>
          <IsInternationalDelivery>false</IsInternationalDelivery>
          <ReturnName>Rocket Records P/L</ReturnName>
          <ReturnAddressLine1>58-76 Stephenson Rd</ReturnAddressLine1>
          <ReturnSuburb>Cremorne</ReturnSuburb>
          <ReturnStateCode>VIC</ReturnStateCode>
          <ReturnPostcode>3121</ReturnPostcode>
          <ReturnCountryCode>AU</ReturnCountryCode>
          <CreatedDateTime>2008-01-21T12:18:32.106+11:00</CreatedDateTime>
          <PostChargeToAccount>5903000</PostChargeToAccount>
          <IsSignatureRequired>Y</IsSignatureRequired>
          <CTCAmount>0.00</CTCAmount>
          <DeliverPartConsignment>N</DeliverPartConsignment>
          <ContainsDangerousGoods>false</ContainsDangerousGoods>
          <PCMSDomesticArticle>
            <ArticleNumber>ALG%013d</ArticleNumber>
            <BarcodeArticleNumber>99700122BBD100000101011503000</BarcodeArticleNumber>
            <Length>12</Length>
            <Width>14</Width>
            <Height>16</Height>
            <ActualWeight>0.5</ActualWeight>
            <CubicWeight>0.5</CubicWeight>
            <ArticleDescription>SOAP Test Article</ArticleDescription>
            <IsTransitCoverRequired>N</IsTransitCoverRequired>
            <TransitCoverAmount>0</TransitCoverAmount>
            <ContentsItem>
              <GoodsDescription>SOAP Test Goods</GoodsDescription>
              <Weight>0.5</Weight>
              <Quantity>1</Quantity>
              <UnitValue>100.00</UnitValue>
              <Value>100</Value>
            </ContentsItem>
          </PCMSDomesticArticle>
        </PCMSConsignment>
      </PCMSManifest>
    </body>
  </SendPCMSManifest>
</PCMS>"""

#-------------------------------------------------------------------------------

soap_env = """<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:epar="http://www.auspost.com.au/eParcel">
<soapenv:Header/>
<soapenv:Body>
<epar:submitManifestForDespatch soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<string xsi:type="xsd:string"><![CDATA[%s]]></string></epar:submitManifestForDespatch></soapenv:Body>
</soapenv:Envelope>"""


#-------------------------------------------------------------------------------

cookie  = binascii.b2a_base64("xxxxx:xxxxx")[:-1]

print '[%s]' % cookie

headers = copy(post_headers)

headers['Authorization']  = 'Basic %s' % cookie
headers['Content-Length'] = len(post_data)

print headers

DO    = 'POST'
URL   =  '/despatchManifest/DespatchManifestWS'
HOST  = 'HOST'

manifest = payload_template % payload_constants

print manifest

post_data = soap_env % manifest

print post_data

#-------------------------------------------------------------------------------

conn  = httplib.HTTPConnection(HOST)

conn.request(DO, URL, post_data_01, headers)

response = conn.getresponse()

print response.status, response.reason

data = response.read()

print data

conn.close()

#-------------------------------------------------------------------------------