The Email microservice is a supports automated processes within the City & County of San Francisco to send templated emails via SendGrid.
Install Postgres (if needed)
brew install postgresqlCreate database
createdb email_microserviceInstall Redis (if needed)
brew install redisCreate a local config (by copying)
cp .env.example .envEdit the following line
export REDIS_URL=redis://localhost:6379Start the databases
brew services start postgresql@14
brew services start redisIf you want to verify the services running, you can use
brew services listInstall Pipenv (if needed)
pip install --user pipenvInstall packages
pipenv install --devNote: if you have (mini)Conda installed, you might have to adjust the Python executable. If so, you can run something like this (see for more info):
pipenv --python=$(conda run which python) --site-packagesMigrate DB
pipenv run alembic upgrade headACCESS_KEY=123456 pipenv run gunicorn --reload 'service.microservice:start_service()' --timeout 600Start celery worker
pipenv run celery workerAll API endpoints must be called via HTTP POST with a JSON request body and the following headers:
ACCESS_KEY: Your API access keyContent-Type:application/json
{
"subject": "Hi Diddly Ho",
"to": [{
"email": "[email protected]",
"name": "Homer Simpson"
}],
"from": {
"email": "[email protected]",
"name": "Ned Flanders"
"template": {
"url": "https://static.file.com/template.html",
"replacements" {
"var1": "hello!",
"var2": {
"first_name": "homer",
"last_name": "simpson"
}
}
}
}
}You can provide attachments either inline (with the base64-encoded content field) or via URL in the path field. You may also provide request headers for URL attachments that require additional authentication.
{
"subject": "Status Report",
"to": [{
"email": "[email protected]",
"name": "Charles Burns"
}],
"from": {
"email": "[email protected]",
"name": "Waylon Smithers"
},
"content": {
"type": "text/plain",
"value": "All systems are in operating condition."
},
"attachments": [
{
"content": "YmFzZTY0IHN0cmluZw==",
"filename": "report.txt",
"type": "text/plain"
},
{
"filename": "report.pdf",
"path": "https://www.springfieldnuclear.com/report.pdf",
"type": "application/pdf",
"headers": {
"api-key": "123ABC"
}
}
]
}{
"subject": "Hi Diddly Ho",
"to": [{
"email": "[email protected]",
"name": "Homer Simpson"
}],
"from": {
"email": "[email protected]",
"name": "Ned Flanders"
},
"content": [
{
"type": "text/plain",
"value": "Try search with https://google.com"
}
],
"filters" : {
"clicktrack" : {
"settings" : {
"enable" : 0,
"enable_text" : false
}
}
}
}Code coverage command with missing statement line numbers
pipenv run python -m pytest -s --cov=service --cov=tasks tests/ --cov-report term-missingCreate a migration
pipenv run alembic revision -m "Add a column"Edit the created revision file to add the steps to implement and rollback the changes you want to make.
Run DB migrations
pipenv run alembic upgrade headTemplates are rendered with Jinja.