Пример #1
0
    def test_rate_calculation_response(self):
        # Setup dummy objects
        package = Package(24, 8, 8, 8, False, '11218', '11780')
        rate_1 = RateCalculation(package, 24.50, 'Priority 2-Day')
        rate_2 = RateCalculation(package, 42.50, 'Priority 1-Day')

        # Rates are added in no particular ordering
        response = RateCalculationResponse(rate_1, rate_2)

        self.assertEqual(response.cheapest().price, 24.50)
Пример #2
0
    def test_rate_calculation_response(self):
        # Setup dummy objects
        package = Package(24, 8, 8, 8, False, '11218', '11780')
        rate_1 = RateCalculation(package, 24.50, 'Priority 2-Day')
        rate_2 = RateCalculation(package, 42.50, 'Priority 1-Day')

        # Rates are added in no particular ordering
        response = RateCalculationResponse(rate_1, rate_2)

        self.assertEqual(response.cheapest().price, 24.50)
Пример #3
0
    def getRate(self,
                rate_type=DOMESTIC,
                method='ALL',
                detailed=False,
                **kwargs):
        # Extract the package item and compose the XML version of it
        package_xml = '<Package ID="{id}">' + \
                        '<Service>{{method}}</Service>' + \
                        '<ZipOrigination>{origin_zip}</ZipOrigination>' + \
                        '<ZipDestination>{destination_zip}</ZipDestination>' + \
                        '<Pounds>{weight_lb}</Pounds>' + \
                        '<Ounces>{weight_oz}</Ounces>' + \
                        '<Container>{shape}</Container>' + \
                        '<Size>{size}</Size>' + \
                        '<Width>{width}</Width>' + \
                        '<Length>{length}</Length>' + \
                        '<Height>{height}</Height>' + \
                        '<Machinable>true</Machinable>' + \
                    '</Package>'
        package = kwargs.get('package', None)

        # Make sure we got a valid package before continuing
        if package is None:
            raise TypeError('`package` is a required argument (received None)')

        # Store the packages XML representatiojn for later use/debugging
        package._xml = package_xml.format(id='0',
                                          origin_zip=package.origin,
                                          destination_zip=package.destination,
                                          weight_lb=unicode(package.weight[0]),
                                          weight_oz=unicode(package.weight[1]),
                                          shape=package.shape,
                                          size=package.size,
                                          width=unicode(package.width),
                                          length=unicode(package.length),
                                          height=unicode(package.height))

        # The reason we format the method second is so that the getDetailedRates code can speicify without going crazy with string parsing.
        params = {
            'package': package._xml.format(
                method=method
            )  # We only allow a single method type since documentation for multiple is poor :/.
        }

        # Make a request for the rate-level information
        raw_response = super(USPSCourier,
                             self).get_server_response(getattr(
                                 self, rate_type + '_rate_endpoint'),
                                                       params,
                                                       method='Rate')

        # Extract the XML data from the parsed response
        package_info = raw_response.find('Package')

        # Check to see if we got a valid response
        error = package_info.find('Error')
        if error is not None:
            return self.process_exception(error)

        # Gather all of the rates that got returned
        raw_rates = package_info.findall('Postage')

        # Create the TrackingResponse and TrackingEvents
        response = RateCalculationResponse()
        for rate in raw_rates:
            response.add(
                RateCalculation(
                    package,
                    rate.find('Rate').text,
                    HTMLParser.HTMLParser().unescape(
                        rate.find('MailService').text)))

        return response
Пример #4
0
    def getRate(self, rate_type=DOMESTIC, method='ALL', detailed=False, **kwargs):
        # Extract the package item and compose the XML version of it
        package_xml = '<Package ID="{id}">' + \
                        '<Service>{{method}}</Service>' + \
                        '<ZipOrigination>{origin_zip}</ZipOrigination>' + \
                        '<ZipDestination>{destination_zip}</ZipDestination>' + \
                        '<Pounds>{weight_lb}</Pounds>' + \
                        '<Ounces>{weight_oz}</Ounces>' + \
                        '<Container>{shape}</Container>' + \
                        '<Size>{size}</Size>' + \
                        '<Width>{width}</Width>' + \
                        '<Length>{length}</Length>' + \
                        '<Height>{height}</Height>' + \
                        '<Machinable>true</Machinable>' + \
                    '</Package>'
        package = kwargs.get('package', None)

        # Make sure we got a valid package before continuing
        if package is None:
            raise TypeError('`package` is a required argument (received None)')

        # Store the packages XML representatiojn for later use/debugging
        package._xml = package_xml.format(
                id='0',
                origin_zip=package.origin,
                destination_zip=package.destination,
                weight_lb=unicode(package.weight[0]),
                weight_oz=unicode(package.weight[1]),
                shape=package.shape,
                size=package.size,
                width=unicode(package.width),
                length=unicode(package.length),
                height=unicode(package.height)
            )

        # The reason we format the method second is so that the getDetailedRates code can speicify without going crazy with string parsing.
        params = {
            'package': package._xml.format(method=method)   # We only allow a single method type since documentation for multiple is poor :/.
        }

        # Make a request for the rate-level information
        raw_response = super(USPSCourier, self).get_server_response(getattr(self, rate_type + '_rate_endpoint'), params, method='Rate')

        # Extract the XML data from the parsed response
        package_info = raw_response.find('Package')

        # Check to see if we got a valid response
        error = package_info.find('Error')
        if error is not None:
            return self.process_exception(error)

        # Gather all of the rates that got returned
        raw_rates = package_info.findall('Postage')

        # Create the TrackingResponse and TrackingEvents
        response = RateCalculationResponse()
        for rate in raw_rates:
            response.add(
                RateCalculation(
                    package,
                    rate.find('Rate').text,
                    HTMLParser.HTMLParser().unescape(rate.find('MailService').text)
                )
            )

        return response