-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
collections.dart
131 lines (113 loc) · 3.78 KB
/
collections.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import 'package:http/http.dart' as http;
import 'client.dart';
import 'model/model.dart';
import 'utils.dart';
/// Provides access to resources related to [Collection].
///
/// See: [Unsplash docs](https://unsplash.com/documentation#collections)
class Collections {
/// Creates a new instance which belongs to [client].
Collections(this.client) : baseUrl = client.baseUrl.resolve('collections/');
/// The parent [UnsplashClient].
final UnsplashClient client;
/// The base url for all endpoints for [Collections].
final Uri baseUrl;
/// List collections
///
/// Get a single page from the list of all collections.
///
/// ## List featured collections
/// Get a single page from the list of featured collections, by setting
/// [featured] to `true`.
///
/// See: [Collections](https://unsplash.com/documentation#list-collections)
/// [Featured Collections](https://unsplash.com/documentation#list-featured-collections)
Request<List<Collection>> list({
int? page,
int? perPage,
bool featured = false,
}) {
assert(page == null || page >= 0);
assert(perPage == null ||
perPage >= 0 && perPage <= client.settings.maxPageSize);
final params = queryParams({
'page': page,
'per_page': perPage,
});
final url = (featured ? baseUrl.resolve('featured') : baseUrl)
.replace(queryParameters: params);
return Request(
client: client,
httpRequest: http.Request('GET', url),
isPublicAction: true,
bodyDeserializer: _deserializeCollections,
);
}
/// Get a collection
///
/// Retrieve a single collection. To view a user’s private collections, the
/// read_collections scope is required.
///
/// Note: See the note on [hotlinking](https://unsplash.com/documentation#hotlinking).
///
/// See: [Unsplash docs](https://unsplash.com/documentation#get-a-collection)
Request<Collection> get(String id) {
final url = baseUrl.resolve(id);
return Request(
client: client,
httpRequest: http.Request('GET', url),
isPublicAction: true,
bodyDeserializer: (dynamic json) =>
Collection.fromJson(json as Map<String, dynamic>),
);
}
/// Get a collection’s photos
///
/// Retrieve a collection’s photos.
///
/// Note: See the note on [hotlinking](https://unsplash.com/documentation#hotlinking).
///
/// See: [Unsplash docs](https://unsplash.com/documentation#get-a-collections-photos)
Request<List<Photo>> photos(
String id, {
int? page,
int? perPage,
PhotoOrientation? orientation,
}) {
assert(page == null || page >= 0);
assert(perPage == null ||
perPage >= 0 && perPage <= client.settings.maxPageSize);
final params = queryParams({
'page': page,
'per_page': perPage,
'orientation': orientation?.let(enumName),
});
final url = baseUrl.resolve('$id/photos').replace(queryParameters: params);
return Request(
client: client,
httpRequest: http.Request('GET', url),
isPublicAction: true,
bodyDeserializer: _deserializePhotos,
);
}
/// List a collection’s related collections
///
/// Retrieve a list of collections related to this one.
///
/// See: [Unsplash docs](https://unsplash.com/documentation#list-a-collections-related-collections)
Request<List<Collection>> related(String id) {
final url = baseUrl.resolve('$id/related');
return Request(
client: client,
httpRequest: http.Request('GET', url),
isPublicAction: true,
bodyDeserializer: _deserializeCollections,
);
}
}
List<Collection> _deserializeCollections(dynamic body) {
return deserializeObjectList(body, (json) => Collection.fromJson(json));
}
List<Photo> _deserializePhotos(dynamic body) {
return deserializeObjectList(body, (json) => Photo.fromJson(json));
}