-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD365PyHelper.py
More file actions
33 lines (27 loc) · 1.58 KB
/
D365PyHelper.py
File metadata and controls
33 lines (27 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import requests
import json
class CrmConnection:
def __init__(self, azure_directory_id, resource, client_id, client_secret):
self.azure_directory_id = azure_directory_id
self.resource = resource
self.client_id = client_id
self.client_secret = client_secret
self.auth_token = None
def get_authtoken(self):
URL = "https://login.microsoftonline.com/" + self.azure_directory_id + "/oauth2/token"
payload = {'client_id':self.client_id, 'resource':self.resource, 'client_secret':self.client_secret, 'grant_type':'client_credentials'}
response = requests.post(url=URL, data=payload)
self.auth_token = response.json()['access_token']
def get_records(self, entity_plural_name):
URL = self.resource.replace('.',".api.",1) + "/api/data/v9.2/" + entity_plural_name
authtokenString = "Bearer " + self.auth_token
HEADERS = {"Authorization":authtokenString}
response = requests.get(url = URL, params = None, headers=HEADERS)
return response
def create_record(self, entity_plural_name, column_values):
URL = self.resource.replace('.',".api.",1) + "/api/data/v9.2/" + entity_plural_name
authtokenString = "Bearer " + self.auth_token
HEADERS = {"Authorization":authtokenString, "Content-Type":"application/json; charset=utf-8", "OData-MaxVersion":"4.0", "OData-Version":"4.0", "Accept":"application/json"}
response = requests.post(url = URL, data = json.dumps(column_values), headers=HEADERS)
print(json.dumps(column_values))
return response