📝 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:
parent
b1779e849d
commit
42a268a678
2 changed files with 37 additions and 5 deletions
|
|
@ -37,6 +37,21 @@ Make a POST request to the given URL.
|
||||||
|
|
||||||
- **Document:** The JSON response from the request as a Document.
|
- **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
|
### JSON Document Builder
|
||||||
|
|
||||||
Build a Document containing a JSON object using a key and another Document page content.
|
Build a Document containing a JSON object using a key and another Document page content.
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@ from langchain.schema import Document
|
||||||
from langflow.database.models.base import orjson_dumps
|
from langflow.database.models.base import orjson_dumps
|
||||||
|
|
||||||
|
|
||||||
class PatchRequest(CustomComponent):
|
class UpdateRequest(CustomComponent):
|
||||||
display_name: str = "Patch Request"
|
display_name: str = "Update Request"
|
||||||
description: str = "Make a PATCH request to the given URL."
|
description: str = "Make a PATCH request to the given URL."
|
||||||
output_types: list[str] = ["Document"]
|
output_types: list[str] = ["Document"]
|
||||||
beta = True
|
beta = True
|
||||||
|
|
@ -19,17 +19,32 @@ class PatchRequest(CustomComponent):
|
||||||
},
|
},
|
||||||
"code": {"show": False},
|
"code": {"show": False},
|
||||||
"document": {"display_name": "Document"},
|
"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,
|
self,
|
||||||
session: requests.Session,
|
session: requests.Session,
|
||||||
document: Document,
|
document: Document,
|
||||||
url: str,
|
url: str,
|
||||||
headers: Optional[dict] = None,
|
headers: Optional[dict] = None,
|
||||||
|
method: str = "PATCH",
|
||||||
) -> Document:
|
) -> Document:
|
||||||
try:
|
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:
|
try:
|
||||||
response_json = response.json()
|
response_json = response.json()
|
||||||
result = orjson_dumps(response_json, indent_2=False)
|
result = orjson_dumps(response_json, indent_2=False)
|
||||||
|
|
@ -52,6 +67,7 @@ class PatchRequest(CustomComponent):
|
||||||
|
|
||||||
def build(
|
def build(
|
||||||
self,
|
self,
|
||||||
|
method: str,
|
||||||
document: Document,
|
document: Document,
|
||||||
url: str,
|
url: str,
|
||||||
headers: Optional[dict] = None,
|
headers: Optional[dict] = None,
|
||||||
|
|
@ -70,7 +86,8 @@ class PatchRequest(CustomComponent):
|
||||||
|
|
||||||
with requests.Session() as session:
|
with requests.Session() as session:
|
||||||
documents = [
|
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
|
self.repr_value = documents
|
||||||
return documents
|
return documents
|
||||||
Loading…
Add table
Add a link
Reference in a new issue