Source code for langchain_community.utilities.requests
"""Lightweight wrapper around requests library, with async support."""fromcontextlibimportasynccontextmanagerfromtypingimportAny,AsyncGenerator,Dict,Literal,Optional,Unionimportaiohttpimportrequestsfromlangchain_core.pydantic_v1importBaseModelfromrequestsimportResponse
[docs]classRequests(BaseModel):"""Wrapper around requests to handle auth and async. The main purpose of this wrapper is to handle authentication (by saving headers) and enable easy async methods on the same base object. """headers:Optional[Dict[str,str]]=Noneaiosession:Optional[aiohttp.ClientSession]=Noneauth:Optional[Any]=Noneverify:Optional[bool]=TrueclassConfig:arbitrary_types_allowed=Trueextra="forbid"
[docs]defget(self,url:str,**kwargs:Any)->requests.Response:"""GET the URL and return the text."""returnrequests.get(url,headers=self.headers,auth=self.auth,verify=self.verify,**kwargs)
[docs]defpost(self,url:str,data:Dict[str,Any],**kwargs:Any)->requests.Response:"""POST to the URL and return the text."""returnrequests.post(url,json=data,headers=self.headers,auth=self.auth,verify=self.verify,**kwargs,)
[docs]defpatch(self,url:str,data:Dict[str,Any],**kwargs:Any)->requests.Response:"""PATCH the URL and return the text."""returnrequests.patch(url,json=data,headers=self.headers,auth=self.auth,verify=self.verify,**kwargs,)
[docs]defput(self,url:str,data:Dict[str,Any],**kwargs:Any)->requests.Response:"""PUT the URL and return the text."""returnrequests.put(url,json=data,headers=self.headers,auth=self.auth,verify=self.verify,**kwargs,)
[docs]defdelete(self,url:str,**kwargs:Any)->requests.Response:"""DELETE the URL and return the text."""returnrequests.delete(url,headers=self.headers,auth=self.auth,verify=self.verify,**kwargs)
@asynccontextmanagerasyncdef_arequest(self,method:str,url:str,**kwargs:Any)->AsyncGenerator[aiohttp.ClientResponse,None]:"""Make an async request."""ifnotself.aiosession:asyncwithaiohttp.ClientSession()assession:asyncwithsession.request(method,url,headers=self.headers,auth=self.auth,**kwargs,)asresponse:yieldresponseelse:asyncwithself.aiosession.request(method,url,headers=self.headers,auth=self.auth,**kwargs,)asresponse:yieldresponse
[docs]@asynccontextmanagerasyncdefaget(self,url:str,**kwargs:Any)->AsyncGenerator[aiohttp.ClientResponse,None]:"""GET the URL and return the text asynchronously."""asyncwithself._arequest("GET",url,**kwargs)asresponse:yieldresponse
[docs]@asynccontextmanagerasyncdefapost(self,url:str,data:Dict[str,Any],**kwargs:Any)->AsyncGenerator[aiohttp.ClientResponse,None]:"""POST to the URL and return the text asynchronously."""asyncwithself._arequest("POST",url,json=data,**kwargs)asresponse:yieldresponse
[docs]@asynccontextmanagerasyncdefapatch(self,url:str,data:Dict[str,Any],**kwargs:Any)->AsyncGenerator[aiohttp.ClientResponse,None]:"""PATCH the URL and return the text asynchronously."""asyncwithself._arequest("PATCH",url,json=data,**kwargs)asresponse:yieldresponse
[docs]@asynccontextmanagerasyncdefaput(self,url:str,data:Dict[str,Any],**kwargs:Any)->AsyncGenerator[aiohttp.ClientResponse,None]:"""PUT the URL and return the text asynchronously."""asyncwithself._arequest("PUT",url,json=data,**kwargs)asresponse:yieldresponse
[docs]@asynccontextmanagerasyncdefadelete(self,url:str,**kwargs:Any)->AsyncGenerator[aiohttp.ClientResponse,None]:"""DELETE the URL and return the text asynchronously."""asyncwithself._arequest("DELETE",url,**kwargs)asresponse:yieldresponse
[docs]defget(self,url:str,**kwargs:Any)->Union[str,Dict[str,Any]]:"""GET the URL and return the text."""returnself._get_resp_content(self.requests.get(url,**kwargs))
[docs]defpost(self,url:str,data:Dict[str,Any],**kwargs:Any)->Union[str,Dict[str,Any]]:"""POST to the URL and return the text."""returnself._get_resp_content(self.requests.post(url,data,**kwargs))
[docs]defpatch(self,url:str,data:Dict[str,Any],**kwargs:Any)->Union[str,Dict[str,Any]]:"""PATCH the URL and return the text."""returnself._get_resp_content(self.requests.patch(url,data,**kwargs))
[docs]defput(self,url:str,data:Dict[str,Any],**kwargs:Any)->Union[str,Dict[str,Any]]:"""PUT the URL and return the text."""returnself._get_resp_content(self.requests.put(url,data,**kwargs))
[docs]defdelete(self,url:str,**kwargs:Any)->Union[str,Dict[str,Any]]:"""DELETE the URL and return the text."""returnself._get_resp_content(self.requests.delete(url,**kwargs))
[docs]asyncdefaget(self,url:str,**kwargs:Any)->Union[str,Dict[str,Any]]:"""GET the URL and return the text asynchronously."""asyncwithself.requests.aget(url,**kwargs)asresponse:returnawaitself._aget_resp_content(response)
[docs]asyncdefapost(self,url:str,data:Dict[str,Any],**kwargs:Any)->Union[str,Dict[str,Any]]:"""POST to the URL and return the text asynchronously."""asyncwithself.requests.apost(url,data,**kwargs)asresponse:returnawaitself._aget_resp_content(response)
[docs]asyncdefapatch(self,url:str,data:Dict[str,Any],**kwargs:Any)->Union[str,Dict[str,Any]]:"""PATCH the URL and return the text asynchronously."""asyncwithself.requests.apatch(url,data,**kwargs)asresponse:returnawaitself._aget_resp_content(response)
[docs]asyncdefaput(self,url:str,data:Dict[str,Any],**kwargs:Any)->Union[str,Dict[str,Any]]:"""PUT the URL and return the text asynchronously."""asyncwithself.requests.aput(url,data,**kwargs)asresponse:returnawaitself._aget_resp_content(response)
[docs]asyncdefadelete(self,url:str,**kwargs:Any)->Union[str,Dict[str,Any]]:"""DELETE the URL and return the text asynchronously."""asyncwithself.requests.adelete(url,**kwargs)asresponse:returnawaitself._aget_resp_content(response)
[docs]classJsonRequestsWrapper(GenericRequestsWrapper):"""Lightweight wrapper around requests library, with async support. The main purpose of this wrapper is to always return a json output."""response_content_type:Literal["text","json"]="json"
[docs]classTextRequestsWrapper(GenericRequestsWrapper):"""Lightweight wrapper around requests library, with async support. The main purpose of this wrapper is to always return a text output."""response_content_type:Literal["text","json"]="text"
# For backwards compatibilityRequestsWrapper=TextRequestsWrapper