Skip to content

Commit 92d97e2

Browse files
committed
Add Status Code Constants
1 parent 18da86f commit 92d97e2

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

lib/src/common/github.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class GitHub {
6262
* Checks if a user exists.
6363
*/
6464
Future<bool> userExists(String name) =>
65-
request("GET", "/users/${name}").then((resp) => resp.statusCode == 200);
65+
request("GET", "/users/${name}").then((resp) => resp.statusCode == StatusCodes.OK);
6666

6767
/**
6868
* Fetches the users specified by [names].
@@ -92,7 +92,7 @@ class GitHub {
9292
* Fetches the repository specified by the [slug].
9393
*/
9494
Future<Repository> repository(RepositorySlug slug) {
95-
return getJSON("/repos/${slug.owner}/${slug.name}", convert: Repository.fromJSON, statusCode: 200, fail: (http.Response response) {
95+
return getJSON("/repos/${slug.owner}/${slug.name}", convert: Repository.fromJSON, statusCode: StatusCodes.OK, fail: (http.Response response) {
9696
if (response.statusCode == 404) {
9797
throw new RepositoryNotFound(this, slug.fullName);
9898
}
@@ -140,7 +140,7 @@ class GitHub {
140140
* Fetches the organization specified by [name].
141141
*/
142142
Future<Organization> organization(String name) {
143-
return getJSON("/orgs/${name}", convert: Organization.fromJSON, statusCode: 200, fail: (http.Response response) {
143+
return getJSON("/orgs/${name}", convert: Organization.fromJSON, statusCode: StatusCodes.OK, fail: (http.Response response) {
144144
if (response.statusCode == 404) {
145145
throw new OrganizationNotFound(this, name);
146146
}
@@ -300,8 +300,8 @@ class GitHub {
300300
* Throws [AccessForbidden] if we are not authenticated.
301301
*/
302302
Future<CurrentUser> currentUser() {
303-
return getJSON("/user", statusCode: 200, fail: (http.Response response) {
304-
if (response.statusCode == 403) {
303+
return getJSON("/user", statusCode: StatusCodes.OK, fail: (http.Response response) {
304+
if (response.statusCode == StatusCodes.FORBIDDEN) {
305305
throw new AccessForbidden(this);
306306
}
307307
}, convert: CurrentUser.fromJSON);
@@ -343,7 +343,7 @@ class GitHub {
343343
* Fetches a Gist by the specified [id].
344344
*/
345345
Future<Gist> gist(String id) {
346-
return getJSON("/gist/${id}", statusCode: 200, convert: Gist.fromJSON);
346+
return getJSON("/gist/${id}", statusCode: StatusCodes.OK, convert: Gist.fromJSON);
347347
}
348348

349349
Stream<BlogPost> blogPosts([String url = "https://github.com/blog.atom"]) => _blogPosts(url);
@@ -409,7 +409,7 @@ class GitHub {
409409
* Fetches a Single Notification
410410
*/
411411
Future<Notification> notification(String id) {
412-
return getJSON("/notification/threads/${id}", statusCode: 200, convert: Notification.fromJSON);
412+
return getJSON("/notification/threads/${id}", statusCode: StatusCodes.OK, convert: Notification.fromJSON);
413413
}
414414

415415
/**
@@ -424,7 +424,7 @@ class GitHub {
424424
* Fetches repository subscription information.
425425
*/
426426
Future<RepositorySubscription> subscription(RepositorySlug slug) {
427-
return getJSON("/repos/${slug.fullName}/subscription", statusCode: 200, convert: RepositorySubscription.fromJSON);
427+
return getJSON("/repos/${slug.fullName}/subscription", statusCode: StatusCodes.OK, convert: RepositorySubscription.fromJSON);
428428
}
429429

430430
Stream<PublicKey> publicKeys([String user]) {
@@ -570,7 +570,7 @@ class GitHub {
570570
* Returns a map of the name to a url of the image.
571571
*/
572572
Future<Map<String, String>> emojis() {
573-
return getJSON("/emojis", statusCode: 200);
573+
return getJSON("/emojis", statusCode: StatusCodes.OK);
574574
}
575575

576576
/**
@@ -625,15 +625,15 @@ class GitHub {
625625
* Gets a language breakdown for the specified repository.
626626
*/
627627
Future<LanguageBreakdown> languages(RepositorySlug slug) =>
628-
getJSON("/repos/${slug.fullName}/languages", statusCode: 200, convert: (github, input) => new LanguageBreakdown(input));
628+
getJSON("/repos/${slug.fullName}/languages", statusCode: StatusCodes.OK, convert: (github, input) => new LanguageBreakdown(input));
629629

630630
/**
631631
* Gets the readme file for a repository.
632632
*/
633633
Future<File> readme(RepositorySlug slug) {
634634
var headers = {};
635635

636-
return getJSON("/repos/${slug.fullName}/readme", headers: headers, statusCode: 200, fail: (http.Response response) {
636+
return getJSON("/repos/${slug.fullName}/readme", headers: headers, statusCode: StatusCodes.OK, fail: (http.Response response) {
637637
if (response.statusCode == 404) {
638638
throw new NotFound(this, response.body);
639639
}
@@ -677,7 +677,7 @@ class GitHub {
677677
* Gets the GitHub API Status.
678678
*/
679679
Future<APIStatus> apiStatus() {
680-
return getJSON("https://status.github.com/api/status.json", statusCode: 200, convert: APIStatus.fromJSON);
680+
return getJSON("https://status.github.com/api/status.json", statusCode: StatusCodes.OK, convert: APIStatus.fromJSON);
681681
}
682682

683683
/**
@@ -737,7 +737,7 @@ class GitHub {
737737
}
738738

739739
Future<PullRequest> pullRequest(RepositorySlug slug, int number) {
740-
return getJSON("/repos/${slug.fullName}/pulls/${number}", convert: PullRequest.fromJSON, statusCode: 200);
740+
return getJSON("/repos/${slug.fullName}/pulls/${number}", convert: PullRequest.fromJSON, statusCode: StatusCodes.OK);
741741
}
742742

743743
/**

lib/src/common/util.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,36 @@ List<MapEntry<dynamic, dynamic>> mapToList(Map<dynamic, dynamic> input) {
117117
out.add(new MapEntry<dynamic, dynamic>(key, input[key]));
118118
}
119119
return out;
120+
}
121+
122+
abstract class StatusCodes {
123+
static const int OK = 200;
124+
static const int CREATED = 201;
125+
static const int ACCEPTED = 202;
126+
static const int NON_AUTHORITATIVE_INFO = 203;
127+
static const int NO_CONTENT = 204;
128+
static const int RESET_CONTENT = 205;
129+
static const int PARTIAL_CONTENT = 206;
130+
131+
static const int MOVED_PERMANENTLY = 301;
132+
static const int FOUND = 302;
133+
static const int NOT_MODIFIED = 304;
134+
static const int TEMPORARY_REDIRECT = 307;
135+
136+
static const int BAD_REQUEST = 400;
137+
static const int UNAUTHORIZED = 401;
138+
static const int PAYMENT_REQUIRED = 402;
139+
static const int FORBIDDEN = 403;
140+
static const int NOT_FOUND = 404;
141+
static const int METHOD_NOT_ALLOWED = 405;
142+
static const int NOT_ACCEPTABLE = 406;
143+
static const int PROXY_AUTHENTICATION_REQUIRED = 407;
144+
static const int REQUEST_TIMEOUT = 408;
145+
static const int CONFLICT = 409;
146+
static const int GONE = 410;
147+
static const int LENGTH_REQUIRED = 411;
148+
static const int PRECONDITION_FAILED = 412;
149+
static const int TOO_MANY_REQUESTS = 429;
150+
151+
static bool isClientError(int code) => code > 400 && code < 500;
120152
}

lib/src/http/client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ part of github.http;
22

33
abstract class Client {
44
Future<Response> request(Request request);
5-
}
5+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy