Coverage for mindsdb / integrations / handlers / sharepoint_handler / utils.py: 0%
39 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1import json
2from typing import Dict, List, Text, Any
4import requests
5from requests import Response
8def create_an_entity(url: str, payload: Dict[Text, Any], bearer_token: str) -> None:
9 """
10 Makes a POST request to the given url
11 Creates an entity for the given url, bearer token and payload
13 url: URL to which the get request is made
14 bearer_token: authentication token for the request
15 payload: a dictionary which provides the metadata information regarding the entity being created
17 Returns
18 None
19 """
20 payload = json.dumps(payload, indent=2)
21 headers = {
22 "Authorization": f"Bearer {bearer_token}",
23 "Content-Type": "application/json",
24 }
25 getresponse(
26 request_type="POST", url=url, headers=headers, payload=payload, files=[]
27 )
30def get_an_entity(url: str, bearer_token: str) -> Any:
31 """
32 Makes a GET request to the given url
33 Gets the entity for the given url and bearer token
35 url: URL to which the get request is made
36 bearer_token: authentication token for the request
38 Returns
39 Dictionary or list of dictionaries containing information/metadata corresponding to the entities
40 """
41 payload = {}
42 headers = {
43 "Authorization": f"Bearer {bearer_token}",
44 "Content-Type": "application/json",
45 }
46 response = getresponse(
47 request_type="GET", url=url, headers=headers, payload=payload, files=[]
48 )
49 response = response.json()["value"]
50 return response
53def update_an_entity(
54 url: str, values_to_update: Dict[Text, Any], bearer_token: str
55) -> None:
56 """
57 Makes a PATCH request to given url with the given values_to_update and bearer_token
58 updates the entity with the provided values
60 url: url provided by the user
61 values_to_update: values that would be used to update the entity, would be passed in the payload
62 Mostly would be a dictionary mapping fields to values
63 bearer_token: authentication token passed in the header to make the request
65 Returns
66 None
67 """
68 payload = values_to_update
69 payload = json.dumps(payload, indent=2)
70 headers = {
71 "Authorization": f"Bearer {bearer_token}",
72 "Content-Type": "application/json",
73 }
74 getresponse(
75 request_type="PATCH", url=url, headers=headers, payload=payload, files=[]
76 )
79def delete_an_entity(url: str, bearer_token: str):
80 """
81 Makes a DELETE request to the given url
83 url: url string provided to which the request would be made
84 bearer_token: authorization token which will be used to execute the request
86 Returns
87 None
88 """
89 payload = {}
90 headers = {
91 "Authorization": f"Bearer {bearer_token}",
92 "Content-Type": "application/json",
93 }
94 getresponse(
95 request_type="DELETE", url=url, headers=headers, payload=payload, files=[]
96 )
99def bearer_token_request(tenant_id: str, client_id: str, client_secret: str) -> Any:
100 """
101 Sends a request to login.microsoftonline.com for the given tenant to generate a bearer token
102 which is then used in making graph api call
104 tenant_id: tenant ID is a globally unique identifier (GUID) for your organization
105 that is different from your organization or domain name
106 client_id: client ID is a globally unique identifier (GUID) for your app registered in Entra
107 client_secret: client secret is the password of the service principal or the app.
109 Returns
110 response: Dictionary containing bearer token and other information
111 """
112 url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/token"
114 payload = {
115 "client_id": client_id,
116 "client_secret": client_secret,
117 "redirect_uri": "http://localhost",
118 "grant_type": "client_credentials",
119 "resource": "https://graph.microsoft.com",
120 }
121 files = []
122 headers = {}
124 response = getresponse(
125 request_type="POST", url=url, headers=headers, payload=payload, files=files
126 )
127 response = response.json()
128 return response
131def getresponse(
132 url: str,
133 payload: Dict[Text, Any],
134 files: List[Any],
135 headers: Dict[Text, Any],
136 request_type: str,
137) -> Response:
138 """
139 Makes a standard HTTP request based on the params provided and returns the response.
140 May raise an error if the response code does not indicate success
142 url: url string provided to which the request would be made
143 payload: the payload which supply additional info regarding the request
144 files: the files that are needed to be passed in the request
145 headers: additional information regarding the request
146 may also indicate the type of content passed in the payload
147 request_type: the request performs different actions based on the request type
148 DELETE/POST/PATCH/GET
150 Returns
151 response: may return based on the response code
152 """
153 response = requests.request(
154 request_type, url, headers=headers, data=payload, files=files
155 )
156 status_code = response.status_code
158 if 400 <= status_code <= 499:
159 raise Exception("Client error: " + response.text)
161 if 500 <= status_code <= 599:
162 raise Exception("Server error: " + response.text)
163 return response