Skip to content

Commit

Permalink
CesiumIonClient and CesiumIonAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
nmfisher committed Sep 9, 2024
1 parent 3cffa75 commit 8c14de7
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/cesium_3d_native.dart
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions lib/src/cesium_3d_native_base.dart
Original file line number Diff line number Diff line change
@@ -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;
}
64 changes: 64 additions & 0 deletions lib/src/ion/CesiumIonAsset.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> 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<String, dynamic> 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)';
}
}
78 changes: 78 additions & 0 deletions lib/src/ion/CesiumIonClient.dart
Original file line number Diff line number Diff line change
@@ -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<List<CesiumIonAsset>> listAssets() async {
final response = await http.get(
Uri.parse('$baseUrl/assets'),
headers: {'Authorization': 'Bearer $accessToken'},
);

if (response.statusCode == 200) {
final Map<String, dynamic> 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<CesiumIonAsset> 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<CesiumIonAsset> createAsset(Map<String, dynamic> 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<void> 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 [email protected].');
} 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));
}
}
}
}

0 comments on commit 8c14de7

Please sign in to comment.