Skip to content

Commit 7ca6506

Browse files
authored
Merge pull request #49 from eukarya-inc/feature/47/upload_file
#47 add upload function
2 parents 96b09ef + d787f62 commit 7ca6506

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

plateauutils/citygmlfinder/from_reearth_cms.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
22
import reearthcmsapi
3-
from reearthcmsapi.apis.tags import items_project_api
3+
from reearthcmsapi.apis.tags import items_project_api, assets_project_api
4+
from reearthcmsapi.model.asset import Asset
45
from reearthcmsapi.model.model import Model
56
from reearthcmsapi.model.versioned_item import VersionedItem
67
from reearthcmsapi.model.asset_embedding import AssetEmbedding
@@ -182,3 +183,50 @@ def private_query(
182183
return url
183184
except reearthcmsapi.ApiException as e:
184185
print("Exception when calling ItemsApi->item_filter: %s\n" % e)
186+
187+
# upload file to the reearth project using reearth-cms-api
188+
def upload_to_reearth(
189+
endpoint: str = None,
190+
access_token: str = None,
191+
project: str = None,
192+
filepath: str = None,
193+
):
194+
"""Re:EarthのプライベートAPIを利用してプロジェクトにファイルをアップロードする
195+
196+
Parameters
197+
----------
198+
endpoint : str
199+
APIのエンドポイント
200+
access_token : str
201+
APIのアクセストークン
202+
project : str
203+
プロジェクトのID
204+
filepath : str
205+
ローカルコンピュータ上のファイルパス
206+
207+
Returns
208+
-------
209+
boolean
210+
アップロードの成否
211+
"""
212+
configuration = reearthcmsapi.Configuration(
213+
host = endpoint,
214+
access_token = access_token
215+
)
216+
with reearthcmsapi.ApiClient(configuration) as api_client:
217+
api_instance = assets_project_api.AssetsProjectApi(api_client)
218+
path_params = {
219+
'projectId': project,
220+
}
221+
body = dict(
222+
file=open(filepath, 'rb'),
223+
skip_decompression=False,
224+
)
225+
try:
226+
api_response = api_instance.asset_create(
227+
path_params=path_params,
228+
body=body,
229+
)
230+
return True
231+
except reearthcmsapi.ApiException as e:
232+
return False

plateauutils/citygmlfinder/tests/test_from_reearth_cms.py

+64
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,67 @@ def test_private_query_fail_on_second():
198198
city_name="西東京市",
199199
)
200200
assert result is None
201+
202+
@httpretty.activate(verbose=True, allow_net_connect=False)
203+
def test_asset_upload():
204+
from plateauutils.citygmlfinder.from_reearth_cms import upload_to_reearth
205+
206+
data = """{
207+
"archiveExtractionStatus": "done",
208+
"contentType": "text/plain; charset=utf-8",
209+
"createdAt": "2023-09-29T08:58:10.712Z",
210+
"file": {
211+
"contentType": "text/plain; charset=utf-8",
212+
"name": "sample.txt",
213+
"path": "/sample.txt",
214+
"size": 13
215+
},
216+
"id": "01hbg2hrwr1qvx6prdxsf44x1z",
217+
"name": "sample.txt",
218+
"previewType": "unknown",
219+
"projectId": "01h2f43g1w67as5meqvbh4xas6",
220+
"totalSize": 13,
221+
"updatedAt": "0001-01-01T00:00:00Z",
222+
"url": "http://localhost:8080/assets/assets/6b/665c61-9168-4a30-b9c4-5ee88be7f71a/sample.txt"
223+
}"""
224+
httpretty.register_uri(
225+
httpretty.POST,
226+
"http://localhost:8081/api/projects/dummy/assets",
227+
body=data,
228+
content_type="application/json",
229+
)
230+
result = upload_to_reearth(
231+
endpoint="http://localhost:8081/api",
232+
access_token="dummy",
233+
project="dummy",
234+
filepath="./plateauutils/citygmlfinder/tests/data1.json"
235+
)
236+
assert (
237+
result
238+
== True
239+
)
240+
241+
@httpretty.activate(verbose=True, allow_net_connect=False)
242+
def test_asset_upload_fail():
243+
from plateauutils.citygmlfinder.from_reearth_cms import upload_to_reearth
244+
245+
data = """{
246+
"error": "operation denied"
247+
}"""
248+
httpretty.register_uri(
249+
httpretty.POST,
250+
"http://localhost:8081/api/projects/dummy/assets",
251+
status=400,
252+
body=data,
253+
content_type="application/json",
254+
)
255+
result = upload_to_reearth(
256+
endpoint="http://localhost:8081/api",
257+
access_token="dummy",
258+
project="dummy",
259+
filepath="./plateauutils/citygmlfinder/tests/data1.json"
260+
)
261+
assert (
262+
result
263+
== False
264+
)

0 commit comments

Comments
 (0)