Skip to content

Commit ace235d

Browse files
committed
Implmeneted getUserById to call get-a-user-using-their-id github api
1 parent 2df0a2a commit ace235d

4 files changed

Lines changed: 145 additions & 4 deletions

File tree

lib/src/common/model/users.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class User {
7676
String? email;
7777

7878
/// If this user is hirable
79+
@JsonKey(name: 'hireable')
7980
bool? hirable;
8081

8182
/// The User's Biography

lib/src/common/model/users.g.dart

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/common/users_service.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class UsersService extends Service {
1616
Future<User> getUser(String? name) =>
1717
github.getJSON('/users/$name', convert: User.fromJson);
1818

19+
/// Fetches the user specified by [id].
20+
///
21+
/// API docs: https://docs.github.com/en/rest/users/users#get-a-user-using-their-id
22+
Future<User> getUserById(int id) =>
23+
github.getJSON('/user/$id', convert: User.fromJson);
24+
1925
/// Updates the Current User.
2026
///
2127
/// API docs: https://developer.github.com/v3/users/#update-the-authenticated-user

test/unit/users_service_test.dart

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import 'package:github/github.dart';
2+
import 'package:http/http.dart';
3+
import 'package:http/testing.dart';
4+
import 'package:test/test.dart';
5+
6+
void main() {
7+
group(UsersService, () {
8+
late GitHub github;
9+
late UsersService usersService;
10+
Request? lastRequest;
11+
12+
void setUpGitHub(Future<Response> Function(Request) handler) {
13+
final client = MockClient((request) async {
14+
lastRequest = request;
15+
return handler(request);
16+
});
17+
github = GitHub(client: client);
18+
usersService = UsersService(github);
19+
}
20+
21+
tearDown(() {
22+
lastRequest = null;
23+
});
24+
25+
test('getUserById calls correct endpoint and parses response', () async {
26+
const userResponse = '''
27+
{
28+
"login": "octocat",
29+
"id": 1,
30+
"node_id": "MDQ6VXNlcjE=",
31+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
32+
"gravatar_id": "",
33+
"url": "https://api.github.com/users/octocat",
34+
"html_url": "https://github.com/octocat",
35+
"followers_url": "https://api.github.com/users/octocat/followers",
36+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
37+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
38+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
39+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
40+
"organizations_url": "https://api.github.com/users/octocat/orgs",
41+
"repos_url": "https://api.github.com/users/octocat/repos",
42+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
43+
"received_events_url": "https://api.github.com/users/octocat/received_events",
44+
"type": "User",
45+
"site_admin": false,
46+
"name": "monalisa octocat",
47+
"company": "GitHub",
48+
"blog": "https://github.com/blog",
49+
"location": "San Francisco",
50+
"email": "octocat@github.com",
51+
"hireable": false,
52+
"bio": "There once was...",
53+
"twitter_username": "monatheoctocat",
54+
"public_repos": 2,
55+
"public_gists": 1,
56+
"followers": 20,
57+
"following": 0,
58+
"created_at": "2008-01-14T04:33:35Z",
59+
"updated_at": "2008-01-14T04:33:35Z"
60+
}
61+
''';
62+
63+
setUpGitHub((request) async {
64+
return Response(userResponse, 200);
65+
});
66+
67+
final user = await usersService.getUserById(1);
68+
69+
expect(user.login, 'octocat');
70+
expect(user.id, 1);
71+
expect(user.nodeId, 'MDQ6VXNlcjE=');
72+
expect(
73+
user.avatarUrl,
74+
'https://github.com/images/error/octocat_happy.gif',
75+
);
76+
expect(user.gravatarId, '');
77+
expect(user.url, 'https://api.github.com/users/octocat');
78+
expect(user.htmlUrl, 'https://github.com/octocat');
79+
expect(
80+
user.followersUrl,
81+
'https://api.github.com/users/octocat/followers',
82+
);
83+
expect(
84+
user.followingUrl,
85+
'https://api.github.com/users/octocat/following{/other_user}',
86+
);
87+
expect(
88+
user.gistsUrl,
89+
'https://api.github.com/users/octocat/gists{/gist_id}',
90+
);
91+
expect(
92+
user.starredUrl,
93+
'https://api.github.com/users/octocat/starred{/owner}{/repo}',
94+
);
95+
expect(
96+
user.subscriptionsUrl,
97+
'https://api.github.com/users/octocat/subscriptions',
98+
);
99+
expect(
100+
user.organizationsUrl,
101+
'https://api.github.com/users/octocat/orgs',
102+
);
103+
expect(user.reposUrl, 'https://api.github.com/users/octocat/repos');
104+
expect(
105+
user.eventsUrl,
106+
'https://api.github.com/users/octocat/events{/privacy}',
107+
);
108+
expect(
109+
user.receivedEventsUrl,
110+
'https://api.github.com/users/octocat/received_events',
111+
);
112+
expect(user.type, 'User');
113+
expect(user.siteAdmin, isFalse);
114+
expect(user.name, 'monalisa octocat');
115+
expect(user.company, 'GitHub');
116+
expect(user.blog, 'https://github.com/blog');
117+
expect(user.location, 'San Francisco');
118+
expect(user.email, 'octocat@github.com');
119+
expect(user.hirable, isFalse);
120+
expect(user.bio, 'There once was...');
121+
expect(user.twitterUsername, 'monatheoctocat');
122+
expect(user.publicReposCount, 2);
123+
expect(user.publicGistsCount, 1);
124+
expect(user.followersCount, 20);
125+
expect(user.followingCount, 0);
126+
expect(user.createdAt, DateTime.parse('2008-01-14T04:33:35Z'));
127+
expect(user.updatedAt, DateTime.parse('2008-01-14T04:33:35Z'));
128+
129+
expect(lastRequest, isNotNull);
130+
expect(lastRequest!.method, 'GET');
131+
expect(lastRequest!.url.path, '/user/1');
132+
});
133+
});
134+
}

0 commit comments

Comments
 (0)