Skip to content

Commit c05bce9

Browse files
committed
Fix upload_file functionality.
1 parent e3ba11b commit c05bce9

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

c8y_api/_base_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ def post_file(self, resource, file, binary_meta_information):
7575
7676
Used for posting binary data.
7777
"""
78-
assert isinstance(binary_meta_information, Binary)
78+
# assert isinstance(binary_meta_information, Binary)
7979
assert file is not None
8080

8181
headers = {'Accept': 'application/json', **self.__default_headers}
8282

83+
import json as js
8384
payload = {
84-
'object': (None, str(binary_meta_information._to_full_json()).replace("'", '"')),
85+
'object': (None, js.dumps(binary_meta_information.to_json())),
8586
'filesize': (None, sys.getsizeof(file)),
8687
'file': (None, file.read())
8788
}

c8y_api/_main_api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from c8y_api._base_api import CumulocityRestApi
88

9-
from c8y_api.model.inventory import Inventory, Identity, DeviceGroupInventory, DeviceInventory
9+
from c8y_api.model.inventory import Inventory, Identity, Binaries, DeviceGroupInventory, DeviceInventory
1010
from c8y_api.model.administration import Users, GlobalRoles, InventoryRoles
1111
from c8y_api.model.measurements import Measurements
1212
from c8y_api.model.applications import Applications
@@ -20,6 +20,7 @@ def __init__(self, base_url, tenant_id, username, password, tfa_token=None, appl
2020
super().__init__(base_url, tenant_id, username, password, tfa_token, application_key)
2121
self.__measurements = Measurements(self)
2222
self.__inventory = Inventory(self)
23+
self.__binaries = Binaries(self)
2324
self.__group_inventory = DeviceGroupInventory(self)
2425
self.__device_inventory = DeviceInventory(self)
2526
self.__identity = Identity(self)
@@ -38,6 +39,10 @@ def measurements(self):
3839
def inventory(self):
3940
return self.__inventory
4041

42+
@property
43+
def binaries(self):
44+
return self.__binaries
45+
4146
@property
4247
def group_inventory(self):
4348
return self.__group_inventory

integration_tests/test_binaries.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) 2020 Software AG,
2+
# Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA,
3+
# and/or its subsidiaries and/or its affiliates and/or their licensors.
4+
# Use, reproduction, transfer, publication or disclosure is prohibited except
5+
# as specifically provided for in your License Agreement with Software AG.
6+
7+
from c8y_api.app import CumulocityApi
8+
from c8y_api.model import Binary
9+
10+
11+
def test_upload_file():
12+
13+
c8y = CumulocityApi()
14+
binary = Binary(filename='some_file.py', media_type='text/raw')
15+
reponse = c8y.binaries.upload(binary, __file__)
16+
assert reponse
17+
assert c8y.tenant_id in reponse['self']

0 commit comments

Comments
 (0)