Paste: ups api vocabulary

Author: erg
Mode: factor
Date: Thu, 25 Oct 2012 17:55:20
Plain Text |
! Copyright (C) 2012 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: http.client kernel namespaces sequences urls
xml.syntax xml.writer io.encodings.utf8 io.encodings.string
xml ;
IN: ups

! Get these from https://www.ups.com/upsdeveloperkit?loc=en_US
SYMBOL: ups-user
SYMBOL: ups-password
SYMBOL: ups-secret-access-key

! Servers
CONSTANT: ups-address-validation-server URL" https://wwwcie.ups.com/ups.app/xml/AV"
CONSTANT: ups-tracking-server URL" https://wwwcie.ups.com/ups.app/xml/Track"

! Common
: make-access-request-xml ( -- xml )
    ups-secret-access-key ups-user ups-password [ get-global ] tri@
    <XML 
        <AccessRequest>
            <AccessLicenseNumber><-></AccessLicenseNumber>
            <UserId><-></UserId>
            <Password><-></Password>
        </AccessRequest>
    XML> ;

: make-transaction-reference-xml ( customer-context xpci-version -- xml )
    <XML
        <TransactionReference>
            <CustomerContext><-></CustomerContext>
            <XpciVersion><-></XpciVersion>
        </TransactionReference>
    XML> ;

: make-request-xml ( action customer-context xpci-version -- xml )
    make-transaction-reference-xml
    <XML
        <Request>
            <RequestAction><-></RequestAction>
            <->
            <RequestOption>1</RequestOption>
        </Request>
    XML> ;

! Address validation
: make-city-state-request-xml ( city state -- xml )
    <XML
        <Address>
            <City><-></City>
            <StateProvinceCode><-></StateProvinceCode>
        </Address>
    XML> ;

: make-address-validation-request-xml ( city state -- xml )
    make-city-state-request-xml
    "AV" "TestGuy" "1.0001" make-request-xml

    <XML
        <AddressValidationRequest xml:lang="en-US">
            <->
            <->
        </AddressValidationRequest>
    XML> ;

: do-ups-request ( xml server -- xml )
    http-post nip
    utf8 decode string>xml ;

: do-address-request ( city state -- xml )
    [ make-access-request-xml ] 2dip
    make-address-validation-request-xml
    [ xml>string ] bi@ append
    ups-address-validation-server do-ups-request ;

! Tracking
: make-tracking-request-xml ( tracking-number-string -- xml )
    "Track" "TestGuy" "1.0001" make-request-xml
    <XML
        <TrackRequest>
            <TrackingNumber><-></TrackingNumber>
            <->
        </TrackRequest>
    XML> ;

: do-tracking-request ( tracking-number-string -- xml )
    [ make-access-request-xml ] dip
    make-tracking-request-xml
    [ xml>string ] bi@ "\n" glue
    ups-tracking-server do-ups-request ;

New Annotation

Summary:
Author:
Mode:
Body: