Skip to content

Return PDF shouldn't assume that type is issuedLite by default #2

@xtornasol512

Description

@xtornasol512

facturama.version = '2.0.3'
Metodo = facturama.Cfdi

Los siguientes metodos: saveAsPdf, saveAsXml y saveAsHtml no cumplen con lo necesitado ya que asumen en el código que sea issuedLite la petición del cfdi y no siempre es así.

Estaría mejor agregar el tipo como parametro, para esto solo leyendo el código se puede observar que para este fin tienen get_by_file("pdf", "issued", "<ID-CFDI>") y regresa el json que esperamos pero nos toca a mano la conversión. Dejo el código citado

# replicate facturama cfdi docs and create a cfdi object
cfdi = facturama.Cfdi.create(cfdi_object)
# Get the ID
print cfdi.Id
>>> "QilvK5E6QtHPxGmMzcYd8"
# try to get pdf 
file = facturama.Cfdi.saveAsPdf("QilvK5E6QtHPxGmMzcYd8", "filename.pdf")
>>> cfdi.saveAsPdf("QilvK5E6QtHPxGmMzcYd8", "filename.pdf"
... )
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    cfdi.saveAsPdf("QilvK5E6QtHPxGmMzcYd8", "filename.pdf"
  File "/vagrant/prescript/src/facturama/facturama/__init__.py", line 228, in saveAsPdf
    html_file = cls.get_by_file('pdf', 'IssuedLite',oid)
  File "/vagrant/prescript/src/facturama/facturama/__init__.py", line 221, in get_by_file
    return cls.build_http_request('get', '{}/{}/{}/{}'.format(cls.__name__, f, t, oid))
  File "/vagrant/prescript/src/facturama/facturama/__init__.py", line 116, in build_http_request
    raise ResourceNotFoundError(body.json())
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Esta forma si se obtiene el payload que se espera, pero sin convertir de base64

# try to get PDF using get_by_file
response = cfdi.get_by_file("pdf", "issued", "QilvK5E6QtHPxGmMzcYd8")
print response
>>> {
    "ContentEncoding": "base64",
    "ContentType": "xml",
    "ContentLength": 4960,
    "Content": "PD94bWwgdmVyc2lvbj0iMS4wIiBlbm==..."}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions