Skip to content

Commit

Permalink
Migrate from dart:html to package:http (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlapides authored Nov 5, 2024
1 parent 6761199 commit 453815f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
/// });
library timezone.browser;
import 'dart:html';
import 'dart:typed_data';
import 'package:http/browser_client.dart' as browser;
import 'package:timezone/timezone.dart';
export 'package:timezone/timezone.dart'
Expand Down Expand Up @@ -43,21 +41,25 @@ const String tzDataDefaultPath =
/// final detroitNow = TZDateTime.now(detroit);
/// });
/// ```
Future<void> initializeTimeZone([String path = tzDataDefaultPath]) {
return HttpRequest.request(path,
method: 'GET',
responseType: 'arraybuffer',
mimeType: 'application/octet-stream')
.then((req) {
final response = req.response;
if (response is ByteBuffer) {
initializeDatabase(response.asUint8List());
Future<void> initializeTimeZone([String path = tzDataDefaultPath]) async {
final client = browser.BrowserClient();
try {
final response = await client.get(
Uri.parse(path),
headers: {'Accept': 'application/octet-stream'},
);
if (response.statusCode == 200) {
initializeDatabase(response.bodyBytes);
} else {
throw TimeZoneInitException(
'Invalid response type: ${response.runtimeType}');
'Request failed with status: ${response.statusCode}',
);
}
}).catchError((dynamic e) {
} on TimeZoneInitException {
rethrow;
} on Exception catch (e) {
throw TimeZoneInitException(e.toString());
}, test: (e) => e is! TimeZoneInitException);
} finally {
client.close();
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ repository: https://github.com/srawlins/timezone
environment:
sdk: '>=2.19.0 <3.0.0'
dependencies:
http: ^1.2.1
path: ^1.8.0
dev_dependencies:
args: any
Expand Down

0 comments on commit 453815f

Please sign in to comment.