diff --git a/lib/cesium_3d_native.dart b/lib/cesium_3d_native.dart new file mode 100644 index 0000000..6b1471b --- /dev/null +++ b/lib/cesium_3d_native.dart @@ -0,0 +1,8 @@ +/// Support for doing something awesome. +/// +/// More dartdocs go here. +library; + +export 'src/cesium_3d_native_base.dart'; + +// TODO: Export any libraries intended for clients of this package. diff --git a/lib/src/cesium_3d_native_base.dart b/lib/src/cesium_3d_native_base.dart new file mode 100644 index 0000000..e8a6f15 --- /dev/null +++ b/lib/src/cesium_3d_native_base.dart @@ -0,0 +1,6 @@ +// TODO: Put public facing types in this file. + +/// Checks if you are awesome. Spoiler: you are. +class Awesome { + bool get isAwesome => true; +} diff --git a/lib/src/ion/CesiumIonAsset.dart b/lib/src/ion/CesiumIonAsset.dart new file mode 100644 index 0000000..07d3415 --- /dev/null +++ b/lib/src/ion/CesiumIonAsset.dart @@ -0,0 +1,64 @@ +class CesiumIonAsset { + final int id; + final String type; + final String name; + final String description; + final int bytes; + final String attribution; + final DateTime dateAdded; + final bool exportable; + final String status; + final int percentComplete; + final bool archivable; + + CesiumIonAsset({ + required this.id, + required this.type, + required this.name, + required this.description, + required this.bytes, + required this.attribution, + required this.dateAdded, + required this.exportable, + required this.status, + required this.percentComplete, + required this.archivable, + }); + + factory CesiumIonAsset.fromJson(Map json) { + return CesiumIonAsset( + id: json['id'], + type: json['type'], + name: json['name'], + description: json['description'], + bytes: json['bytes'], + attribution: json['attribution'], + dateAdded: DateTime.parse(json['dateAdded']), + exportable: json['exportable'], + status: json['status'], + percentComplete: json['percentComplete'], + archivable: json['archivable'], + ); + } + + Map toJson() { + return { + 'id': id, + 'type': type, + 'name': name, + 'description': description, + 'bytes': bytes, + 'attribution': attribution, + 'dateAdded': dateAdded.toIso8601String(), + 'exportable': exportable, + 'status': status, + 'percentComplete': percentComplete, + 'archivable': archivable, + }; + } + + @override + String toString() { + return 'CesiumIonAsset(id: $id, type: $type, name: $name, status: $status, percentComplete: $percentComplete)'; + } +} \ No newline at end of file diff --git a/lib/src/ion/CesiumIonClient.dart b/lib/src/ion/CesiumIonClient.dart new file mode 100644 index 0000000..682cb56 --- /dev/null +++ b/lib/src/ion/CesiumIonClient.dart @@ -0,0 +1,78 @@ +import 'dart:convert'; +import 'package:cesium_3d_native/src/ion/CesiumIonAsset.dart'; +import 'package:http/http.dart' as http; + +class CesiumIonClient { + final String accessToken; + final String baseUrl = 'https://api.cesium.com/v1'; + + CesiumIonClient(this.accessToken); + + Future> listAssets() async { + final response = await http.get( + Uri.parse('$baseUrl/assets'), + headers: {'Authorization': 'Bearer $accessToken'}, + ); + + if (response.statusCode == 200) { + final Map data = json.decode(response.body); + return (data['items'] as List) + .map((item) => CesiumIonAsset.fromJson(item)) + .toList(); + } else { + throw Exception('Failed to load assets: ${response.statusCode}'); + } + } + + Future getAsset(int assetId) async { + final response = await http.get( + Uri.parse('$baseUrl/assets/$assetId'), + headers: {'Authorization': 'Bearer $accessToken'}, + ); + + if (response.statusCode == 200) { + return CesiumIonAsset.fromJson(json.decode(response.body)); + } else { + throw Exception('Failed to fetch asset: ${response.statusCode}'); + } + } + + Future createAsset(Map assetData) async { + final response = await http.post( + Uri.parse('$baseUrl/assets'), + headers: { + 'Authorization': 'Bearer $accessToken', + 'Content-Type': 'application/json', + }, + body: json.encode(assetData), + ); + + if (response.statusCode == 200) { + return CesiumIonAsset.fromJson(json.decode(response.body)['assetMetadata']); + } else { + throw Exception('Failed to create asset: ${response.statusCode}'); + } + } + + Future waitUntilReady(int assetId) async { + while (true) { + final asset = await getAsset(assetId); + if (asset.status == 'COMPLETE') { + print('Asset tiled successfully'); + print('View in ion: https://ion.cesium.com/assets/${asset.id}'); + break; + } else if (asset.status == 'DATA_ERROR') { + throw Exception('ion detected a problem with the uploaded data.'); + } else if (asset.status == 'ERROR') { + throw Exception('An unknown tiling error occurred, please contact support@cesium.com.'); + } else { + if (asset.status == 'NOT_STARTED') { + print('Tiling pipeline initializing.'); + } else { // IN_PROGRESS + print('Asset is ${asset.percentComplete}% complete.'); + } + await Future.delayed(Duration(seconds: 10)); + } + } + } +} \ No newline at end of file