📝 docs(components/utilities.mdx): add documentation for Update Request feature

🚀 feat(UpdateRequest.py): add UpdateRequest component to make PATCH or PUT requests to a given URL
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-22 17:35:20 -03:00
commit 42a268a678
2 changed files with 37 additions and 5 deletions

View file

@ -37,6 +37,21 @@ Make a POST request to the given URL.
- **Document:** The JSON response from the request as a Document.
### Update Request
Make a PATCH or PUT request to the given URL.
**Params**
- **URL:** The URL to make the request to.
- **Headers:** A dictionary of headers to send with the request.
- **Document:** The Document containing a JSON object to send with the request.
- **Method:** The HTTP method to use for the request. Can be either `PATCH` or `PUT`.
**Output**
- **Document:** The JSON response from the request as a Document.
### JSON Document Builder
Build a Document containing a JSON object using a key and another Document page content.

View file

@ -5,8 +5,8 @@ from langchain.schema import Document
from langflow.database.models.base import orjson_dumps
class PatchRequest(CustomComponent):
display_name: str = "Patch Request"
class UpdateRequest(CustomComponent):
display_name: str = "Update Request"
description: str = "Make a PATCH request to the given URL."
output_types: list[str] = ["Document"]
beta = True
@ -19,17 +19,32 @@ class PatchRequest(CustomComponent):
},
"code": {"show": False},
"document": {"display_name": "Document"},
"method": {
"display_name": "Method",
"field_type": "str",
"info": "The HTTP method to use.",
"options": ["PATCH", "PUT"],
"value": "PATCH",
},
}
def patch_document(
def update_document(
self,
session: requests.Session,
document: Document,
url: str,
headers: Optional[dict] = None,
method: str = "PATCH",
) -> Document:
try:
response = session.patch(url, headers=headers, data=document.page_content)
if method == "PATCH":
response = session.patch(
url, headers=headers, data=document.page_content
)
elif method == "PUT":
response = session.put(url, headers=headers, data=document.page_content)
else:
raise ValueError(f"Unsupported method: {method}")
try:
response_json = response.json()
result = orjson_dumps(response_json, indent_2=False)
@ -52,6 +67,7 @@ class PatchRequest(CustomComponent):
def build(
self,
method: str,
document: Document,
url: str,
headers: Optional[dict] = None,
@ -70,7 +86,8 @@ class PatchRequest(CustomComponent):
with requests.Session() as session:
documents = [
self.patch_document(session, doc, url, headers) for doc in documents
self.update_document(session, doc, url, headers, method)
for doc in documents
]
self.repr_value = documents
return documents