-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and use
upload_documents
to minimize repeated code in repeated …
…tasks
- Loading branch information
1 parent
565ccab
commit 3f0000f
Showing
2 changed files
with
39 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import contextlib | ||
|
||
|
||
def upload_documents(*, client, document_paths): | ||
documents = ( | ||
client.post( | ||
"/api/documents", | ||
json=[{"name": document_path.name} for document_path in document_paths], | ||
) | ||
.raise_for_status() | ||
.json() | ||
) | ||
|
||
with contextlib.ExitStack() as stack: | ||
files = [ | ||
stack.enter_context(open(document_path, "rb")) | ||
for document_path in document_paths | ||
] | ||
client.put( | ||
"/api/documents", | ||
files=[ | ||
("documents", (document["id"], file)) | ||
for document, file in zip(documents, files) | ||
], | ||
) | ||
|
||
return documents |