Skip to content

Add support for files endpoint #38

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

Merged
merged 6 commits into from
May 17, 2021
Merged

Conversation

xiaohua-scale
Copy link
Contributor

@xiaohua-scale xiaohua-scale commented May 14, 2021

  1. Add support for files endpoint: https://docs.scale.com/reference#file-overview
  2. Add an instruction_batch param for batch creation: https://docs.scale.com/reference#batch-overview
  3. Add test for import_file & upload_file

unittest && e2e tested with the following code

import scaleapi
from scaleapi.tasks import TaskType

client = scaleapi.ScaleClient("<api_key>")


batch = "test-client-project-1-instruction-batch-1"
project = "test-client-project-1"


# Upload File
with open("test_image.png", "rb") as f:
    data = ("x", f)

    # Upload File
    payload = dict(project_name=project)
    upload_result = client.upload_file(
        file=data,
        project_name=project,
    )
    print(upload_result.__repr__())

# Import File
import_result = client.import_file(
    "https://static.scale.com/uploads/selfserve-sample-image.png",
    project_name=project,
)

print(import_result.__repr__())


client.create_batch(
    project=project,
    batch_name=batch,
    instruction_batch=True,
)

payload = dict(
    project=project,
    attachments=[
        {"type": "image", "content": import_result.attachment_url},
        {"type": "image", "content": upload_result.attachment_url},
    ],
    batch=batch,
)

client.create_task(task_type=TaskType.TextCollection, **payload)

client.finalize_batch(batch_name=batch)

@xiaohua-scale xiaohua-scale requested review from fatihkurtoglu, a team and zhichunl May 14, 2021 05:28
@@ -30,10 +30,20 @@ def __init__(self, api_key, user_agent_extension=None):
"Content-Type": "application/json",
"User-Agent": self._generate_useragent(user_agent_extension),
}
self._headers_multipart_form_data = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you specify a files parameter (a dictionary), then requests will send a multipart/form-data POST instead of a application/x-www-form-urlencoded POST.

ref: https://stackoverflow.com/questions/12385179/how-to-send-a-multipart-form-data-with-requests-in-python

@xiaohua-scale xiaohua-scale requested review from shaun-scale and removed request for a team May 14, 2021 20:31
Copy link
Contributor

@fatihkurtoglu fatihkurtoglu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@fatihkurtoglu fatihkurtoglu merged commit 727bb95 into master May 17, 2021
@fatihkurtoglu fatihkurtoglu deleted the files-endpoint-support branch May 17, 2021 23:43
@fatihkurtoglu fatihkurtoglu restored the files-endpoint-support branch May 17, 2021 23:46
@fatihkurtoglu fatihkurtoglu deleted the files-endpoint-support branch May 17, 2021 23:47
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 this pull request may close these issues.

3 participants