Skip to content

Commit 74ea73b

Browse files
author
Govinda Raju
committed
Adding User-Agent and updating model definitions
Updated ask-sdk-model-runtime test cases to use UnitTest framework
1 parent eb55886 commit 74ea73b

20 files changed

+563
-252
lines changed

ask-sdk-model-runtime/CHANGELOG.rst

+6
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ CHANGELOG
66
---
77

88
* Initial release of alexa sdk model runtime.
9+
10+
1.0.1
11+
-----
12+
13+
* Adding User-Agent functionality support to set custom user agent values.
14+
* Updated all Unit tests to Python UnitTest framework.

ask-sdk-model-runtime/ask_sdk_model_runtime/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
from .serializer import DefaultSerializer, Serializer
2929
from .exceptions import (
3030
ServiceException, SerializationException, ApiClientException)
31-
from .service_client_response import ServiceClientResponse
31+
from .service_client_response import ServiceClientResponse
32+
from .utils import user_agent_info

ask-sdk-model-runtime/ask_sdk_model_runtime/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
'Authentication Configuration etc that is used by the SDK '
2222
'models.')
2323
__url__ = 'https://github.com/alexa/alexa-apis-for-python'
24-
__version__ = '1.0.0'
24+
__version__ = '1.0.1'
2525
__author__ = 'Alexa Skills Kit'
2626
__author_email__ = '[email protected]'
2727
__license__ = 'Apache 2.0'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights
4+
# Reserved.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License").
7+
# You may not use this file except in compliance with the License.
8+
# A copy of the License is located at
9+
#
10+
# http://aws.amazon.com/apache2.0/
11+
#
12+
# or in the "license" file accompanying this file. This file is
13+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
14+
# OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the
16+
# License.
17+
#
18+
import sys
19+
20+
def user_agent_info(sdk_version, custom_user_agent):
21+
# type: (str, str) -> str
22+
"""Return the user agent info along with the SDK and Python
23+
Version information.
24+
25+
:param sdk_version: Version of the SDK being used.
26+
:type sdk_version: str
27+
:param custom_user_agent: Custom User Agent string provided by
28+
the developer.
29+
:type custom_user_agent: str
30+
:return: User Agent Info string
31+
:rtype: str
32+
"""
33+
python_version = ".".join(str(x) for x in sys.version_info[0:3])
34+
user_agent = "ask-python-model/{} Python/{}".format(
35+
sdk_version, python_version)
36+
if custom_user_agent is None:
37+
return user_agent
38+
else:
39+
return user_agent + " " + custom_user_agent

ask-sdk-model-runtime/tests/unit/test_api_client.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def test_convert_null_header_tuples_to_dict(self):
4141
test_headers_list = None
4242
expected_headers_dict = {}
4343

44-
assert self.test_api_client._convert_list_tuples_to_dict(
45-
test_headers_list) == expected_headers_dict, (
44+
self.assertEqual(self.test_api_client._convert_list_tuples_to_dict(
45+
test_headers_list), expected_headers_dict, (
4646
"DefaultApiClient failed to convert null headers list to empty "
47-
"dict object")
47+
"dict object"))
4848

4949
def test_convert_header_tuples_to_dict(self):
5050
test_headers_list = [
@@ -53,20 +53,20 @@ def test_convert_header_tuples_to_dict(self):
5353
expected_headers_dict = {
5454
"header_1": "test_1, test_3", "header_2": "test_2"}
5555

56-
assert self.test_api_client._convert_list_tuples_to_dict(
57-
test_headers_list) == expected_headers_dict, (
56+
self.assertEqual(self.test_api_client._convert_list_tuples_to_dict(
57+
test_headers_list), expected_headers_dict, (
5858
"DefaultApiClient failed to convert header list of tuples to "
5959
"dictionary format needed for http "
60-
"request call")
60+
"request call"))
6161

6262
def test_convert_null_header_dict_to_tuples(self):
6363
test_headers_dict = None
6464
expected_headers_list = []
6565

66-
assert self.test_api_client._convert_dict_to_list_tuples(
67-
test_headers_dict) == expected_headers_list, (
66+
self.assertEqual(self.test_api_client._convert_dict_to_list_tuples(
67+
test_headers_dict), expected_headers_list, (
6868
"DefaultApiClient failed to convert null headers dict to empty "
69-
"list object")
69+
"list object"))
7070

7171
def test_convert_header_dict_to_tuples(self):
7272
test_headers_dict = {
@@ -76,11 +76,11 @@ def test_convert_header_dict_to_tuples(self):
7676
("header_1", "test_1"), ("header_1", "test_3"),
7777
("header_2", "test_2"), ("header_3", "test_4")]
7878

79-
assert set(self.test_api_client._convert_dict_to_list_tuples(
80-
test_headers_dict)) == set(
79+
self.assertEqual(set(self.test_api_client._convert_dict_to_list_tuples(
80+
test_headers_dict)), set(
8181
expected_headers_list), (
8282
"DefaultApiClient failed to convert headers dict to list of "
83-
"tuples format for ApiClientResponse")
83+
"tuples format for ApiClientResponse"))
8484

8585
def test_resolve_valid_http_method(self):
8686
with mock.patch("requests.get",
@@ -104,15 +104,15 @@ def test_resolve_invalid_http_method_throw_exception(self):
104104
with self.assertRaises(ApiClientException) as exc:
105105
self.test_api_client.invoke(test_invalid_method_request)
106106

107-
assert "Invalid request method: GET_TEST" in str(exc.exception)
107+
self.assertIn("Invalid request method: GET_TEST", str(exc.exception))
108108

109109
def test_invoke_http_method_throw_exception(self):
110110
with mock.patch("requests.get",
111111
side_effect=Exception("test exception")):
112112
with self.assertRaises(ApiClientException) as exc:
113113
self.test_api_client.invoke(self.valid_request)
114114

115-
assert "Error executing the request: test exception" in str(exc.exception)
115+
self.assertIn("Error executing the request: test exception", str(exc.exception))
116116

117117
def test_api_client_invoke_with_method_headers_processed(self):
118118
self.valid_request.headers = [
@@ -131,21 +131,21 @@ def test_api_client_invoke_with_method_headers_processed(self):
131131
side_effect=lambda *args, **kwargs: test_response):
132132
actual_response = self.test_api_client.invoke(self.valid_request)
133133

134-
assert set(actual_response.headers) == set([
134+
self.assertEqual(set(actual_response.headers), set([
135135
("response_header_1", "test_1"),
136136
("response_header_1", "test_3"),
137137
("response_header_2", "test_2"),
138138
("response_header_3", "test_4")]), (
139139
"Response headers from client doesn't match with the "
140-
"expected headers")
140+
"expected headers"))
141141

142-
assert actual_response.status_code == 400, (
142+
self.assertEqual(actual_response.status_code, 400, (
143143
"Status code from client response doesn't match with the "
144-
"expected response status code")
144+
"expected response status code"))
145145

146-
assert actual_response.body == "test response body", (
146+
self.assertEqual(actual_response.body, "test response body", (
147147
"Body from client response doesn't match with the expected "
148-
"response body")
148+
"response body"))
149149

150150
def test_api_client_invoke_with_http_url_throw_error(self):
151151
test_invalid_url_scheme_request = ApiClientRequest(
@@ -157,7 +157,7 @@ def test_api_client_invoke_with_http_url_throw_error(self):
157157
with self.assertRaises(ApiClientException) as exc:
158158
self.test_api_client.invoke(test_invalid_url_scheme_request)
159159

160-
assert "Requests against non-HTTPS endpoints are not allowed." in str(exc.exception)
160+
self.assertIn("Requests against non-HTTPS endpoints are not allowed.", str(exc.exception))
161161

162162
def test_api_client_invoke_with_http_case_sensitive_url_throw_error(self):
163163
test_invalid_url_scheme_request = ApiClientRequest(
@@ -169,7 +169,7 @@ def test_api_client_invoke_with_http_case_sensitive_url_throw_error(self):
169169
with self.assertRaises(ApiClientException) as exc:
170170
self.test_api_client.invoke(test_invalid_url_scheme_request)
171171

172-
assert "Requests against non-HTTPS endpoints are not allowed." in str(exc.exception)
172+
self.assertIn("Requests against non-HTTPS endpoints are not allowed.", str(exc.exception))
173173

174174
def test_api_client_invoke_with_no_url_schema_throw_error(self):
175175
test_invalid_url_scheme_request = ApiClientRequest(
@@ -181,7 +181,7 @@ def test_api_client_invoke_with_no_url_schema_throw_error(self):
181181
with self.assertRaises(ApiClientException) as exc:
182182
self.test_api_client.invoke(test_invalid_url_scheme_request)
183183

184-
assert "Requests against non-HTTPS endpoints are not allowed." in str(exc.exception)
184+
self.assertIn("Requests against non-HTTPS endpoints are not allowed.", str(exc.exception))
185185

186186
def test_api_client_send_request_with_raw_data_serialized_for_json_content(
187187
self):

0 commit comments

Comments
 (0)