Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from dart:html to package:http #194

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 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 All @@ -33,7 +31,7 @@ const String tzDataDefaultPath =

/// Initialize Time Zone database.
///
/// Throws [TimeZoneInitException] when something is worng.
/// Throws [TimeZoneInitException] when something is wrong.
///
/// ```dart
/// import 'package:timezone/browser.dart';
Expand All @@ -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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely needs a version bump and a changelog entry, but @srawlins can comment there

sdk: '>=2.19.0 <3.0.0'
dependencies:
http: ^1.2.1
path: ^1.8.0
dev_dependencies:
args: any
Expand Down