Package: pyBRPost
Brazilian Post Office ModuleThis module is to help whoever needs a API to query the brazilian post office to know:When the package will arriveHow much will it costPossibility to query different shipping methodsInstallationTo install this module simply add it via pippip install pyBRPostIt requires some extra modules:pip install requests xmltodictUsageTo use this module all you need is to configure the package information and execute the get_fare()
method. Here's a simple test:from br_posts import fare, options

tst = fare.Fare()
tst.requestServices = [
    options.Service.SEDEX_10_RETAIL,
    options.Service.SEDEX_12,
    options.Service.SEDEX_CASH,
    options.Service.SEDEX_RETAIL,
    options.Service.SEDEX_TODAY_RETAIL,
    options.Service.PAC_CASH,
    options.Service.PAC_RETAIL
]
tst.dimensions['weight'] = 0.875
tst.dimensions['length'] = 16
tst.dimensions['width'] = 11
tst.dimensions['height'] = 10
tst.dimensions['diameter'] = 10
tst.cepDestination = '09571000'
tst.cepOrigin = '24738791'
tst.value = 2500.75
tst.packageFormat = options.ObjectType.BOX
tst.extras['receiving_warning'] = True
try:
    fare_return = tst.get_fare()
except Exception as error:
    print(error)
    exit(-1)
for ret in fare_return:
    print('Service: {serv}'.format(serv=ret['service']))
    error_code = ret['error_code']
    if error_code != 0:
        print('Error ({code}): {desc}'.format(code=error_code, desc=ret['error_msg']))
    else:
        print('Time: {time} day{mult}'.format(time=ret['delivery_time'], mult='s' if ret['delivery_time'] > 1 else ''))
        print('Value: R$ {val}'.format(val=ret['value']))
        print('Deliver on saturday? {sat}'.format(sat=ret['delivery_saturday']))
        print('Delivery on: {delivery_date}'.format(
            delivery_date=tst.get_estimated_delivery_day(add_days=1,
                                                         travel_days=ret['delivery_time'],
                                                         deliver_on_saturday=ret['delivery_saturday']).strftime('%d/%m/%Y')))
        print('')This is a little big, so let's drill down this example in 3 easy steps:Importfrom br_posts import fare, optionsThose are the basic imports, there's also a 'ship.errors' import that you can use to capture
the exceptions.The 'ship.options' file contains the enum classes Service and ObjectType that, are responsible to configure
the methods to send the package, and the format of the package.Those are the options you have for the Service enum:SEDEX_RETAIL - Common Sedex typeSEDEX_TO_CHARGE - Sedex to be charged on the receiverSEDEX_10_RETAIL - Sedex 10SEDEX_TODAY_RETAIL - Sedex on the same daySEDEX_CASH - Sedex payment on cashSEDEX_PAYMENT_ON_DELIVERY - Sedex payment on deliverySEDEX_12 - Sedex 12PAC_RETAIL - Common PACPAC_CASH - PAC on cashPAC_PAYMENT_ON_DELIVERY - PAC with payment on deliveryAnd those are the options you have for the ObjectType enum:BOX - Most common of Sedex for big productsROLL - Can be sometimes referred as "prism"LETTER - Anything that can be sent as ane EnvelopeYou can find more of those options on theCorreios site.SetupAfter importing, you need to instantiate a 'ship.fare.Fare' object, and create the initial setup:tst = fare.Fare()
tst.requestServices = [
    options.Service.SEDEX_10_RETAIL,
    options.Service.SEDEX_12,
    options.Service.SEDEX_CASH,
    options.Service.SEDEX_RETAIL,
    options.Service.SEDEX_TODAY_RETAIL,
    options.Service.PAC_CASH,
    options.Service.PAC_RETAIL
]
tst.dimensions['weight'] = 0.875
tst.dimensions['length'] = 16
tst.dimensions['width'] = 11
tst.dimensions['height'] = 10
tst.dimensions['diameter'] = 10
tst.cepDestination = '09571000'
tst.cepOrigin = '24738791'
tst.value = 2500.75
tst.packageFormat = options.ObjectType.BOX
tst.extras['receiving_warning'] = TrueOf all those options, only those are mandatory:requestServicedimenstionscepDestinationcepOriginpackageFormatIf some of those are missing, or wrong, there's 2 possible exceptions that will be thrown when
executing the get_fare() method:InvalidParameterErrorMissingParametersErrorGetting the fareAfter importing and setting up the information to query, you should call the get_fare() method:try:
    fare_return = tst.get_fare()
except Exception as error:
    print(error)
    exit(-1)This method will POST the Correios API with the setup you created before, if there's anything wrong
with the setup it'll raise an exception. If everything works out smoothly, it'll return a list
with all the results (usually one for 'requestService'), and you can easily read them as a dictionary:for ret in fare_return:
    print('Service: {serv}'.format(serv=ret['service']))
    error_code = ret['error_code']
    if error_code != 0:
        print('Error ({code}): {desc}'.format(code=error_code, desc=ret['error_msg']))
    else:
        print('Time: {time} day{mult}'.format(time=ret['delivery_time'], mult='s' if ret['delivery_time'] > 1 else ''))
        print('Value: R$ {val}'.format(val=ret['value']))
        print('Deliver on saturday? {sat}'.format(sat=ret['delivery_saturday']))There's one "gift" inside this package that hasnothingto do with the Correios API, an
estimated delivery method, that you can call to get a possible date on the delivery:print('Delivery on: {delivery_date}'.format(
            delivery_date=tst.get_estimated_delivery_day(add_days=1,
                                                         travel_days=ret['delivery_time'],
                                                         deliver_on_saturday=ret['delivery_saturday']).strftime('%d/%m/%Y')))
        print('')