galnet 2.0.0
galnet: ^2.0.0 copied to clipboard
A Dart client for the Elite Dangerous Galnet API. Fetch articles with pagination, locale support, and full type safety.
example/galnet.dart
import 'package:galnet/galnet.dart';
import 'package:galnet/src/article.dart';
/// Main entry point for the Galnet API example.
/// Demonstrates various ways to fetch and process articles from the Elite Dangerous Galnet API.
void main() async {
/// Create a new GalnetClient instance with a user agent and default locale.
final GalnetClient client = GalnetClient(
userAgent: 'GalnetExampleApp/1.0.0',
locale: GalnetLocales.enGB,
);
try {
/// Example 1: Fetch a single page of the latest articles.
print('Fetching latest Galnet articles...');
final ResponsePage response = await client.fetch();
print('Found ${response.articles.length} articles:');
for (final Article article in response.articles) {
print('- ${article.title} (${article.galnetDate.toIso8601String()})');
}
/// Example 2: Stream ALL articles with automatic pagination.
/// Note: Limited to 10 articles for demonstration purposes.
print('\nFetching ALL articles (streaming):');
int count = 0;
await for (final Article article in client.fetchAll()) {
count++;
print('$count. ${article.title}');
if (count >= 10) break;
}
/// Example 3: Fetch articles filtered by publication date.
/// Uses RequestParameters to filter, sort, and paginate results.
print('\nFetching articles published after a specific date:');
final RequestParameters params = RequestParameters()
..filterGreaterThanOrEqual(
'published_at',
DateTime(2025, 1, 1).toIso8601String(),
)
..sortBy('-published_at')
..paginate(limit: 5);
final ResponsePage filteredResponse = await client.fetch(
requestParameters: params,
);
for (final Article article in filteredResponse.articles) {
print('- ${article.title} (Published: ${article.galnetDate})');
}
/// Example 4: Fetch articles in a different locale (French).
print('\nFetching articles in French:');
final GalnetClient frClient = GalnetClient.frFR(
userAgent: 'GalnetExampleApp/1.0.0',
);
final ResponsePage frResponse = await frClient.fetch();
for (final Article article in frResponse.articles) {
print('- ${article.title}');
}
frClient.close();
} on GalnetApiException catch (e) {
/// Handle API errors (4xx, 5xx status codes).
print('API Error [${e.statusCode}]: ${e.message}');
} on GalnetTimeoutException catch (e) {
/// Handle request timeout errors.
print('Request timed out: ${e.message}');
} on GalnetDataException catch (e) {
/// Handle invalid or missing data in the response.
print('Data error: ${e.message}');
} finally {
/// Always close the client to release network resources.
client.close();
}
}