Skip to content

Commit efdb91f

Browse files
committed
Add initial CreateMixin
Add initial `transip.mixins.CreateMixin` to allow API objects to be created. Even though this should allow all objects to be created, the validation of the required attributes is still missing. Fixes #15 Signed-off-by: Roald Nefs <info@roaldnefs.com>
1 parent 85b5c73 commit efdb91f

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

tests/services/test_ssh_keys.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
# You should have received a copy of the GNU Lesser General Public License
1818
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
1919

20-
from typing import Type, List
20+
from typing import Type, List, Tuple, Any, Dict
2121
import responses # type: ignore
22+
import json
2223

2324
from transip import TransIP
2425
from transip.v6.services.ssh_key import SshKey
@@ -82,3 +83,25 @@ def test_ssh_keys_delete(transip_minimal_client: Type[TransIP]) -> None:
8283
transip_minimal_client.ssh_keys.delete(ssh_key_id)
8384
except Exception as exc:
8485
assert False, f"'transip.TransIP.ssh_keys.delete' raised an exception {exc}"
86+
87+
88+
@responses.activate
89+
def test_ssh_keys_create(transip_minimal_client: Type[TransIP]) -> None:
90+
ssh_key_data: Dict[str, str] = {
91+
"sshKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDf2pxWX/yhUBDyk2LPhvRtI0LnVO8PyR5Zt6AHrnhtLGqK+8YG9EMlWbCCWrASR+Q1hFQG example",
92+
"description": "Jim key"
93+
}
94+
95+
responses.add(
96+
responses.POST,
97+
"https://api.transip.nl/v6/ssh-keys",
98+
status=201,
99+
content_type='application/json',
100+
match=[
101+
responses.json_params_matcher(ssh_key_data),
102+
]
103+
)
104+
105+
transip_minimal_client.ssh_keys.create(ssh_key_data)
106+
107+
assert len(responses.calls) == 1

transip/mixins.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# You should have received a copy of the GNU Lesser General Public License
1818
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
1919

20-
from typing import Optional, List, Type
20+
from typing import Optional, List, Type, Dict, Any
2121

2222
from transip import TransIP
2323
from transip.base import ApiObject
@@ -76,3 +76,19 @@ def list(self, **kwargs) -> List[Type[ApiObject]]:
7676
for obj in self.client.get(self.path)[self._resp_list_attr]:
7777
objs.append(self._obj_cls(self, obj)) # type: ignore
7878
return objs
79+
80+
81+
class CreateMixin:
82+
"""
83+
Create a new ApiObject.
84+
"""
85+
86+
client: TransIP
87+
path: str
88+
89+
def create(self, data: Optional[Dict[str, Any]]=None, **kwargs):
90+
if data is None:
91+
data = {}
92+
93+
if self.path:
94+
self.client.post(self.path, json=data)

transip/v6/services/ssh_key.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
from typing import Optional, Type
2121

2222
from transip.base import ApiService, ApiObject
23-
from transip.mixins import GetMixin, DeleteMixin, ListMixin
23+
from transip.mixins import GetMixin, DeleteMixin, ListMixin, CreateMixin
2424

2525

2626
class SshKey(ApiObject):
2727

2828
_id_attr: str = "id"
2929

3030

31-
class SshKeyService(GetMixin, DeleteMixin, ListMixin, ApiService):
31+
class SshKeyService(GetMixin, CreateMixin, DeleteMixin, ListMixin, ApiService):
3232

3333
_path: str = "/ssh-keys"
3434
_obj_cls: Optional[Type[ApiObject]] = SshKey

0 commit comments

Comments
 (0)