Skip to content

Commit

Permalink
Support overwriting api-data.line.me in v3 packages (#619)
Browse files Browse the repository at this point in the history
The line-bot-sdk-python has transitioned to version 3, introducing
automatic code generation. However, it did not continue to support
overwriting at api-data.line.me, which was available up to version 2.
This update reintroduces support for overwriting at api-data.line.me in
version 3.


Resolve #616
  • Loading branch information
Yang-33 authored Apr 13, 2024
1 parent 9c0ce8b commit ada22dd
Show file tree
Hide file tree
Showing 55 changed files with 487 additions and 638 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class {{classname}}(object):
if api_client is None:
api_client = ApiClient.get_default()
self.api_client = api_client
self.line_base_path = "{{basePath}}"

{{#operation}}

{{#asyncio}}
Expand Down Expand Up @@ -144,6 +146,9 @@ class {{classname}}(object):
)
_host = _hosts[_host_index]
{{/servers.0}}
{{^servers.0}}
_host = self.line_base_path
{{/servers.0}}
_params = locals()

_all_params = [
Expand Down Expand Up @@ -301,9 +306,7 @@ class {{classname}}(object):
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
{{#servers.0}}
_host=_host,
{{/servers.0}}
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))
{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class ApiClient(object):
body = self.sanitize_for_serialization(body)

# request url
if _host is None:
if self.configuration.host is not None:
url = self.configuration.host + resource_path
else:
# use server/host defined in path or operation instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class AsyncApiClient(object):
body = self.sanitize_for_serialization(body)

# request url
if _host is None:
if self.configuration.host is not None:
url = self.configuration.host + resource_path
else:
# use server/host defined in path or operation instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Async{{classname}}(object):
if api_client is None:
api_client = AsyncApiClient.get_default()
self.api_client = api_client
self.line_base_path = "{{basePath}}"

{{#operation}}

@overload
Expand Down Expand Up @@ -141,6 +143,9 @@ class Async{{classname}}(object):
)
_host = _hosts[_host_index]
{{/servers.0}}
{{^servers.0}}
_host = self.line_base_path
{{/servers.0}}
_params = locals()

_all_params = [
Expand Down Expand Up @@ -298,9 +303,7 @@ class Async{{classname}}(object):
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
{{#servers.0}}
_host=_host,
{{/servers.0}}
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))
{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ conf = {{{packageName}}}.Configuration(
):
"""Constructor
"""
self._base_path = "{{{basePath}}}" if host is None else host
self._base_path = host
"""Default Base url
"""
self.server_index = 0 if server_index is None and host is None else server_index
Expand Down Expand Up @@ -526,87 +526,7 @@ conf = {{{packageName}}}.Configuration(
"SDK Package Version: {__version__}".\
format(env=sys.platform, pyversion=sys.version, package_version=__version__)

def get_host_settings(self):
"""Gets an array of host settings

:return: An array of host settings
"""
return [
{{#servers}}
{
'url': "{{{url}}}",
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}
{{#-first}}
'variables': {
{{/-first}}
'{{{name}}}': {
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
'default_value': "{{{defaultValue}}}",
{{#enumValues}}
{{#-first}}
'enum_values': [
{{/-first}}
"{{{.}}}"{{^-last}},{{/-last}}
{{#-last}}
]
{{/-last}}
{{/enumValues}}
}{{^-last}},{{/-last}}
{{#-last}}
}
{{/-last}}
{{/variables}}
}{{^-last}},{{/-last}}
{{/servers}}
]

def get_host_from_settings(self, index, variables=None, servers=None):
"""Gets host URL based on the index and variables
:param index: array index of the host settings
:param variables: hash of variable and the corresponding value
:param servers: an array of host settings or None
:return: URL based on host settings
"""
if index is None:
return self._base_path

variables = {} if variables is None else variables
servers = self.get_host_settings() if servers is None else servers

try:
server = servers[index]
except IndexError:
raise ValueError(
"Invalid index {0} when selecting the host settings. "
"Must be less than {1}".format(index, len(servers)))

url = server['url']

# go through variables and replace placeholders
for variable_name, variable in server.get('variables', {}).items():
used_value = variables.get(
variable_name, variable['default_value'])

if 'enum_values' in variable \
and used_value not in variable['enum_values']:
raise ValueError(
"The variable `{0}` in the host URL has invalid value "
"{1}. Must be {2}.".format(
variable_name, variables[variable_name],
variable['enum_values']))

url = url.replace("{" + variable_name + "}", used_value)

return url

@property
def host(self):
"""Return generated host."""
return self.get_host_from_settings(self.server_index, variables=self.server_variables)

@host.setter
def host(self, value):
"""Fix base path."""
self._base_path = value
self.server_index = None
return self._base_path
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
{{#required}}
{{#isNullable}}
if value is None:
return v
return value

{{/isNullable}}
{{/required}}
Expand Down
24 changes: 24 additions & 0 deletions linebot/v3/audience/api/async_manage_audience.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def __init__(self, api_client=None):
if api_client is None:
api_client = AsyncApiClient.get_default()
self.api_client = api_client
self.line_base_path = "https://api.line.me"


@overload
async def activate_audience_group(self, audience_group_id : Annotated[StrictInt, Field(..., description="The audience ID.")], **kwargs) -> None: # noqa: E501
Expand Down Expand Up @@ -136,6 +138,7 @@ def activate_audience_group_with_http_info(self, audience_group_id : Annotated[S
:rtype: None
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -199,6 +202,7 @@ def activate_audience_group_with_http_info(self, audience_group_id : Annotated[S
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -279,6 +283,7 @@ def add_audience_to_audience_group_with_http_info(self, add_audience_to_audience
:rtype: None
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -349,6 +354,7 @@ def add_audience_to_audience_group_with_http_info(self, add_audience_to_audience
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -429,6 +435,7 @@ def create_audience_group_with_http_info(self, create_audience_group_request : C
:rtype: tuple(CreateAudienceGroupResponse, status_code(int), headers(HTTPHeaderDict))
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -505,6 +512,7 @@ def create_audience_group_with_http_info(self, create_audience_group_request : C
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -585,6 +593,7 @@ def create_click_based_audience_group_with_http_info(self, create_click_based_au
:rtype: tuple(CreateClickBasedAudienceGroupResponse, status_code(int), headers(HTTPHeaderDict))
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -661,6 +670,7 @@ def create_click_based_audience_group_with_http_info(self, create_click_based_au
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -741,6 +751,7 @@ def create_imp_based_audience_group_with_http_info(self, create_imp_based_audien
:rtype: tuple(CreateImpBasedAudienceGroupResponse, status_code(int), headers(HTTPHeaderDict))
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -817,6 +828,7 @@ def create_imp_based_audience_group_with_http_info(self, create_imp_based_audien
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -897,6 +909,7 @@ def delete_audience_group_with_http_info(self, audience_group_id : Annotated[Str
:rtype: None
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -960,6 +973,7 @@ def delete_audience_group_with_http_info(self, audience_group_id : Annotated[Str
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -1040,6 +1054,7 @@ def get_audience_data_with_http_info(self, audience_group_id : Annotated[StrictI
:rtype: tuple(GetAudienceDataResponse, status_code(int), headers(HTTPHeaderDict))
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -1110,6 +1125,7 @@ def get_audience_data_with_http_info(self, audience_group_id : Annotated[StrictI
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -1186,6 +1202,7 @@ def get_audience_group_authority_level_with_http_info(self, **kwargs) -> ApiResp
:rtype: tuple(GetAudienceGroupAuthorityLevelResponse, status_code(int), headers(HTTPHeaderDict))
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -1251,6 +1268,7 @@ def get_audience_group_authority_level_with_http_info(self, **kwargs) -> ApiResp
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -1351,6 +1369,7 @@ def get_audience_groups_with_http_info(self, page : Annotated[conint(strict=True
:rtype: tuple(GetAudienceGroupsResponse, status_code(int), headers(HTTPHeaderDict))
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -1440,6 +1459,7 @@ def get_audience_groups_with_http_info(self, page : Annotated[conint(strict=True
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -1520,6 +1540,7 @@ def update_audience_group_authority_level_with_http_info(self, update_audience_g
:rtype: None
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -1590,6 +1611,7 @@ def update_audience_group_authority_level_with_http_info(self, update_audience_g
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

Expand Down Expand Up @@ -1674,6 +1696,7 @@ def update_audience_group_description_with_http_info(self, audience_group_id : A
:rtype: None
"""

_host = self.line_base_path
_params = locals()

_all_params = [
Expand Down Expand Up @@ -1748,5 +1771,6 @@ def update_audience_group_description_with_http_info(self, audience_group_id : A
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
_host=_host,
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))
2 changes: 2 additions & 0 deletions linebot/v3/audience/api/async_manage_audience_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def __init__(self, api_client=None):
if api_client is None:
api_client = AsyncApiClient.get_default()
self.api_client = api_client
self.line_base_path = "https://api.line.me"


@overload
async def add_user_ids_to_audience(self, file : Annotated[Union[StrictBytes, StrictStr], Field(..., description="A text file with one user ID or IFA entered per line. Specify text/plain as Content-Type. Max file number: 1 Max number: 1,500,000 ")], audience_group_id : Annotated[Optional[StrictInt], Field(description="The audience ID.")] = None, upload_description : Annotated[Optional[StrictStr], Field(description="The description to register with the job")] = None, **kwargs) -> None: # noqa: E501
Expand Down
Loading

0 comments on commit ada22dd

Please sign in to comment.