Coder Social home page Coder Social logo

alphagov / notifications-python-client Goto Github PK

View Code? Open in Web Editor NEW
19.0 17.0 22.0 867 KB

Python client for the GOV.UK Notify API

Home Page: https://docs.notifications.service.gov.uk/python.html

License: MIT License

Shell 0.87% Python 95.49% Makefile 2.02% Dockerfile 1.63%
govuk-notify government-as-a-platform python

notifications-python-client's Introduction

notifications-python-client's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

notifications-python-client's Issues

MS Excel opens CSV files with incorrect encoding

This issue is only tangentially related to the notify service, but I wanted to flag it for discussion.

When CSV files are opened by MS Excel, Excel assumes that the file is ASCII encoded - unless a Byte Order Mark is specified in the first 3 octets of the file to denote the encoding. Further details can be found here: https://stackoverflow.com/a/155176

The result of Excel assuming the incorrect encoding is that special characters are rendered incorrectly to the user.

Excel's behaviour here is quite different to other standard spreadsheet applications. Calc on Linux and Numbers on Mac do a pretty good job of automatically detecting the encoding of the file.

I believe that this issue is a good one to discuss now that the Notify service supports CSV file uploads/downloads fully with the addition of is_csv to prepare_upload.

A very simple idea that I have is to call this behaviour out in the client docs (there's a new section on CSV uploads) - in order to get ahead of this issue for developers that are looking to send CSVs from their apps.

Example Code: CSV which is badly rendered in Excel:

import io
from notifications_python_client import prepare_upload
from notifications_python_client.notifications import NotificationsAPIClient

notifications_client = NotificationsAPIClient("XXX")

csv_contents = 'Büyükdere Cad,foo,bar'
buf = io.BytesIO(csv_contents.encode('utf-8'))
file_content = prepare_upload(buf, is_csv=True)

notifications_client.send_email_notification(
    email_address='[email protected]',
    template_id='XXX',
    personalisation={
        'link_to_file': file_content,
    },
)

Example Code: CSV which is rendered correctly in Excel:

import codecs
import io
from notifications_python_client import prepare_upload
from notifications_python_client.notifications import NotificationsAPIClient

notifications_client = NotificationsAPIClient("XXX")

csv_contents = 'Büyükdere Cad,foo,bar'
buf = io.BytesIO(codecs.BOM_UTF8 + csv_contents.encode('utf-8'))
file_content = prepare_upload(buf, is_csv=True)

notifications_client.send_email_notification(
    email_address='[email protected]',
    template_id='XXX',
    personalisation={
        'link_to_file': file_content,
    },
)

A proposal for fixing CSV downloads in notify

The Problem

GOVUK notify (https://www.notifications.service.gov.uk/) is a web service we are using to send emails to external partners. For our use case, we need to send these partners spreadsheet files (CSVs) in an agreed format for them to process and send back to us. GOVUK notify offers a mechanism for file uploads/downloads which we would like to use for this purpose.

Unfortunately, CSV files uploaded as part of a notify email message end up being saved as TXT files. This means that our partners download these files as TXT files, which is against our agreed process and is causing us problems.

The problematic bit of code lies in GOVUK's document-download-api web service, here: https://github.com/alphagov/document-download-api/blob/master/app/upload/views.py#L18

The get_mime_type() utility acts based on the contents of a file only (at this point in the flow, the service does not have access to file name/extension) - and so it identifies the file as a TXT file instead of as a CSV.

Proposed Solution

We need to provide a solution here which works both for our particular situation and for existing notify users (who may have worked around this problem in their own ways).

The proposal is for us to adjust notifications-python-client, notifications-api and document-download-api so that we can optionally pass the correct file mimetype through to the logic that saves the file.

This would require the following changes:

The rollout for these changes would need to be back to front - e.g. changes for document-download-api and then notifications-api and finally notifications-python-client. This would hopefully take the form of one pull request for each repository. These changes are purely additive/optional and not BC breaking so hopefully deployment should be simple.

Alternative Solutions

  • It could be possible to simply improve the mimetype auto detection in the document-download-api upload endpoint. However, this would be fiddly to do reliably and this could present a backwards compatibility breaking change for clients that assume that CSVs will be mis-identified as TXT files.
  • notifications-python-client could be adjusted to instead send the file extension (csv) or whole file name (myfile.csv) to make things simpler for clients. This would add some complexity somewhere later in the chain to resolve this to a mimetype.

Further Questions

  • Do all of the other notification client libraries need adjusting as part of this work? It would be preferable to get this working for the python client first, from my perspective.
  • The documentation probably needs some adjustment as part of this work. Should this also be done in PRs? Are the docs also under version control? Where?

PyPI package includes integration_test as top-level package

Hi,

I noticed that the PyPI package includes integration_test as a top-level package.

This means that after installing the package (e.g. pip install notifications-python-client) you can do things like:

import integration_test
from integration_test.integration_tests import get_template_by_id

This isn't causing me a problem, but it could lead to accidental imports or conflicts etc. (as integration_test is a pretty generic package name).

(In my case, I ended up there when searching for uses of NotificationsAPIClient in my IDE, and was a bit confused for a second...)

Initial Update

Hi 👊

This is my first visit to this fine repo, but it seems you have been working hard to keep all dependencies updated so far.

Once you have closed this issue, I'll create separate pull requests for every update as soon as I find one.

That's it for now!

Happy merging! 🤖

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.