Skip to content

Commit

Permalink
Abstract out where we get the API details
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Oct 22, 2024
1 parent 0088cd0 commit 8bfa0b2
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 115 deletions.
116 changes: 2 additions & 114 deletions generator/lib/googleapis_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import 'dart:convert';
import 'dart:io';

import 'package:discoveryapis_generator/discoveryapis_generator.dart';
import 'package:http/io_client.dart';
import 'package:io/ansi.dart' as ansi;
import 'package:pool/pool.dart';

import 'src/fetch_core.dart';
import 'src/package_configuration.dart';
import 'src/utils.dart';

Future<List<RestDescription>> downloadDiscoveryDocuments(
String outputDir,
) async {
final apis = await fetchDiscoveryDocuments();
final apis = await const FetchCore.service().fetchDiscoveryDocuments();
writeDiscoveryDocuments(outputDir, apis);
return apis;
}
Expand All @@ -42,116 +40,6 @@ void writeDiscoveryDocuments(
}
}

Future<List<RestDescription>> fetchDiscoveryDocuments({
Map<String, String>? existingRevisions,
Map<String, String>? additionalEntries,
}) async {
final client = IOClient();

Future<RestDescription?> download(DirectoryListItems item) async {
try {
final result = await client.get(
Uri.parse(item.discoveryRestUrl!),
headers: requestHeaders,
);

if (result.statusCode != 200) {
throw StateError(
'Not a 200 – ${result.statusCode}\n${result.body}',
);
}

final description = RestDescription.fromJson(
jsonDecode(result.body) as Map<String, dynamic>,
);

// Sort members here for stability in the output!
description.sort();

return description;
} catch (e, stack) {
print(
ansi.red.wrap(
'''
${item.discoveryRestUrl}
Failed to retrieve document for "${item.name}:${item.version}" -> Ignoring!
$e
$stack
''',
),
);
return null;
}
}

try {
final directoryList = await DiscoveryApi(client).apis.list();
final list = directoryList.items!;

if (additionalEntries != null) {
for (var entry in additionalEntries.entries) {
list.add(DirectoryListItems(
id: entry.key,
discoveryRestUrl: entry.value,
));
}
}

final pool = Pool(10);
try {
var count = 0;
return await pool
.forEach(list, (DirectoryListItems item) async {
print(ansi.darkGray.wrap(
'Requesting ${++count} of ${list.length} - ${item.id}',
));

RestDescription? description;
for (var i = 1; i <= 10; i++) {
description = await download(item);
if (i > 1) {
print(' ${item.id} try #$i');
}

final existingRevision = existingRevisions![description?.id!];
if (existingRevision != null &&
existingRevision != description!.revision) {
final compare =
existingRevision.compareTo(description.revision!);

if (compare.isNegative) {
print(
' New! ${description.id} '
'from $existingRevision to ${description.revision}',
);
} else {
final tryAgainLag = i > 5 ? 5 : i;
print(
' Old revision for ${description.id} '
'from $existingRevision to ${description.revision}.\n'
' Trying again in $tryAgainLag second(s) – '
'${item.discoveryRestUrl}',
);
await Future<void>.delayed(Duration(seconds: tryAgainLag));
continue;
}
}

return description;
}
return description;
})
.where((rd) => rd != null)
.cast<RestDescription>()
.toList();
} finally {
await pool.close();
}
} finally {
client.close();
}
}

List<RestDescription> loadDiscoveryDocuments(String directory) =>
Directory(directory)
.listSync()
Expand Down
14 changes: 14 additions & 0 deletions generator/lib/src/fetch_core.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:discoveryapis_generator/discoveryapis_generator.dart';

import 'fetch_service.dart';

abstract class FetchCore {
const FetchCore();

const factory FetchCore.service() = FetchService;

Future<List<RestDescription>> fetchDiscoveryDocuments({
Map<String, String>? existingRevisions,
Map<String, String>? additionalEntries,
});
}
125 changes: 125 additions & 0 deletions generator/lib/src/fetch_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import 'dart:async';
import 'dart:convert';

import 'package:discoveryapis_generator/discoveryapis_generator.dart';
import 'package:http/io_client.dart';
import 'package:io/ansi.dart' as ansi;
import 'package:pool/pool.dart';

import 'fetch_core.dart';
import 'utils.dart';

class FetchService extends FetchCore {
const FetchService();

@override
Future<List<RestDescription>> fetchDiscoveryDocuments({
Map<String, String>? existingRevisions,
Map<String, String>? additionalEntries,
}) async {
final client = IOClient();

Future<RestDescription?> download(DirectoryListItems item) async {
try {
final result = await client.get(
Uri.parse(item.discoveryRestUrl!),
headers: requestHeaders,
);

if (result.statusCode != 200) {
throw StateError(
'Not a 200 – ${result.statusCode}\n${result.body}',
);
}

final description = RestDescription.fromJson(
jsonDecode(result.body) as Map<String, dynamic>,
);

// Sort members here for stability in the output!
description.sort();

return description;
} catch (e, stack) {
print(
ansi.red.wrap(
'''
${item.discoveryRestUrl}
Failed to retrieve document for "${item.name}:${item.version}" -> Ignoring!
$e
$stack
''',
),
);
return null;
}
}

try {
final directoryList = await DiscoveryApi(client).apis.list();
final list = directoryList.items!;

if (additionalEntries != null) {
for (var entry in additionalEntries.entries) {
list.add(DirectoryListItems(
id: entry.key,
discoveryRestUrl: entry.value,
));
}
}

final pool = Pool(10);
try {
var count = 0;
return await pool
.forEach(list, (DirectoryListItems item) async {
print(ansi.darkGray.wrap(
'Requesting ${++count} of ${list.length} - ${item.id}',
));

RestDescription? description;
for (var i = 1; i <= 10; i++) {
description = await download(item);
if (i > 1) {
print(' ${item.id} try #$i');
}

final existingRevision = existingRevisions![description?.id!];
if (existingRevision != null &&
existingRevision != description!.revision) {
final compare =
existingRevision.compareTo(description.revision!);

if (compare.isNegative) {
print(
' New! ${description.id} '
'from $existingRevision to ${description.revision}',
);
} else {
final tryAgainLag = i > 5 ? 5 : i;
print(
' Old revision for ${description.id} '
'from $existingRevision to ${description.revision}.\n'
' Trying again in $tryAgainLag second(s) – '
'${item.discoveryRestUrl}',
);
await Future<void>.delayed(Duration(seconds: tryAgainLag));
continue;
}
}

return description;
}
return description;
})
.where((rd) => rd != null)
.cast<RestDescription>()
.toList();
} finally {
await pool.close();
}
} finally {
client.close();
}
}
}
3 changes: 2 additions & 1 deletion generator/lib/src/package_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:discoveryapis_generator/src/namer.dart';
import 'package:yaml/yaml.dart';

import '../googleapis_generator.dart';
import 'fetch_core.dart';

class Package {
final String name;
Expand Down Expand Up @@ -117,7 +118,7 @@ class DiscoveryPackagesConfiguration {
}

// Get all rest discovery documents & initialize this object.
final allApis = await fetchDiscoveryDocuments(
final allApis = await const FetchCore.service().fetchDiscoveryDocuments(
existingRevisions: existingApiRevisions,
additionalEntries: additionalEntries,
);
Expand Down

0 comments on commit 8bfa0b2

Please sign in to comment.