-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from HealthITAU/fix/pydantic2.0
- Loading branch information
Showing
746 changed files
with
818 additions
and
4,458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,6 @@ __pycache__/ | |
|
||
test.py | ||
gen.py | ||
setup.cfg | ||
setup.py | ||
|
||
# C extensions | ||
*.so | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
requests | ||
pydantic<2 | ||
pydantic>=2 | ||
jinja2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[metadata] | ||
description_file=README.md | ||
license_files=LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from setuptools import setup | ||
from setuptools import find_packages | ||
import subprocess | ||
import os | ||
|
||
pycw_version = subprocess.run(['git', 'describe', '--tags'], stdout=subprocess.PIPE).stdout.decode('utf-8').strip() | ||
with open(os.path.join(os.path.dirname(__file__), 'README.md')) as file: | ||
long_description = file.read() | ||
|
||
setup( | ||
name='pyconnectwise', | ||
version=pycw_version, | ||
license='gpl-3.0', | ||
author="Health IT", | ||
author_email='[email protected]', | ||
description='A full-featured Python client for the ConnectWise API\'s', | ||
long_description=long_description, | ||
long_description_content_type='text/markdown', | ||
url='https://github.com/HealthITAU/pyconnectwise', | ||
keywords=['ConnectWise', 'Manage', 'Automate', 'API', 'Python', 'Client', 'Annotated', 'Typed', 'MSP'], | ||
install_requires=[ | ||
'requests', | ||
'pydantic>=2', | ||
'jinja2' | ||
], | ||
classifiers=[ | ||
'Development Status :: 4 - Beta', | ||
'Intended Audience :: Developers', | ||
'Topic :: Software Development :: Libraries :: Python Modules', | ||
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)', | ||
'Programming Language :: Python :: 3.11', | ||
'Programming Language :: Python :: 3.10', | ||
], | ||
package_dir = {"":"src"}, | ||
packages = find_packages(where="src"), | ||
python_requires = ">=3.10.1" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,57 @@ | ||
from __future__ import annotations | ||
from pydantic import BaseModel | ||
from pydantic import BaseModel, ConfigDict | ||
from pyconnectwise.utils.naming import to_camel_case | ||
from typing import Optional, Type, Any, TypeVar | ||
|
||
Model = TypeVar('Model', bound='BaseModel') | ||
|
||
class ConnectWiseModel(BaseModel): | ||
|
||
class Config: | ||
alias_generator = to_camel_case | ||
allow_population_by_field_name = True | ||
use_enum_values = True | ||
model_config = ConfigDict(alias_generator=to_camel_case, populate_by_name=True, use_enum_values=True, protected_namespaces=()) | ||
|
||
@classmethod | ||
def construct(cls: Type['ConnectWiseModel'], **values: Any) -> 'ConnectWiseModel': | ||
def model_construct(cls: Type['ConnectWiseModel'], **values: Any) -> 'ConnectWiseModel': | ||
""" | ||
Overriding Pydantics construct method to allow for nested models. | ||
""" | ||
m = cls.__new__(cls) | ||
fields_values: dict[str, Any] = {} | ||
defaults: dict[str, Any] = {} | ||
|
||
def handle_nested_model(field, value): | ||
if issubclass(field.type_, BaseModel): | ||
if issubclass(type(field.annotation), BaseModel): | ||
if isinstance(value, list): | ||
return [field.type_.construct(**v) for v in value] | ||
return [field.annotation.model_construct(**v) for v in value] | ||
else: | ||
return field.type_.construct(**value) | ||
return field.annotation.model_construct(**value) | ||
else: | ||
return value | ||
|
||
for name, field in cls.__fields__.items(): | ||
if field.alt_alias and field.alias in values: | ||
for name, field in cls.model_fields.items(): | ||
if field.alias and field.alias in values: | ||
fields_values[name] = handle_nested_model(field, values[field.alias]) | ||
elif name in values: | ||
fields_values[name] = handle_nested_model(field, values[name]) | ||
elif not field.required: | ||
fields_values[name] = field.get_default() | ||
else: | ||
value = values.get(name) | ||
if value is None: | ||
fields_values[name] = None | ||
continue | ||
fields_values[name] = handle_nested_model(field, value) | ||
elif not field.is_required(): | ||
defaults[name] = field.get_default(call_default_factory=True) | ||
fields_values.update(defaults) | ||
|
||
_extra: dict[str, Any] | None = None | ||
if cls.model_config.get('extra') == 'allow': | ||
_extra = {} | ||
for k, v in values.items(): | ||
_extra[k] = v | ||
else: | ||
fields_values.update(values) | ||
object.__setattr__(m, '__dict__', fields_values) | ||
object.__setattr__(m, '__fields_set__', set(values.keys())) | ||
m._init_private_attributes() | ||
object.__setattr__(m, '__pydantic_fields_set__', fields_values.keys()) | ||
if not cls.__pydantic_root_model__: | ||
object.__setattr__(m, '__pydantic_extra__', _extra) | ||
|
||
if cls.__pydantic_post_init__: | ||
m.model_post_init(None) | ||
elif not cls.__pydantic_root_model__: | ||
# Note: if there are any private attributes, cls.__pydantic_post_init__ would exist | ||
# Since it doesn't, that means that `__pydantic_private__` should be set to None | ||
object.__setattr__(m, '__pydantic_private__', None) | ||
|
||
return m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.