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

Add calendar versioning #338

Merged
merged 1 commit into from
Nov 30, 2022
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
14 changes: 14 additions & 0 deletions lib/src/common/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class GitHub {
GitHub({
Authentication? auth,
this.endpoint = 'https://api.github.com',
this.version = '2022-11-28',
http.Client? client,
}) : auth = auth ?? Authentication.anonymous(),
client = client ?? http.Client();
Expand All @@ -29,12 +30,24 @@ class GitHub {
static const _ratelimitResetHeader = 'x-ratelimit-reset';
static const _ratelimitRemainingHeader = 'x-ratelimit-remaining';

@visibleForTesting
static const versionHeader = 'X-GitHub-Api-Version';

/// Authentication Information
Authentication? auth;

/// API Endpoint
final String endpoint;

/// Calendar version of the GitHub API to use.
///
/// Changing this value is unsupported. However, it may unblock you if there's
/// hotfix versions.
///
/// See also:
/// * https://docs.github.com/en/rest/overview/api-versions?apiVersion=2022-11-28
final String version;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Most of this change was me debating if we should make this private. I lean towards making it public to give an easy escape hatch for downstream dependencies to override what the library is using.


/// HTTP Client
final http.Client client;

Expand Down Expand Up @@ -306,6 +319,7 @@ class GitHub {
}

headers.putIfAbsent('Accept', () => v3ApiMimeType);
headers.putIfAbsent(versionHeader, () => version);

final response = await request(
method,
Expand Down
26 changes: 26 additions & 0 deletions test/common/github_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:io';

import 'package:github/src/common/github.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:test/test.dart';

void main() {
group(GitHub, () {
test('passes calendar version header', () async {
Request? request;
final client = MockClient((r) async {
request = r;
return Response('{}', HttpStatus.ok);
});

final github = GitHub(client: client);
await github.getJSON(''); // Make HTTP request

expect(request, isNotNull);
expect(request!.headers.containsKey(GitHub.versionHeader), isTrue);
final version = request!.headers[GitHub.versionHeader];
expect(version, github.version);
});
});
}