From 741c26d0142413b80b89049772a5e693e064ebfb Mon Sep 17 00:00:00 2001 From: Casey Hillers Date: Thu, 28 Dec 2023 09:26:23 -0800 Subject: [PATCH] Update MiscService.getApiStatus to v2 (#393) * v1 has been deleted Co-authored-by: Rob Becker --- CHANGELOG.md | 5 +++ lib/src/common/misc_service.dart | 4 +- lib/src/common/model/misc.dart | 62 ++++++++++++++++++++++++------ lib/src/common/model/misc.g.dart | 44 ++++++++++++++++----- pubspec.yaml | 2 +- test/common/misc_service_test.dart | 37 ++++++++++++++++++ 6 files changed, 131 insertions(+), 23 deletions(-) create mode 100644 test/common/misc_service_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 9acbade0..953f9937 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 9.21.0 + +* Update MiscService.getApiStatus() to use the v2 API + * `APIStatus` has been refactored to match, now exposing `page` and `status` + ## 9.20.0 * Add a Changes object to the PullRequestEvent object so we can see what changed in edited PR events by @ricardoamador in https://github.com/SpinlockLabs/github.dart/pull/390 diff --git a/lib/src/common/misc_service.dart b/lib/src/common/misc_service.dart index 4951848f..bb351f3d 100644 --- a/lib/src/common/misc_service.dart +++ b/lib/src/common/misc_service.dart @@ -68,8 +68,10 @@ class MiscService extends Service { } /// Gets the GitHub API Status. + /// + /// API docs: https://www.githubstatus.com/api Future getApiStatus() => - github.getJSON('https://status.github.com/api/status.json', + github.getJSON('https://status.github.com/api/v2/status.json', statusCode: StatusCodes.OK, convert: APIStatus.fromJson); /// Returns an ASCII Octocat with the specified [text]. diff --git a/lib/src/common/model/misc.dart b/lib/src/common/model/misc.dart index adf7b2be..18fc0a54 100644 --- a/lib/src/common/model/misc.dart +++ b/lib/src/common/model/misc.dart @@ -60,27 +60,65 @@ class RateLimit { Map toJson() => _$RateLimitToJson(this); } -/// Model class for the GitHub api status. +/// Model class for the GitHub API status. @JsonSerializable() class APIStatus { APIStatus({ + this.page, this.status, - this.lastUpdatedAt, - this.createdOn, - this.message, }); - final String? status; - @JsonKey(name: 'last_updated') - final DateTime? lastUpdatedAt; + /// Details about where to find more information. + final APIStatusPage? page; - @JsonKey(name: 'created_on') - final DateTime? createdOn; - - @JsonKey(name: 'body') - final String? message; + /// An overview of the current status. + final APIStatusMessage? status; factory APIStatus.fromJson(Map input) => _$APIStatusFromJson(input); Map toJson() => _$APIStatusToJson(this); } + +@JsonSerializable() +class APIStatusPage { + const APIStatusPage({ + this.id, + this.name, + this.url, + this.updatedAt, + }); + + /// Unique identifier for the current status. + final String? id; + + final String? name; + + /// Where to get more detailed information. + final String? url; + + @JsonKey(name: 'updated_at') + final DateTime? updatedAt; + + factory APIStatusPage.fromJson(Map input) => + _$APIStatusPageFromJson(input); + Map toJson() => _$APIStatusPageToJson(this); +} + +/// Overview class of the GitHub API status. +@JsonSerializable() +class APIStatusMessage { + const APIStatusMessage({ + this.description, + this.indicator, + }); + + /// A human description of the blended component status. + final String? description; + + /// An indicator - one of none, minor, major, or critical. + final String? indicator; + + factory APIStatusMessage.fromJson(Map input) => + _$APIStatusMessageFromJson(input); + Map toJson() => _$APIStatusMessageToJson(this); +} diff --git a/lib/src/common/model/misc.g.dart b/lib/src/common/model/misc.g.dart index 34362c50..d7b3896c 100644 --- a/lib/src/common/model/misc.g.dart +++ b/lib/src/common/model/misc.g.dart @@ -31,19 +31,45 @@ Map _$RateLimitToJson(RateLimit instance) => { }; APIStatus _$APIStatusFromJson(Map json) => APIStatus( - status: json['status'] as String?, - lastUpdatedAt: json['last_updated'] == null + page: json['page'] == null ? null - : DateTime.parse(json['last_updated'] as String), - createdOn: json['created_on'] == null + : APIStatusPage.fromJson(json['page'] as Map), + status: json['status'] == null ? null - : DateTime.parse(json['created_on'] as String), - message: json['body'] as String?, + : APIStatusMessage.fromJson(json['status'] as Map), ); Map _$APIStatusToJson(APIStatus instance) => { + 'page': instance.page, 'status': instance.status, - 'last_updated': instance.lastUpdatedAt?.toIso8601String(), - 'created_on': instance.createdOn?.toIso8601String(), - 'body': instance.message, + }; + +APIStatusPage _$APIStatusPageFromJson(Map json) => + APIStatusPage( + id: json['id'] as String?, + name: json['name'] as String?, + url: json['url'] as String?, + updatedAt: json['updated_at'] == null + ? null + : DateTime.parse(json['updated_at'] as String), + ); + +Map _$APIStatusPageToJson(APIStatusPage instance) => + { + 'id': instance.id, + 'name': instance.name, + 'url': instance.url, + 'updated_at': instance.updatedAt?.toIso8601String(), + }; + +APIStatusMessage _$APIStatusMessageFromJson(Map json) => + APIStatusMessage( + description: json['description'] as String?, + indicator: json['indicator'] as String?, + ); + +Map _$APIStatusMessageToJson(APIStatusMessage instance) => + { + 'description': instance.description, + 'indicator': instance.indicator, }; diff --git a/pubspec.yaml b/pubspec.yaml index 4fb206a9..0251b7c7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: github -version: 9.20.0 +version: 9.21.0 description: A high-level GitHub API Client Library that uses Github's v3 API homepage: https://github.com/SpinlockLabs/github.dart diff --git a/test/common/misc_service_test.dart b/test/common/misc_service_test.dart new file mode 100644 index 00000000..2066e271 --- /dev/null +++ b/test/common/misc_service_test.dart @@ -0,0 +1,37 @@ +import 'dart:io'; + +import 'package:github/src/common.dart'; +import 'package:http/http.dart'; +import 'package:http/testing.dart'; +import 'package:test/test.dart'; + +void main() { + MiscService create(Future Function(Request) f) { + final client = MockClient(f); + final github = GitHub(client: client); + return MiscService(github); + } + + test('api status', () async { + final miscService = create( + (_) async => Response(''' +{ + "page":{ + "id":"kctbh9vrtdwd", + "name":"GitHub", + "url":"https://www.githubstatus.com", + "updated_at": "2023-11-29T08:03:04Z" + }, + "status": { + "description": "Partial System Outage", + "indicator": "major" + } +}''', HttpStatus.ok), + ); + final status = await miscService.getApiStatus(); + expect(status.page, isNotNull); + expect(status.page?.id, 'kctbh9vrtdwd'); + expect(status.status, isNotNull); + expect(status.status?.indicator, 'major'); + }); +}