Skip to content

Commit a5dbcdb

Browse files
author
root
committed
improvements
1 parent 0598872 commit a5dbcdb

File tree

3 files changed

+69
-25
lines changed

3 files changed

+69
-25
lines changed

dynamics365crm/client.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Client:
77
header = {"Accept": "application/json, */*", "content-type": "application/json; charset=utf-8",
88
'OData-MaxVersion': '4.0', 'OData-Version': '4.0'}
99

10-
def __init__(self, client_id, client_secret, token=None):
10+
def __init__(self, client_id=None, client_secret=None, token=None):
1111
self.client_id = client_id
1212
self.client_secret = client_secret
1313
self.token = token
@@ -116,32 +116,42 @@ def parse_response(self, response):
116116
return response.json()
117117

118118
def url_petition(self, redirect_uri, resource):
119-
url = "https://login.microsoftonline.com/{0}/oauth2/authorize?client_id={1}&response_type={2}&redirect_uri={3}&response_mode={4}&resource={5}".format(
120-
"common", self.client_id, "code", redirect_uri, "query", resource)
119+
if self.client_id is not None and redirect_uri is not None and resource is not None:
120+
url = "https://login.microsoftonline.com/{0}/oauth2/authorize?client_id={1}&response_type={2}&redirect_uri={3}&response_mode={4}&resource={5}".format(
121+
"common", self.client_id, "code", redirect_uri, "query", resource)
122+
123+
# this part needs an administrator autorization
124+
# url = "https://login.microsoftonline.com/common/adminconsent?client_id={0}&redirect_uri={1}".format(
125+
# client_id, redirect_uri)
126+
return url
127+
else:
128+
raise Exception("The attributes necessary to get the url were not obtained.")
121129

122-
# this part needs an administrator autorization
123-
# url = "https://login.microsoftonline.com/common/adminconsent?client_id={0}&redirect_uri={1}".format(
124-
# client_id, redirect_uri)
125-
return url
126130

127131
def exchange_code(self, redirect_uri, code):
128-
url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
129-
args = {
130-
'client_id': self.client_id,
131-
'redirect_uri': redirect_uri,
132-
'client_secret': self.client_secret,
133-
'code': code,
134-
'grant_type': 'authorization_code',
135-
}
136-
response = requests.post(url, data=args)
137-
return self.parse_response(response)
132+
if self.client_id is not None and self.client_secret is not None and redirect_uri is not None and code is not None:
133+
url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
134+
args = {
135+
'client_id': self.client_id,
136+
'redirect_uri': redirect_uri,
137+
'client_secret': self.client_secret,
138+
'code': code,
139+
'grant_type': 'authorization_code',
140+
}
141+
response = requests.post(url, data=args)
142+
return self.parse_response(response)
143+
else:
144+
raise Exception("The attributes necessary to exchange the code were not obtained.")
138145

139146
def refresh_token(self, refresh_token, redirect_uri, resource):
140-
url = "https://login.microsoftonline.com/common/oauth2/token"
141-
args = {"client_id": self.client_id, "grant_type": "refresh_token", "refresh_token": refresh_token,
142-
"redirect_uri": redirect_uri, "client_secret": self.client_secret, "resource": resource}
143-
response = requests.post(url, data=args)
144-
return self.parse_response(response)
147+
if self.client_id is not None and self.client_secret is not None and refresh_token is not None and redirect_uri is not None and resource is not None:
148+
url = "https://login.microsoftonline.com/common/oauth2/token"
149+
args = {"client_id": self.client_id, "grant_type": "refresh_token", "refresh_token": refresh_token,
150+
"redirect_uri": redirect_uri, "client_secret": self.client_secret, "resource": resource}
151+
response = requests.post(url, data=args)
152+
return self.parse_response(response)
153+
else:
154+
raise Exception("The attributes necessary to refresh the token were not obtained.")
145155

146156
def set_token(self, token):
147157
"""

dynamics365crm/test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
"""
55
MAIN INSTANCE (petition)
6-
obligatory send the access_token
76
"""
8-
petition = Client("")
97
tenant_id = ""
108
client_id = ""
119
client_secret = ""
1210
dynamics_resource = ""
1311
CRM_resource = ""
1412
refresh_token = ""
13+
token = ""
14+
petition = Client(client_id=client_id, client_secret=client_secret, token=token)
1515

1616
"""
1717
API ENDPOINTS EXAMPLES
@@ -22,7 +22,7 @@
2222
REFRESH TOKEN
2323
to refresh the token you have to send the client_id, the client_secret, the refresh_token, the redirect_uri, and the resource
2424
Example:
25-
refresh = petition.refresh_token(client_id, client_id, refresh_token, "REDIRECT URI", CRM_resource)
25+
refresh = petition.refresh_token(refresh_token, redirect_uri, resource)
2626
pprint.pprint(refresh)
2727
"""
2828

test/test_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
from unittest import TestCase
3+
from urllib.parse import urlparse, parse_qs
4+
from dynamics365crm.client import Client
5+
6+
7+
class Dynamics365CRMTestCases(TestCase):
8+
9+
def setUp(self):
10+
self.client_id = os.environ.get('CLIENT_ID')
11+
self.client_secret = os.environ.get('CLIENT_SECRET')
12+
self.token = os.environ.get('ACCESS_TOKEN')
13+
self.redirect_url = os.environ.get('REDIRECT_URL')
14+
self.resource = os.environ.get('RESOURCE')
15+
self.client = Client(client_id=self.client_id, client_secret=self.client_secret, token=self.token)
16+
17+
def test_oauth_access(self):
18+
url = self.client.url_petition(self.redirect_url, self.resource)
19+
self.assertIsInstance(url, str)
20+
o = urlparse(url)
21+
query = parse_qs(o.query)
22+
self.assertIn('client_id', query)
23+
self.assertEqual(query['client_id'][0], self.client_id)
24+
self.assertIn('redirect_uri', query)
25+
self.assertEqual(query['redirect_uri'][0], self.redirect_url)
26+
27+
def test_get_data(self):
28+
response = self.client.get_data(type="contacts", select="fullname, emailaddress1, createdon", orderby="createdon desc", top="1")
29+
self.assertIsInstance(response, dict)
30+
31+
def test_create_data(self):
32+
response = self.client.create_data(type='contacts', firstname="NAME", lastname="LASTNAME", middlename="MIDDLENAME", emailaddress1="[email protected]")
33+
print(response)
34+
self.assertIsInstance(response, bool)

0 commit comments

Comments
 (0)