Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend submission.create to support attachments #102

Open
yanokwa opened this issue Nov 13, 2024 · 1 comment · May be fixed by #105
Open

Extend submission.create to support attachments #102

yanokwa opened this issue Nov 13, 2024 · 1 comment · May be fixed by #105

Comments

@yanokwa
Copy link
Member

yanokwa commented Nov 13, 2024

I sometimes have to pull data from a device and send it to a server. I'd like to script that with pyODK but there's no easy way to create a submission with attachments. It'd be nice to have that.

Here's what I'm doing...

#!/usr/bin/env python3

from pathlib import Path
import requests

SUBMISSION_URL = "https://foo.getodk.cloud/v1/key/1234567890/projects/1/submission"
INSTANCES_PATH = Path("/Users/me/Desktop/instances")

def submit_instances(folder_path):
    files = {}

    for file in folder_path.iterdir():
        if file.is_file():
            file_content = file.read_bytes()
            
            if file.suffix == ".xml":
                print(file.name)
                files["xml_submission_file"] = ("instance.xml", file_content)
            else:
                files[file.name] = (file.name, file_content)

    response = requests.post(
        SUBMISSION_URL,
        files=files,
        headers={"X-OpenRosa-Version": "1.0"}
    )

    if response.status_code != 201:
        print(response.text)

for folder in INSTANCES_PATH.iterdir():
    if folder.is_dir():
        submit_instances(folder)
@spwoodcock
Copy link
Member

#104 makes a start towards this.

Hopefully it should be a simple update to the SubmissionService .post to include attachment data.
Then we could call SubmissionAttachmentService .upload for each attachment after the main submission POST.

@spwoodcock spwoodcock linked a pull request Nov 21, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants