Skip to content

Commit 13f9c48

Browse files
committed
Optimize Python SDK
1 parent 670f2a5 commit 13f9c48

File tree

6 files changed

+85
-20
lines changed

6 files changed

+85
-20
lines changed

asposeDiagramcloud/api_client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ApiClient(object):
4242
Ref: https://github.com/swagger-api/swagger-codegen
4343
Do not edit the class manually.
4444
45+
:param configuration: .Configuration object for this client
4546
:param host: The base path for the server to call.
4647
:param header_name: a header to pass when making calls to the API.
4748
:param header_value: a header value to pass when making calls to the API.
@@ -59,16 +60,20 @@ class ApiClient(object):
5960
'object': object,
6061
}
6162

62-
def __init__(self, host=None, header_name=None, header_value=None, cookie=None):
63+
def __init__(self, host=None,configuration=None, header_name=None, header_value=None, cookie=None):
6364
"""
6465
Constructor of the class.
6566
"""
67+
if configuration is None:
68+
configuration = Configuration()
69+
self.configuration = configuration
70+
6671
self.rest_client = RESTClientObject()
6772
self.default_headers = {}
6873
if header_name is not None:
6974
self.default_headers[header_name] = header_value
7075
if host is None:
71-
self.host = Configuration().host
76+
self.host = self.configuration.host
7277
else:
7378
self.host = host
7479
self.cookie = cookie
@@ -99,7 +104,7 @@ def __call_api(self, resource_path, method,
99104
_return_http_data_only=None, collection_formats=None, _preload_content=True,
100105
_request_timeout=None):
101106

102-
config = Configuration()
107+
config = self.configuration
103108

104109
# header parameters
105110
header_params = header_params or {}
@@ -505,7 +510,7 @@ def update_params_for_auth(self, headers, querys, auth_settings):
505510
:param querys: Query parameters tuple list to be updated.
506511
:param auth_settings: Authentication setting identifiers list.
507512
"""
508-
config = Configuration()
513+
config = self.configuration
509514

510515
if not auth_settings:
511516
return
@@ -532,7 +537,7 @@ def __deserialize_file(self, response):
532537
:param response: RESTResponse.
533538
:return: file path.
534539
"""
535-
config = Configuration()
540+
config = self.configuration
536541

537542
fd, path = tempfile.mkstemp(dir=config.temp_folder_path)
538543
os.close(fd)

asposeDiagramcloud/apis/diagram_file_api.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717
import os
1818
import re
19+
import six
1920

2021
# python 2 and python 3 compatibility library
2122
from six import iteritems
@@ -40,6 +41,57 @@ def __init__(self, api_client=None):
4041
config.api_client = ApiClient()
4142
self.api_client = config.api_client
4243

44+
self.__request_token()
45+
46+
def __request_token(self):
47+
config = self.api_client.configuration
48+
host = config.host
49+
self.api_client.host = 'https://api.aspose.cloud/'
50+
request_url = "oauth2/token"
51+
form_params = [('grant_type', 'client_credentials'), ('client_id', config.api_key['app_sid']),
52+
('client_secret', config.api_key['api_key'])]
53+
54+
header_params = {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'}
55+
56+
data = self.api_client.call_api(request_url, 'POST',
57+
{},
58+
[],
59+
header_params,
60+
post_params=form_params,
61+
response_type='object',
62+
files={}, _return_http_data_only=True)
63+
access_token = data['access_token'] if six.PY3 else data['access_token'].encode('utf8')
64+
refresh_token = data['refresh_token'] if six.PY3 else data['refresh_token'].encode('utf8')
65+
self.api_client.configuration.access_token = access_token
66+
self.api_client.host = host
67+
self.api_client.configuration.refresh_token = refresh_token
68+
69+
self.api_client.set_default_header("Authorization", "Bearer " + access_token)
70+
71+
72+
# Refresh token method is going to be removed soon. Obsolete, do not use
73+
def __refresh_token(self):
74+
config = self.api_client.configuration
75+
host = config.host
76+
config.host = ''
77+
request_url = "oauth2/token"
78+
form_params = [('grant_type', 'refresh_token'), ('refresh_token', config.refresh_token)]
79+
80+
header_params = {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'}
81+
82+
data = self.api_client.call_api(request_url, 'POST',
83+
{},
84+
[],
85+
header_params,
86+
post_params=form_params,
87+
response_type='object',
88+
files={}, _return_http_data_only=True)
89+
access_token = data['access_token'] if six.PY3 else data['access_token'].encode('utf8')
90+
refresh_token = data['refresh_token'] if six.PY3 else data['refresh_token'].encode('utf8')
91+
self.api_client.configuration.access_token = access_token
92+
self.api_client.configuration.host = host
93+
self.api_client.configuration.refresh_token = refresh_token
94+
4395
def diagram_file_get_diagram(self, name, **kwargs):
4496
"""
4597
Read document info or export.
@@ -511,3 +563,6 @@ def diagram_file_put_upload_with_http_info(self, name, **kwargs):
511563
_preload_content=params.get('_preload_content', True),
512564
_request_timeout=params.get('_request_timeout'),
513565
collection_formats=collection_formats)
566+
567+
568+

asposeDiagramcloud/configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self):
6666

6767
# Logging Settings
6868
self.logger = {}
69-
self.logger["package_logger"] = logging.getLogger("asposeDiagramcloud")
69+
self.logger["package_logger"] = logging.getLogger("asposediagramcloud")
7070
self.logger["urllib3_logger"] = logging.getLogger("urllib3")
7171
# Log format
7272
self.logger_format = '%(asctime)s %(levelname)s %(message)s'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from setuptools import setup, find_packages
1616

1717
NAME = "asposediagramcloud"
18-
VERSION = "18.10.1"
18+
VERSION = "18.10.2"
1919
# To install the library, run the following
2020
#
2121
# python setup.py install

test/test_base.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__)) + "/..")
77
sys.path.append(ABSPATH)
88

9-
import asposeDiagramcloud
10-
from asposeDiagramcloud.rest import ApiException
11-
from asposeDiagramcloud.apis.o_auth_api import OAuthApi
12-
from asposeDiagramcloud.api_client import ApiClient
9+
import asposediagramcloud
10+
from asposediagramcloud.rest import ApiException
11+
from asposediagramcloud.apis.o_auth_api import OAuthApi
12+
from asposediagramcloud.api_client import ApiClient
13+
from asposediagramcloud.configuration import Configuration
1314
import asposestoragecloud
1415

1516
grantType = "client_credentials"
@@ -18,7 +19,7 @@
1819

1920
def GetAccessToken():
2021
client = ApiClient('https://api.aspose.cloud/')
21-
api = asposeDiagramcloud.apis.o_auth_api.OAuthApi(client)
22+
api = asposediagramcloud.apis.o_auth_api.OAuthApi(client)
2223
data = api.o_auth_post(grantType, clientId, clientSecret)
2324
return data.access_token
2425

@@ -27,8 +28,12 @@ def GetAccessToken():
2728
def GetApiClient():
2829
global api_client
2930
if api_client == None:
30-
api_client = ApiClient('https://api.aspose.cloud/v1.1')
31-
api_client.set_default_header("Authorization", "Bearer " + GetAccessToken())
31+
configuration=Configuration()
32+
configuration.api_key['app_sid'] = '84220e69-32e2-41c4-ba2f-662a0a01433e' # Put your appSid here
33+
configuration.api_key['api_key'] = '883dc8d6b8ecd879dae35cb363e9eb56' # Put your appkey here
34+
api_client = ApiClient('https://api.aspose.cloud/v1.1',configuration)
35+
36+
#api_client.set_default_header("Authorization", "Bearer " + GetAccessToken())
3237
return api_client
3338

3439

test/test_diagram_file_api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919

2020
ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__)) + "/..")
2121
sys.path.append(ABSPATH)
22-
import asposeDiagramcloud
22+
import asposediagramcloud
2323
import test_base
2424

25-
from asposeDiagramcloud.rest import ApiException
26-
from asposeDiagramcloud.apis.diagram_file_api import DiagramFileApi
27-
from asposeDiagramcloud.models import FileFormatRequest
25+
from asposediagramcloud.rest import ApiException
26+
from asposediagramcloud.apis.diagram_file_api import DiagramFileApi
27+
from asposediagramcloud.models import FileFormatRequest
2828

2929
class TestDiagramFileApi(unittest.TestCase):
3030
""" DiagramFileApi unit test stubs """
3131

3232
def setUp(self):
3333
self.api_client = test_base.GetApiClient()
34-
self.api = asposeDiagramcloud.apis.diagram_file_api.DiagramFileApi(self.api_client)
34+
self.api = asposediagramcloud.apis.diagram_file_api.DiagramFileApi(self.api_client)
3535

3636
def tearDown(self):
3737
pass
@@ -45,7 +45,7 @@ def test_diagram_file_get_diagram(self):
4545
name ='file_get_1.vdx'
4646
folder = "Temp"
4747
result = self.api.diagram_file_get_diagram(name,format="pdf",folder=folder)
48-
print(result);
48+
#print(result)
4949
self.assertIsNotNone(result, 'Error has occurred while get file')
5050

5151
def test_diagram_file_post_save_as(self):

0 commit comments

Comments
 (0)