From 9c3062cbc79b0d9ffc963648b61695a6aeebda72 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Wed, 31 Jan 2024 16:11:52 +0300 Subject: [PATCH 01/17] Refactor request information to reduce code for generator methods --- .../default_query_parameters.py | 2 +- ...figuration.py => request_configuration.py} | 7 +++- kiota_abstractions/request_information.py | 40 ++++++++++++++----- kiota_abstractions/serialization/__init__.py | 1 + 4 files changed, 36 insertions(+), 14 deletions(-) rename kiota_abstractions/{base_request_configuration.py => request_configuration.py} (78%) diff --git a/kiota_abstractions/default_query_parameters.py b/kiota_abstractions/default_query_parameters.py index b5af0a1e..8d11f8e4 100644 --- a/kiota_abstractions/default_query_parameters.py +++ b/kiota_abstractions/default_query_parameters.py @@ -7,7 +7,7 @@ @dataclass -class GetQueryParameters: +class QueryParameters: """ Default placeholder class for query parameters. """ diff --git a/kiota_abstractions/base_request_configuration.py b/kiota_abstractions/request_configuration.py similarity index 78% rename from kiota_abstractions/base_request_configuration.py rename to kiota_abstractions/request_configuration.py index a912def2..5a3e0cd8 100644 --- a/kiota_abstractions/base_request_configuration.py +++ b/kiota_abstractions/request_configuration.py @@ -4,14 +4,15 @@ # See License in the project root for license information. # ------------------------------------ from dataclasses import dataclass -from typing import Dict, List, Optional, Union +from typing import Dict, List, Optional +from .default_query_parameters import QueryParameters from .headers_collection import HeadersCollection from .request_option import RequestOption @dataclass -class BaseRequestConfiguration: +class RequestConfiguration: """ Configuration for the request such as headers, query parameters, and middleware options. """ @@ -20,3 +21,5 @@ class BaseRequestConfiguration: # Request options options: Optional[List[RequestOption]] = None + + query_parameters: QueryParameters = None diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index b914396d..701e5d78 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -9,9 +9,11 @@ from stduritemplate import StdUriTemplate from ._version import VERSION +from .default_query_parameters import QueryParameters from .headers_collection import HeadersCollection from .method import Method from .request_option import RequestOption +from .request_configuration import RequestConfiguration from .serialization import Parsable, SerializationWriter if TYPE_CHECKING: @@ -23,11 +25,6 @@ tracer = trace.get_tracer(OBSERVABILITY_TRACER_NAME, VERSION) -@dataclass -class QueryParams: - pass - - class RequestInformation: """This class represents an abstract HTTP request""" @@ -36,23 +33,30 @@ class RequestInformation: CONTENT_TYPE_HEADER = "Content-Type" REQUEST_TYPE_KEY = "com.microsoft.kiota.request.type" - def __init__(self) -> None: + def __init__(self, method: Method = None, url_template: str = None, path_parameters: Dict[str, Any] = {}) -> None: + """Creates a new instance of the RequestInformation class. + + Args: + method (Method, optional): The request method. Defaults to None. + url_template (str, optional): The given url template. Defaults to None. + path_parameters (Dict[str, Any], optional): Path parameters for the request. Defaults to {}. + """ # The uri of the request self.__uri: Optional[Url] = None self.__request_options: Dict[str, RequestOption] = {} # The path parameters for the current request - self.path_parameters: Dict[str, Any] = {} + self.path_parameters: Dict[str, Any] = path_parameters # The URL template for the request - self.url_template: Optional[str] = None + self.url_template: Optional[str] = url_template # The HTTP Method for the request - self.http_method: Optional[Method] = None + self.http_method: Optional[Method] = method # The query parameters for the request - self.query_parameters: Dict[str, QueryParams] = {} + self.query_parameters: Dict[str, Any] = {} # The Request Headers self.headers: HeadersCollection = HeadersCollection() @@ -60,6 +64,20 @@ def __init__(self) -> None: # The Request Body self.content: Optional[BytesIO] = None + + def configure(self, request_configuration: RequestConfiguration) -> None: + """Configures the current request information headers, query parameters, and options + based on the request configuration provided + + Args: + request_configuration (RequestConfiguration): Configuration instance to + configure the request information. + """ + if request_configuration: + self.headers.add_all(request_configuration.headers) + self.add_request_options(request_configuration.options) + self.set_query_string_parameters_from_raw_object(request_configuration.query_parameters) + @property def url(self) -> Url: """Gets the URL of the request""" @@ -202,7 +220,7 @@ def set_stream_content(self, value: BytesIO, content_type: Optional[str] = None) self.headers.try_add(self.CONTENT_TYPE_HEADER, content_type) self.content = value - def set_query_string_parameters_from_raw_object(self, q: Optional[QueryParams]) -> None: + def set_query_string_parameters_from_raw_object(self, q: Optional[QueryParameters]) -> None: if q: for field in fields(q): key = field.name diff --git a/kiota_abstractions/serialization/__init__.py b/kiota_abstractions/serialization/__init__.py index b6d2f990..ff4a764d 100644 --- a/kiota_abstractions/serialization/__init__.py +++ b/kiota_abstractions/serialization/__init__.py @@ -1,4 +1,5 @@ from .additional_data_holder import AdditionalDataHolder +from .composed_type_wrapper import ComposedTypeWrapper from .parsable import Parsable from .parsable_factory import ParsableFactory from .parse_node import ParseNode From b86e700866e08f8c3ed058f44641571f2df77ef1 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Wed, 31 Jan 2024 16:12:02 +0300 Subject: [PATCH 02/17] Add test cases --- tests/test_request_information.py | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/test_request_information.py b/tests/test_request_information.py index 89073746..c7328ee0 100644 --- a/tests/test_request_information.py +++ b/tests/test_request_information.py @@ -1,7 +1,11 @@ import pytest +from dataclasses import dataclass +from typing import Optional from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.headers_collection import HeadersCollection +from kiota_abstractions.request_configuration import RequestConfiguration +from kiota_abstractions.default_query_parameters import QueryParameters def test_initialization(): @@ -83,3 +87,46 @@ def test_set_stream_content(mock_request_information): mock_request_information.set_stream_content(b'stream') assert mock_request_information.content == b'stream' assert mock_request_information.headers.get("content-type") == {"application/octet-stream"} + +def test_configure_empty_configuration(mock_request_information): + """Tests configuring the request information + """ + request_config = RequestConfiguration() + mock_request_information.configure(request_config) + assert not mock_request_information.headers.get_all() + assert not mock_request_information.request_options + assert not mock_request_information.query_parameters + +def test_configure_request_configuration(mock_request_information): + """Tests configuring the request information + """ + + @dataclass + class CustomParams(QueryParameters): + query1: Optional[str] = None + + def get_query_parameter(self,original_name: Optional[str] = None) -> str: + """ + Maps the query parameters names to their encoded names for the URI template parsing. + param original_name: The original query parameter name in the class. + Returns: str + """ + if not original_name: + raise TypeError("original_name cannot be null.") + if original_name == "query1": + return "%24query1" + + query_params = CustomParams(query1="value1") + + headers = HeadersCollection() + headers.add("header1", "value1") + headers.add("header2", "value2") + + request_config = RequestConfiguration(headers=headers, query_parameters=query_params) + + + mock_request_information.configure(request_config) + assert mock_request_information.headers.get("header1") == {"value1"} + assert mock_request_information.headers.get("header2") == {"value2"} + assert mock_request_information.query_parameters == {"%24query1": "value1"} + assert not mock_request_information.request_options \ No newline at end of file From c0891d28967d295dca5aca8f7775f977e12ea355 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Wed, 31 Jan 2024 16:12:12 +0300 Subject: [PATCH 03/17] Bump minor version --- kiota_abstractions/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kiota_abstractions/_version.py b/kiota_abstractions/_version.py index f7adbeb1..42c1c2b2 100644 --- a/kiota_abstractions/_version.py +++ b/kiota_abstractions/_version.py @@ -1 +1 @@ -VERSION: str = "1.1.0" +VERSION: str = "1.2.0" From b4ec6a1a0f71a4231b867018d22d9ad8b774d1f7 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Wed, 31 Jan 2024 16:12:24 +0300 Subject: [PATCH 04/17] Add CHANGELOG entry --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4d6c4c2..f60d2ba2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.2.0] - 2024-01-31 + +### Added +- Added methods in request information to reduce the amount of code being generated. + ## [1.1.0] - 2024-01-25 ### Added From d904c76881bebeb90d858faa90abf0e923b39bed Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Wed, 31 Jan 2024 16:20:45 +0300 Subject: [PATCH 05/17] Fix build issues --- kiota_abstractions/request_configuration.py | 4 ++-- kiota_abstractions/request_information.py | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/kiota_abstractions/request_configuration.py b/kiota_abstractions/request_configuration.py index 5a3e0cd8..bf3dab08 100644 --- a/kiota_abstractions/request_configuration.py +++ b/kiota_abstractions/request_configuration.py @@ -21,5 +21,5 @@ class RequestConfiguration: # Request options options: Optional[List[RequestOption]] = None - - query_parameters: QueryParameters = None + + query_parameters: Optional[QueryParameters] = None diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index 701e5d78..91ac8ed1 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -12,8 +12,8 @@ from .default_query_parameters import QueryParameters from .headers_collection import HeadersCollection from .method import Method -from .request_option import RequestOption from .request_configuration import RequestConfiguration +from .request_option import RequestOption from .serialization import Parsable, SerializationWriter if TYPE_CHECKING: @@ -33,13 +33,19 @@ class RequestInformation: CONTENT_TYPE_HEADER = "Content-Type" REQUEST_TYPE_KEY = "com.microsoft.kiota.request.type" - def __init__(self, method: Method = None, url_template: str = None, path_parameters: Dict[str, Any] = {}) -> None: + def __init__( + self, + method: Method, + url_template: str, + path_parameters: Dict[str, Any] = {} + ) -> None: """Creates a new instance of the RequestInformation class. Args: - method (Method, optional): The request method. Defaults to None. - url_template (str, optional): The given url template. Defaults to None. - path_parameters (Dict[str, Any], optional): Path parameters for the request. Defaults to {}. + method (Method): The request method. + url_template (str): The given url template. + path_parameters (Dict[str, Any], optional): Path parameters + for the request. Defaults to {}. """ # The uri of the request self.__uri: Optional[Url] = None @@ -64,7 +70,6 @@ def __init__(self, method: Method = None, url_template: str = None, path_paramet # The Request Body self.content: Optional[BytesIO] = None - def configure(self, request_configuration: RequestConfiguration) -> None: """Configures the current request information headers, query parameters, and options based on the request configuration provided @@ -77,7 +82,7 @@ def configure(self, request_configuration: RequestConfiguration) -> None: self.headers.add_all(request_configuration.headers) self.add_request_options(request_configuration.options) self.set_query_string_parameters_from_raw_object(request_configuration.query_parameters) - + @property def url(self) -> Url: """Gets the URL of the request""" @@ -123,7 +128,7 @@ def request_options(self) -> Dict[str, RequestOption]: """Gets the request options for the request.""" return self.__request_options - def add_request_options(self, options: List[RequestOption]) -> None: + def add_request_options(self, options: Optional[List[RequestOption]]) -> None: if not options: return for option in options: From decc4bd4937372363a3fbcc1c53a955ab24a0b95 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Wed, 31 Jan 2024 16:21:03 +0300 Subject: [PATCH 06/17] Update tests --- tests/conftest.py | 2 +- tests/test_request_information.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 138d0f80..ad6bece9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -117,7 +117,7 @@ def mock_user(): @pytest.fixture def mock_request_information(): - request_info = RequestInformation() + request_info = RequestInformation(None, None) request_info.url = "https://example.com" return request_info diff --git a/tests/test_request_information.py b/tests/test_request_information.py index c7328ee0..2e6e1b5f 100644 --- a/tests/test_request_information.py +++ b/tests/test_request_information.py @@ -11,7 +11,7 @@ def test_initialization(): """Tests initialization of the RequestInformation objects """ - request_info = RequestInformation() + request_info = RequestInformation(None, None) assert request_info assert not request_info.path_parameters assert not request_info.query_parameters From db37cc7e558cb7adcfefd41b7b67e17c0342e0b9 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Wed, 31 Jan 2024 16:24:29 +0300 Subject: [PATCH 07/17] Code formatting --- kiota_abstractions/request_information.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index 91ac8ed1..2e6e02e3 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -34,10 +34,7 @@ class RequestInformation: REQUEST_TYPE_KEY = "com.microsoft.kiota.request.type" def __init__( - self, - method: Method, - url_template: str, - path_parameters: Dict[str, Any] = {} + self, method: Method, url_template: str, path_parameters: Dict[str, Any] = {} ) -> None: """Creates a new instance of the RequestInformation class. From eb2be14cf04388d90b2e91a5f3a0fa8f5f903f60 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Thu, 1 Feb 2024 13:37:53 +0300 Subject: [PATCH 08/17] Revert breaking changes --- kiota_abstractions/default_query_parameters.py | 2 +- kiota_abstractions/request_configuration.py | 6 +++--- kiota_abstractions/request_information.py | 13 ++++++++----- tests/conftest.py | 2 +- tests/test_request_information.py | 12 ++++++------ 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/kiota_abstractions/default_query_parameters.py b/kiota_abstractions/default_query_parameters.py index 8d11f8e4..b5af0a1e 100644 --- a/kiota_abstractions/default_query_parameters.py +++ b/kiota_abstractions/default_query_parameters.py @@ -7,7 +7,7 @@ @dataclass -class QueryParameters: +class GetQueryParameters: """ Default placeholder class for query parameters. """ diff --git a/kiota_abstractions/request_configuration.py b/kiota_abstractions/request_configuration.py index bf3dab08..1f27f3cb 100644 --- a/kiota_abstractions/request_configuration.py +++ b/kiota_abstractions/request_configuration.py @@ -6,13 +6,13 @@ from dataclasses import dataclass from typing import Dict, List, Optional -from .default_query_parameters import QueryParameters +from .default_query_parameters import GetQueryParameters from .headers_collection import HeadersCollection from .request_option import RequestOption @dataclass -class RequestConfiguration: +class BaseRequestConfiguration: """ Configuration for the request such as headers, query parameters, and middleware options. """ @@ -22,4 +22,4 @@ class RequestConfiguration: # Request options options: Optional[List[RequestOption]] = None - query_parameters: Optional[QueryParameters] = None + query_parameters: Optional[GetQueryParameters] = None diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index 2e6e02e3..f2816307 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -9,10 +9,10 @@ from stduritemplate import StdUriTemplate from ._version import VERSION -from .default_query_parameters import QueryParameters +from .default_query_parameters import GetQueryParameters from .headers_collection import HeadersCollection from .method import Method -from .request_configuration import RequestConfiguration +from .request_configuration import BaseRequestConfiguration from .request_option import RequestOption from .serialization import Parsable, SerializationWriter @@ -34,7 +34,10 @@ class RequestInformation: REQUEST_TYPE_KEY = "com.microsoft.kiota.request.type" def __init__( - self, method: Method, url_template: str, path_parameters: Dict[str, Any] = {} + self, + method: Optional[Method] = None, + url_template: Optional[str] = None, + path_parameters: Dict[str, Any] = {} ) -> None: """Creates a new instance of the RequestInformation class. @@ -67,7 +70,7 @@ def __init__( # The Request Body self.content: Optional[BytesIO] = None - def configure(self, request_configuration: RequestConfiguration) -> None: + def configure(self, request_configuration: BaseRequestConfiguration) -> None: """Configures the current request information headers, query parameters, and options based on the request configuration provided @@ -222,7 +225,7 @@ def set_stream_content(self, value: BytesIO, content_type: Optional[str] = None) self.headers.try_add(self.CONTENT_TYPE_HEADER, content_type) self.content = value - def set_query_string_parameters_from_raw_object(self, q: Optional[QueryParameters]) -> None: + def set_query_string_parameters_from_raw_object(self, q: Optional[GetQueryParameters]) -> None: if q: for field in fields(q): key = field.name diff --git a/tests/conftest.py b/tests/conftest.py index ad6bece9..138d0f80 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -117,7 +117,7 @@ def mock_user(): @pytest.fixture def mock_request_information(): - request_info = RequestInformation(None, None) + request_info = RequestInformation() request_info.url = "https://example.com" return request_info diff --git a/tests/test_request_information.py b/tests/test_request_information.py index 2e6e1b5f..bd2a10e6 100644 --- a/tests/test_request_information.py +++ b/tests/test_request_information.py @@ -4,14 +4,14 @@ from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.headers_collection import HeadersCollection -from kiota_abstractions.request_configuration import RequestConfiguration -from kiota_abstractions.default_query_parameters import QueryParameters +from kiota_abstractions.request_configuration import BaseRequestConfiguration +from kiota_abstractions.default_query_parameters import GetQueryParameters def test_initialization(): """Tests initialization of the RequestInformation objects """ - request_info = RequestInformation(None, None) + request_info = RequestInformation() assert request_info assert not request_info.path_parameters assert not request_info.query_parameters @@ -91,7 +91,7 @@ def test_set_stream_content(mock_request_information): def test_configure_empty_configuration(mock_request_information): """Tests configuring the request information """ - request_config = RequestConfiguration() + request_config = BaseRequestConfiguration() mock_request_information.configure(request_config) assert not mock_request_information.headers.get_all() assert not mock_request_information.request_options @@ -102,7 +102,7 @@ def test_configure_request_configuration(mock_request_information): """ @dataclass - class CustomParams(QueryParameters): + class CustomParams(GetQueryParameters): query1: Optional[str] = None def get_query_parameter(self,original_name: Optional[str] = None) -> str: @@ -122,7 +122,7 @@ def get_query_parameter(self,original_name: Optional[str] = None) -> str: headers.add("header1", "value1") headers.add("header2", "value2") - request_config = RequestConfiguration(headers=headers, query_parameters=query_params) + request_config = BaseRequestConfiguration(headers=headers, query_parameters=query_params) mock_request_information.configure(request_config) From ca04c07d4f4ec5988707e3bb919a3f40c9272bb0 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Thu, 1 Feb 2024 13:51:30 +0300 Subject: [PATCH 09/17] Rename base request configuration file --- .../{request_configuration.py => base_request_configuration.py} | 0 kiota_abstractions/request_information.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename kiota_abstractions/{request_configuration.py => base_request_configuration.py} (100%) diff --git a/kiota_abstractions/request_configuration.py b/kiota_abstractions/base_request_configuration.py similarity index 100% rename from kiota_abstractions/request_configuration.py rename to kiota_abstractions/base_request_configuration.py diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index f2816307..c6c7a95a 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -9,10 +9,10 @@ from stduritemplate import StdUriTemplate from ._version import VERSION +from .base_request_configuration import BaseRequestConfiguration from .default_query_parameters import GetQueryParameters from .headers_collection import HeadersCollection from .method import Method -from .request_configuration import BaseRequestConfiguration from .request_option import RequestOption from .serialization import Parsable, SerializationWriter From 6264d5e9d157e40716df39e13eedffc0a3c113ea Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Thu, 1 Feb 2024 13:51:45 +0300 Subject: [PATCH 10/17] Update tests --- tests/test_request_information.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_request_information.py b/tests/test_request_information.py index bd2a10e6..b54c45d8 100644 --- a/tests/test_request_information.py +++ b/tests/test_request_information.py @@ -4,7 +4,7 @@ from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.headers_collection import HeadersCollection -from kiota_abstractions.request_configuration import BaseRequestConfiguration +from kiota_abstractions.base_request_configuration import BaseRequestConfiguration from kiota_abstractions.default_query_parameters import GetQueryParameters From dbc8ec68d5aaf75274a9cd2ce10b0ee34b827fa4 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Thu, 1 Feb 2024 18:55:14 +0300 Subject: [PATCH 11/17] Deprecate GetQueryParameters class --- kiota_abstractions/base_request_configuration.py | 4 ++-- kiota_abstractions/default_query_parameters.py | 12 +++++++++++- kiota_abstractions/request_information.py | 4 ++-- tests/test_request_information.py | 4 ++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/kiota_abstractions/base_request_configuration.py b/kiota_abstractions/base_request_configuration.py index 1f27f3cb..89df3742 100644 --- a/kiota_abstractions/base_request_configuration.py +++ b/kiota_abstractions/base_request_configuration.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from typing import Dict, List, Optional -from .default_query_parameters import GetQueryParameters +from .default_query_parameters import QueryParameters from .headers_collection import HeadersCollection from .request_option import RequestOption @@ -22,4 +22,4 @@ class BaseRequestConfiguration: # Request options options: Optional[List[RequestOption]] = None - query_parameters: Optional[GetQueryParameters] = None + query_parameters: Optional[QueryParameters] = None diff --git a/kiota_abstractions/default_query_parameters.py b/kiota_abstractions/default_query_parameters.py index b5af0a1e..cc3b1ef3 100644 --- a/kiota_abstractions/default_query_parameters.py +++ b/kiota_abstractions/default_query_parameters.py @@ -4,10 +4,20 @@ # See License in the project root for license information. # ------------------------------------ from dataclasses import dataclass +from warnings import warn @dataclass -class GetQueryParameters: +class QueryParameters: """ Default placeholder class for query parameters. """ + pass + + +@dataclass +class GetQueryParameters(QueryParameters): + """ + Default placeholder class for query parameters. + """ + warn("GetQueryParameters is deprecated. Use QueryParameters instead.", DeprecationWarning) diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index c6c7a95a..71c22d42 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -10,7 +10,7 @@ from ._version import VERSION from .base_request_configuration import BaseRequestConfiguration -from .default_query_parameters import GetQueryParameters +from .default_query_parameters import QueryParameters from .headers_collection import HeadersCollection from .method import Method from .request_option import RequestOption @@ -225,7 +225,7 @@ def set_stream_content(self, value: BytesIO, content_type: Optional[str] = None) self.headers.try_add(self.CONTENT_TYPE_HEADER, content_type) self.content = value - def set_query_string_parameters_from_raw_object(self, q: Optional[GetQueryParameters]) -> None: + def set_query_string_parameters_from_raw_object(self, q: Optional[QueryParameters]) -> None: if q: for field in fields(q): key = field.name diff --git a/tests/test_request_information.py b/tests/test_request_information.py index b54c45d8..85188a2b 100644 --- a/tests/test_request_information.py +++ b/tests/test_request_information.py @@ -5,7 +5,7 @@ from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.headers_collection import HeadersCollection from kiota_abstractions.base_request_configuration import BaseRequestConfiguration -from kiota_abstractions.default_query_parameters import GetQueryParameters +from kiota_abstractions.default_query_parameters import QueryParameters def test_initialization(): @@ -102,7 +102,7 @@ def test_configure_request_configuration(mock_request_information): """ @dataclass - class CustomParams(GetQueryParameters): + class CustomParams(QueryParameters): query1: Optional[str] = None def get_query_parameter(self,original_name: Optional[str] = None) -> str: From 8cb4b2a794a8c01bde36d2d73cda6e439762bc60 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Mon, 5 Feb 2024 15:35:24 +0300 Subject: [PATCH 12/17] Deprecate base request configuration class --- kiota_abstractions/base_request_configuration.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/kiota_abstractions/base_request_configuration.py b/kiota_abstractions/base_request_configuration.py index 89df3742..d54e5dc4 100644 --- a/kiota_abstractions/base_request_configuration.py +++ b/kiota_abstractions/base_request_configuration.py @@ -5,6 +5,7 @@ # ------------------------------------ from dataclasses import dataclass from typing import Dict, List, Optional +from warnings import warn from .default_query_parameters import QueryParameters from .headers_collection import HeadersCollection @@ -12,7 +13,7 @@ @dataclass -class BaseRequestConfiguration: +class RequestConfiguration: """ Configuration for the request such as headers, query parameters, and middleware options. """ @@ -23,3 +24,11 @@ class BaseRequestConfiguration: options: Optional[List[RequestOption]] = None query_parameters: Optional[QueryParameters] = None + + +@dataclass +class BaseRequestConfiguration: + warn( + "BaseRequestConfiguration is deprecated. Use RequestConfiguration class instead.", + DeprecationWarning + ) From 77be2a2c334156eb2ffc560a3ee9756eb60a6168 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Mon, 5 Feb 2024 15:36:05 +0300 Subject: [PATCH 13/17] Add generic query parameters --- .../default_query_parameters.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/kiota_abstractions/default_query_parameters.py b/kiota_abstractions/default_query_parameters.py index cc3b1ef3..21842406 100644 --- a/kiota_abstractions/default_query_parameters.py +++ b/kiota_abstractions/default_query_parameters.py @@ -4,6 +4,7 @@ # See License in the project root for license information. # ------------------------------------ from dataclasses import dataclass +from typing import List, Optional from warnings import warn @@ -12,7 +13,26 @@ class QueryParameters: """ Default placeholder class for query parameters. """ - pass + # Include count of items + count: Optional[bool] = None + + # Expand related entities + expand: Optional[List[str]] = None + + # Filter items by property values + filter: Optional[str] = None + + # Order items by property values + orderby: Optional[List[str]] = None + + # Select properties to be returned + select: Optional[List[str]] = None + + # Skip the first n items + skip: Optional[int] = None + + # Show only the first n items + top: Optional[int] = None @dataclass From 8e48428e037751811eadc4a305167925f5b6d8b2 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Mon, 5 Feb 2024 15:36:26 +0300 Subject: [PATCH 14/17] Update tests --- kiota_abstractions/request_information.py | 4 ++-- tests/test_request_information.py | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index 71c22d42..040ec2b6 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -9,7 +9,7 @@ from stduritemplate import StdUriTemplate from ._version import VERSION -from .base_request_configuration import BaseRequestConfiguration +from .base_request_configuration import RequestConfiguration from .default_query_parameters import QueryParameters from .headers_collection import HeadersCollection from .method import Method @@ -70,7 +70,7 @@ def __init__( # The Request Body self.content: Optional[BytesIO] = None - def configure(self, request_configuration: BaseRequestConfiguration) -> None: + def configure(self, request_configuration: RequestConfiguration) -> None: """Configures the current request information headers, query parameters, and options based on the request configuration provided diff --git a/tests/test_request_information.py b/tests/test_request_information.py index 85188a2b..df76f4ed 100644 --- a/tests/test_request_information.py +++ b/tests/test_request_information.py @@ -4,7 +4,7 @@ from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.headers_collection import HeadersCollection -from kiota_abstractions.base_request_configuration import BaseRequestConfiguration +from kiota_abstractions.base_request_configuration import RequestConfiguration from kiota_abstractions.default_query_parameters import QueryParameters @@ -91,7 +91,7 @@ def test_set_stream_content(mock_request_information): def test_configure_empty_configuration(mock_request_information): """Tests configuring the request information """ - request_config = BaseRequestConfiguration() + request_config = RequestConfiguration() mock_request_information.configure(request_config) assert not mock_request_information.headers.get_all() assert not mock_request_information.request_options @@ -113,20 +113,22 @@ def get_query_parameter(self,original_name: Optional[str] = None) -> str: """ if not original_name: raise TypeError("original_name cannot be null.") - if original_name == "query1": - return "%24query1" + if original_name == "filter": + return "%24filter" - query_params = CustomParams(query1="value1") + query_params = CustomParams(filter="query1") headers = HeadersCollection() headers.add("header1", "value1") headers.add("header2", "value2") - request_config = BaseRequestConfiguration(headers=headers, query_parameters=query_params) + request_config = RequestConfiguration(headers=headers, query_parameters=query_params) mock_request_information.configure(request_config) assert mock_request_information.headers.get("header1") == {"value1"} assert mock_request_information.headers.get("header2") == {"value2"} - assert mock_request_information.query_parameters == {"%24query1": "value1"} + key, value = "%24filter", "query1" + assert (key in mock_request_information.query_parameters + and mock_request_information.query_parameters[key] == value) assert not mock_request_information.request_options \ No newline at end of file From 4de934f6491a7e3dd4a5113ee771909a59bee594 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Mon, 5 Feb 2024 15:58:49 +0300 Subject: [PATCH 15/17] Filter unused query params --- kiota_abstractions/request_information.py | 4 +++- tests/test_request_information.py | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index 040ec2b6..47c47fbf 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -233,7 +233,9 @@ def set_query_string_parameters_from_raw_object(self, q: Optional[QueryParameter serialization_key = q.get_query_parameter(key) # type: ignore if serialization_key: key = serialization_key - self.query_parameters[key] = getattr(q, field.name) + value = getattr(q, field.name) + if value is not None: + self.query_parameters[key] = value def _get_serialization_writer( self, diff --git a/tests/test_request_information.py b/tests/test_request_information.py index df76f4ed..f6fb873b 100644 --- a/tests/test_request_information.py +++ b/tests/test_request_information.py @@ -128,7 +128,5 @@ def get_query_parameter(self,original_name: Optional[str] = None) -> str: mock_request_information.configure(request_config) assert mock_request_information.headers.get("header1") == {"value1"} assert mock_request_information.headers.get("header2") == {"value2"} - key, value = "%24filter", "query1" - assert (key in mock_request_information.query_parameters - and mock_request_information.query_parameters[key] == value) + assert mock_request_information.query_parameters == {"%24filter": "query1"} assert not mock_request_information.request_options \ No newline at end of file From 0f46aaabff9d0827ace0022c7d2e5c18c27c9fde Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Mon, 5 Feb 2024 16:21:35 +0300 Subject: [PATCH 16/17] BaseRequestConfiguration to inherit from RequestConfiguration --- kiota_abstractions/base_request_configuration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kiota_abstractions/base_request_configuration.py b/kiota_abstractions/base_request_configuration.py index d54e5dc4..3d6e48ef 100644 --- a/kiota_abstractions/base_request_configuration.py +++ b/kiota_abstractions/base_request_configuration.py @@ -27,7 +27,7 @@ class RequestConfiguration: @dataclass -class BaseRequestConfiguration: +class BaseRequestConfiguration(RequestConfiguration): warn( "BaseRequestConfiguration is deprecated. Use RequestConfiguration class instead.", DeprecationWarning From 8401c4d727a00ea9339a96b28866147ed63ad6fb Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Mon, 5 Feb 2024 17:51:43 +0300 Subject: [PATCH 17/17] Use generic type for query parameters --- .../base_request_configuration.py | 10 +++++----- .../default_query_parameters.py | 20 ------------------- kiota_abstractions/request_information.py | 4 ++-- tests/test_request_information.py | 9 +++------ 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/kiota_abstractions/base_request_configuration.py b/kiota_abstractions/base_request_configuration.py index 3d6e48ef..1cc0ecfc 100644 --- a/kiota_abstractions/base_request_configuration.py +++ b/kiota_abstractions/base_request_configuration.py @@ -4,25 +4,25 @@ # See License in the project root for license information. # ------------------------------------ from dataclasses import dataclass -from typing import Dict, List, Optional +from typing import Generic, List, Optional, TypeVar from warnings import warn -from .default_query_parameters import QueryParameters from .headers_collection import HeadersCollection from .request_option import RequestOption +QueryParameters = TypeVar('QueryParameters') + @dataclass -class RequestConfiguration: +class RequestConfiguration(Generic[QueryParameters]): """ Configuration for the request such as headers, query parameters, and middleware options. """ # Request headers headers: HeadersCollection = HeadersCollection() - # Request options options: Optional[List[RequestOption]] = None - + # Request query parameters query_parameters: Optional[QueryParameters] = None diff --git a/kiota_abstractions/default_query_parameters.py b/kiota_abstractions/default_query_parameters.py index 21842406..6bd71daf 100644 --- a/kiota_abstractions/default_query_parameters.py +++ b/kiota_abstractions/default_query_parameters.py @@ -13,26 +13,6 @@ class QueryParameters: """ Default placeholder class for query parameters. """ - # Include count of items - count: Optional[bool] = None - - # Expand related entities - expand: Optional[List[str]] = None - - # Filter items by property values - filter: Optional[str] = None - - # Order items by property values - orderby: Optional[List[str]] = None - - # Select properties to be returned - select: Optional[List[str]] = None - - # Skip the first n items - skip: Optional[int] = None - - # Show only the first n items - top: Optional[int] = None @dataclass diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index 47c47fbf..fb2fa4a3 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -10,7 +10,6 @@ from ._version import VERSION from .base_request_configuration import RequestConfiguration -from .default_query_parameters import QueryParameters from .headers_collection import HeadersCollection from .method import Method from .request_option import RequestOption @@ -21,6 +20,7 @@ Url = str T = TypeVar("T", bound=Parsable) +QueryParameters = TypeVar('QueryParameters') OBSERVABILITY_TRACER_NAME = "microsoft-python-kiota-abstractions" tracer = trace.get_tracer(OBSERVABILITY_TRACER_NAME, VERSION) @@ -227,7 +227,7 @@ def set_stream_content(self, value: BytesIO, content_type: Optional[str] = None) def set_query_string_parameters_from_raw_object(self, q: Optional[QueryParameters]) -> None: if q: - for field in fields(q): + for field in fields(q): # type: ignore key = field.name if hasattr(q, "get_query_parameter"): serialization_key = q.get_query_parameter(key) # type: ignore diff --git a/tests/test_request_information.py b/tests/test_request_information.py index f6fb873b..e83a88cc 100644 --- a/tests/test_request_information.py +++ b/tests/test_request_information.py @@ -5,7 +5,6 @@ from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.headers_collection import HeadersCollection from kiota_abstractions.base_request_configuration import RequestConfiguration -from kiota_abstractions.default_query_parameters import QueryParameters def test_initialization(): @@ -102,8 +101,8 @@ def test_configure_request_configuration(mock_request_information): """ @dataclass - class CustomParams(QueryParameters): - query1: Optional[str] = None + class CustomParams: + filter: Optional[str] = None def get_query_parameter(self,original_name: Optional[str] = None) -> str: """ @@ -117,14 +116,12 @@ def get_query_parameter(self,original_name: Optional[str] = None) -> str: return "%24filter" query_params = CustomParams(filter="query1") - headers = HeadersCollection() headers.add("header1", "value1") headers.add("header2", "value2") request_config = RequestConfiguration(headers=headers, query_parameters=query_params) - - + mock_request_information.configure(request_config) assert mock_request_information.headers.get("header1") == {"value1"} assert mock_request_information.headers.get("header2") == {"value2"}