-
Notifications
You must be signed in to change notification settings - Fork 1
Enable creating a static resource from a URL #26
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,10 +151,13 @@ def create_remote( | |
| @simple_connection_retry | ||
| def create_static( | ||
| self, | ||
| file_to_upload: str, # the path of the file | ||
| file_to_upload: str, # the path or URL of the file | ||
| payload: dict, | ||
| dataset_id: str | None = None, | ||
| is_communautary: bool = False, | ||
| from_url: bool = False, | ||
| file_name: str | None = None, | ||
| mime: str | None = None, | ||
| ) -> Resource: | ||
| if dataset_id and self.__class__.__name__ == "Dataset": | ||
| raise ValueError( | ||
|
|
@@ -170,9 +173,26 @@ def create_static( | |
| if is_communautary: | ||
| url += "community/" | ||
| logging.info(f"🆕 Creating '{payload['title']}' for {file_to_upload}") | ||
| r = self._client.session.post(url, files={"file": open(file_to_upload, "rb")}) | ||
| r.raise_for_status() | ||
| metadata = r.json() | ||
| if from_url: | ||
| if file_name is None or mime is None: | ||
| raise ValueError( | ||
| "When uploading from a URL, file_name and mime arguments are required." | ||
| ) | ||
| with self._client.session.stream("GET", file_to_upload) as streamed: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we open/close the session stream without a |
||
| streamed.raise_for_status() | ||
| streamed.read() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is apparently the syntax for |
||
| with self._client.session.stream( | ||
| "POST", | ||
| url, | ||
| files={"file": (file_name, streamed.content, mime)}, | ||
| ) as r: | ||
| r.raise_for_status() | ||
| r.read() | ||
| metadata = r.json() | ||
| else: | ||
| r = self._client.session.post(url, files={"file": open(file_to_upload, "rb")}) | ||
| r.raise_for_status() | ||
| metadata = r.json() | ||
| resource_id = metadata["id"] | ||
| r = Resource( | ||
| id=resource_id, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.