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

Require an expected status code for every request #225

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
79 changes: 49 additions & 30 deletions lib/src/common/activity_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class ActivityService extends Service {

/// Marks all notifications up to [lastRead] as read.
///
/// API docs: https://developer.github.com/v3/activity/notifications/#mark-as-read
/// API docs: https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read
Future<bool> markNotificationsRead({DateTime lastRead}) {
final data = {};

Expand All @@ -158,7 +158,9 @@ class ActivityService extends Service {
}

return github
.request('PUT', '/notifications', body: GitHubJson.encode(data))
.request('PUT', '/notifications',
body: GitHubJson.encode(data),
statusCode: StatusCodes.RESET_CONTENT)
.then((response) {
return response.statusCode == 205;
});
Expand All @@ -180,7 +182,8 @@ class ActivityService extends Service {

return github
.request('PUT', '/repos/${slug.fullName}/notifications',
body: GitHubJson.encode(data))
body: GitHubJson.encode(data),
statusCode: StatusCodes.RESET_CONTENT)
.then((response) {
return response.statusCode == 205;
});
Expand All @@ -198,7 +201,8 @@ class ActivityService extends Service {
/// API docs: https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read
Future<bool> markThreadRead(String threadId) {
return github
.request('PATCH', '/notifications/threads/$threadId')
.request('PATCH', '/notifications/threads/$threadId',
statusCode: StatusCodes.RESET_CONTENT)
.then((response) {
return response.statusCode == StatusCodes.RESET_CONTENT;
});
Expand Down Expand Up @@ -234,33 +238,42 @@ class ActivityService extends Service {

/// Checks if the currently authenticated user has starred the specified repository.
///
/// API docs: https://developer.github.com/v3/activity/starring/#check-if-you-are-starring-a-repository
Future<bool> isStarred(RepositorySlug slug) {
return github
.request('GET', '/user/starred/${slug.fullName}')
.then((response) {
return response.statusCode == 204;
});
/// API docs: https://developer.github.com/v3/activity/starring/#check-if-a-repository-is-starred-by-the-authenticated-user
Future<bool> isStarred(RepositorySlug slug) async {
try {
await github.request(
'GET',
'/user/starred/${slug.fullName}',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
} on NotFound {
return false;
}
}

/// Stars the specified repository for the currently authenticated user.
///
/// API docs: https://developer.github.com/v3/activity/starring/#star-a-repository
Future star(RepositorySlug slug) {
return github.request('PUT', '/user/starred/${slug.fullName}',
headers: {'Content-Length': '0'}).then((response) {
return null;
});
/// API docs: https://developer.github.com/v3/activity/starring/#star-a-repository-for-the-authenticated-user
Future<void> star(RepositorySlug slug) async {
await github.request(
'PUT',
'/user/starred/${slug.fullName}',
headers: {'Content-Length': '0'},
statusCode: StatusCodes.NO_CONTENT,
);
}

/// Unstars the specified repository for the currently authenticated user.
///
/// API docs: https://developer.github.com/v3/activity/starring/#unstar-a-repository
Future unstar(RepositorySlug slug) {
return github.request('DELETE', '/user/starred/${slug.fullName}',
headers: {'Content-Length': '0'}).then((response) {
return null;
});
/// API docs: https://developer.github.com/v3/activity/starring/#unstar-a-repository-for-the-authenticated-user
Future<void> unstar(RepositorySlug slug) async {
await github.request(
'DELETE',
'/user/starred/${slug.fullName}',
headers: {'Content-Length': '0'},
statusCode: StatusCodes.NO_CONTENT,
);
}

/// Lists the watchers of the specified repository.
Expand Down Expand Up @@ -318,11 +331,13 @@ class ActivityService extends Service {
/// Deletes a Repository Subscription
///
/// API docs: https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription
Future deleteRepositorySubscription(RepositorySlug slug) {
return github.request('DELETE', '/repos/${slug.fullName}/subscription',
headers: {'Content-Length': '0'}).then((response) {
return null;
});
Future<void> deleteRepositorySubscription(RepositorySlug slug) async {
await github.request(
'DELETE',
'/repos/${slug.fullName}/subscription',
headers: {'Content-Length': '0'},
statusCode: StatusCodes.NO_CONTENT,
);
}
}

Expand Down Expand Up @@ -385,7 +400,9 @@ class EventPoller {
headers['If-None-Match'] = _lastFetched;
}

github.request('GET', path, headers: headers).then(handleEvent);
github
.request('GET', path, headers: headers, statusCode: null)
.then(handleEvent);
});
}

Expand All @@ -395,7 +412,9 @@ class EventPoller {
headers['If-None-Match'] = _lastFetched;
}

github.request('GET', path, headers: headers).then(handleEvent);
github
.request('GET', path, headers: headers, statusCode: null)
.then(handleEvent);

return _controller.stream;
}
Expand Down
57 changes: 38 additions & 19 deletions lib/src/common/gists_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ class GistsService extends Service {
/// Deletes the specified Gist.
///
/// API docs: https://developer.github.com/v3/gists/#delete-a-gist
Future<bool> deleteGist(String id) {
return github.request('DELETE', '/gists/$id').then((response) {
return response.statusCode == 204;
});
Future<bool> deleteGist(String id) async {
await github.request(
'DELETE',
'/gists/$id',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
}

/// Edits a Gist.
Expand Down Expand Up @@ -125,28 +128,41 @@ class GistsService extends Service {
/// Star the specified Gist.
///
/// API docs: https://developer.github.com/v3/gists/#star-a-gist
Future<bool> starGist(String id) {
return github.request('POST', '/gists/$id/star').then((response) {
return response.statusCode == 204;
});
Future<bool> starGist(String id) async {
await github.request(
'POST',
'/gists/$id/star',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
}

/// Unstar the specified Gist.
///
/// API docs: https://developer.github.com/v3/gists/#star-a-gist
Future<bool> unstarGist(String id) {
return github.request('DELETE', '/gists/$id/star').then((response) {
return response.statusCode == 204;
});
Future<bool> unstarGist(String id) async {
await github.request(
'DELETE',
'/gists/$id/star',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
}

/// Checks if the specified Gist is starred.
///
/// API docs: https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
Future<bool> isGistStarred(String id) {
return github.request('GET', '/gists/$id/star').then((response) {
return response.statusCode == 204;
});
Future<bool> isGistStarred(String id) async {
try {
await github.request(
'GET',
'/gists/$id/star',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
} on NotFound {
return false;
}
}

/// Forks the specified Gist.
Expand Down Expand Up @@ -176,9 +192,12 @@ class GistsService extends Service {
///
/// API docs: https://developer.github.com/v3/gists/comments/#create-a-comment
Future<GistComment> createComment(String gistId, CreateGistComment request) {
return github.postJSON('/gists/$gistId/comments',
body: GitHubJson.encode(request),
convert: (i) => GistComment.fromJson(i));
return github.postJSON(
'/gists/$gistId/comments',
body: GitHubJson.encode(request),
statusCode: StatusCodes.CREATED,
convert: (i) => GistComment.fromJson(i),
);
}

// TODO: Implement editComment: https://developer.github.com/v3/gists/comments/#edit-a-comment
Expand Down
26 changes: 16 additions & 10 deletions lib/src/common/git_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,27 @@ class GitService extends Service {
final headers = {'content-length': body.length.toString()};

return github
.request('PATCH', '/repos/${slug.fullName}/git/refs/$ref',
body: body, headers: headers)
.then((response) {
return GitReference.fromJson(
jsonDecode(response.body) as Map<String, dynamic>);
});
.request(
'PATCH',
'/repos/${slug.fullName}/git/refs/$ref',
body: body,
headers: headers,
statusCode: StatusCodes.OK,
)
.then((response) => GitReference.fromJson(
jsonDecode(response.body) as Map<String, dynamic>));
}

/// Deletes a reference.
///
/// API docs: https://developer.github.com/v3/git/refs/#delete-a-reference
Future<bool> deleteReference(RepositorySlug slug, String ref) {
return github
.request('DELETE', '/repos/${slug.fullName}/git/refs/$ref')
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
Future<bool> deleteReference(RepositorySlug slug, String ref) async {
await github.request(
'DELETE',
'/repos/${slug.fullName}/git/refs/$ref',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
}

/// Fetches a tag from the repo given a SHA.
Expand Down
10 changes: 5 additions & 5 deletions lib/src/common/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class GitHub {
/// The default [convert] function returns the input object.
Future<T> getJSON<S, T>(
String path, {
int statusCode,
@required int statusCode,
void Function(http.Response response) fail,
Map<String, String> headers,
Map<String, String> params,
Expand Down Expand Up @@ -183,7 +183,7 @@ class GitHub {
/// [T] represents the type return from this function after conversion
Future<T> postJSON<S, T>(
String path, {
int statusCode,
@required int statusCode,
void Function(http.Response response) fail,
Map<String, String> headers,
Map<String, dynamic> params,
Expand Down Expand Up @@ -225,7 +225,7 @@ class GitHub {
/// [T] represents the type return from this function after conversion
Future<T> putJSON<S, T>(
String path, {
int statusCode,
@required int statusCode,
void Function(http.Response response) fail,
Map<String, String> headers,
Map<String, dynamic> params,
Expand All @@ -248,7 +248,7 @@ class GitHub {
Future<T> requestJson<S, T>(
String method,
String path, {
int statusCode,
@required int statusCode,
void Function(http.Response response) fail,
Map<String, String> headers,
Map<String, dynamic> params,
Expand Down Expand Up @@ -298,10 +298,10 @@ class GitHub {
Future<http.Response> request(
String method,
String path, {
@required int statusCode,
Map<String, String> headers,
Map<String, dynamic> params,
dynamic body,
int statusCode,
void Function(http.Response response) fail,
String preview,
}) async {
Expand Down
Loading