Skip to content

Commit

Permalink
Merge pull request #9 from rodekruis/feat.create-kobo-headers
Browse files Browse the repository at this point in the history
Feat.create kobo headers
  • Loading branch information
tijsziere authored Oct 27, 2023
2 parents a5fb0cc + 0055eb2 commit 0287c5e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,23 @@ See below for an example configuration, in which programId was not included as a
#### Nota Bene
The 121 API is currently throttled at 3000 submissions per minute. If you expect to go over this limit, please reach out the the 121 platform team.

### Create headers endpoint
If you need to map a lot of questions, creating the headers manually is cumbersome. The `/create-kobo-headers` endpoint automates this. It expects 4 query parameters:
- `system`: required, enum (options: 121, espocrm, generic)
- `kobouser`: your kobo username
- `kobopassword`: your kobo password
- `koboassetId `: the assed id of the survey (to be found in the url: https://kobonew.ifrc.org/#/forms/`ASSETID`/summary)

In the body you can pass all the headers you want to create as key value pairs, for example:
```json
{
"last_name": "lastName",
"first_name": "firstName",
"household_size": "hhSize"
}
```

This endpoint assumes the IFRC kobo server (`https://kobonew.ifrc.org`)

### Generic endpoint

Expand Down
39 changes: 38 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fastapi.responses import RedirectResponse, JSONResponse
from fastapi.security.api_key import APIKeyHeader, APIKey
from pydantic import BaseModel
from enum import Enum
from clients.espo_api_client import EspoAPI
import requests
import os
Expand Down Expand Up @@ -43,6 +44,10 @@
},
)

class system(str, Enum):
system_generic = "generic"
system_espo = "espocrm"
system_121 = "121"

def required_headers(
targeturl: str = Header(),
Expand Down Expand Up @@ -232,6 +237,39 @@ async def kobo_to_121(request: Request, dependencies=Depends(required_headers_12
target_response = response.content.decode("utf-8")
return JSONResponse(status_code=response.status_code, content=target_response)

@app.post("/create-kobo-headers")
async def create_kobo_headers(json_data: dict, system: system, kobouser: str, kobopassword: str, koboassetId: str):

if json_data is None:
raise HTTPException(status_code=400, detail="JSON data is required")

target_url = f"https://kobonew.ifrc.org/api/v2/assets/{koboassetId}/hooks/"
auth = (kobouser, kobopassword)

payload = {
"name": "koboconnect",
"endpoint": f"https://kobo-connect.azurewebsites.net/kobo-to-{system}",
"active": True,
"subset_fields": [],
"email_notification": True,
"export_type": "json",
"auth_level": "no_auth",
"settings": {
"custom_headers": {
}
},
"payload_template": ""
}

payload["settings"]["custom_headers"]=json_data

response = requests.post(target_url,auth=auth,json=payload)

if response.status_code == 200 or 201:
return JSONResponse(content={"message": "Sucess"})
else:
return JSONResponse(content={"message": "Failed to post data to the target endpoint"}, status_code=response.status_code)


@app.post("/kobo-to-generic")
async def kobo_to_generic(request: Request, dependencies=Depends(required_headers)):
Expand Down Expand Up @@ -267,6 +305,5 @@ async def kobo_to_generic(request: Request, dependencies=Depends(required_header
target_response = response.content.decode("utf-8")
return JSONResponse(status_code=200, content=target_response)


if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=int(port), reload=True)

0 comments on commit 0287c5e

Please sign in to comment.