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

fix: list value for GET search params #136

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/src/core/service_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class ServiceHelper implements Service {
return object;
}

Map<String, String> _convertQueryParameters(
Map<String, dynamic> _convertQueryParameters(
final Map<String, dynamic> queryParameters,
) {
final serializedParameters = queryParameters.map((key, value) {
Expand All @@ -301,16 +301,16 @@ class ServiceHelper implements Service {
} else if (value is List?) {
return MapEntry(
key,
value?.toSet().join(','),
value?.map((e) => e.toString()).toList()
);
} else if (value is Serializable) {
return MapEntry(
key,
value.value,
value.value.toString(),
myConsciousness marked this conversation as resolved.
Show resolved Hide resolved
);
}

return MapEntry(key, value);
return MapEntry(key, value.toString());
});

return Map.from(_removeNullValues(serializedParameters) ?? {}).map(
Expand All @@ -322,7 +322,7 @@ class ServiceHelper implements Service {
return MapEntry(key, value.toUtc().toIso8601String());
}

return MapEntry(key, value.toString());
return MapEntry(key, value);
},
);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/src/service/v1/accounts/accounts_v1_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,10 @@ class _AccountsV1Service extends BaseService implements AccountsV1Service {
super.transformMultiDataResponse(
await super.get(
UserContext.oauth2Only,
'/api/v1/accounts/relationships?${accountIds.map((e) => 'id[]=$e').join('&')}',
'/api/v1/accounts/relationships',
queryParameters: {
'id[]': accountIds,
}
),
dataBuilder: Relationship.fromJson,
);
Expand All @@ -1745,7 +1748,10 @@ class _AccountsV1Service extends BaseService implements AccountsV1Service {
super.transformMultiDataResponse(
await super.get(
UserContext.oauth2Only,
'/api/v1/accounts/familiar_followers?${accountIds.map((e) => 'id[]=$e').join('&')}',
'/api/v1/accounts/familiar_followers',
queryParameters: {
'id[]': accountIds
}
),
dataBuilder: FamiliarFollower.fromJson,
);
Expand Down
37 changes: 22 additions & 15 deletions test/mocks/client_context_stubs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ MockClientContext buildGetStub(
final UserContext userContext,
final String unencodedPath,
final String resourcePath,
final Map<String, String> queryParameters, {
final Map<String, dynamic> queryParameters, {
Map<String, String> headers = const {},
int statusCode = 200,
}) {
final mockClientContext = MockClientContext();
final requestUri = Uri.https(instance, unencodedPath, queryParameters);

when(mockClientContext.get(
userContext,
Uri.https(instance, unencodedPath, queryParameters),
requestUri,
headers: headers,
)).thenAnswer(
(_) async => Response(
Expand All @@ -35,7 +36,7 @@ MockClientContext buildGetStub(
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'GET',
Uri(),
requestUri,
),
),
);
Expand All @@ -51,10 +52,11 @@ MockClientContext buildPostStub(
int statusCode = 200,
}) {
final mockClientContext = MockClientContext();
final requestUri = Uri.https(instance, unencodedPath, {});

when(mockClientContext.post(
userContext,
Uri.https(instance, unencodedPath, {}),
requestUri,
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer(
Expand All @@ -66,7 +68,7 @@ MockClientContext buildPostStub(
},
request: Request(
'POST',
Uri(),
requestUri,
),
),
);
Expand All @@ -83,10 +85,11 @@ MockClientContext buildPostMultipartStub(
int statusCode = 200,
}) {
final mockClientContext = MockClientContext();
final requestUri = Uri.https(instance, unencodedPath, queryParameters);

when(mockClientContext.postMultipart(
userContext,
Uri.https(instance, unencodedPath, queryParameters),
requestUri,
files: anyNamed('files'),
)).thenAnswer(
(_) async => Response(
Expand All @@ -97,7 +100,7 @@ MockClientContext buildPostMultipartStub(
},
request: Request(
'POST',
Uri(),
requestUri,
),
),
);
Expand All @@ -112,10 +115,11 @@ MockClientContext buildDeleteStub(
int statusCode = 200,
}) {
final mockClientContext = MockClientContext();
final requestUri = Uri.https(instance, unencodedPath);

when(mockClientContext.delete(
UserContext.oauth2Only,
Uri.https(instance, unencodedPath),
requestUri,
body: anyNamed('body'),
)).thenAnswer(
(_) async => Response(
Expand All @@ -124,7 +128,7 @@ MockClientContext buildDeleteStub(
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'DELETE',
Uri(),
requestUri,
),
),
);
Expand All @@ -139,10 +143,11 @@ MockClientContext buildPutStub(
int statusCode = 200,
}) {
final mockClientContext = MockClientContext();
final requestUri = Uri.https(instance, unencodedPath);

when(mockClientContext.put(
UserContext.oauth2Only,
Uri.https(instance, unencodedPath),
requestUri,
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer(
Expand All @@ -152,7 +157,7 @@ MockClientContext buildPutStub(
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'PUT',
Uri(),
requestUri,
),
),
);
Expand All @@ -168,10 +173,11 @@ MockClientContext buildPatchStub(
int statusCode = 200,
}) {
final mockClientContext = MockClientContext();
final requestUri = Uri.https(instance, unencodedPath);

when(mockClientContext.patch(
userContext,
Uri.https(instance, unencodedPath),
requestUri,
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer(
Expand All @@ -181,7 +187,7 @@ MockClientContext buildPatchStub(
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'PATCH',
Uri(),
requestUri,
),
),
);
Expand All @@ -197,10 +203,11 @@ MockClientContext buildPatchMultipartStub(
int statusCode = 200,
}) {
final mockClientContext = MockClientContext();
final requestUri = Uri.https(instance, unencodedPath);

when(mockClientContext.patchMultipart(
userContext,
Uri.https(instance, unencodedPath),
requestUri,
files: anyNamed('files'),
)).thenAnswer(
(_) async => Response(
Expand All @@ -209,7 +216,7 @@ MockClientContext buildPatchMultipartStub(
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'PATCH',
Uri(),
requestUri,
),
),
);
Expand Down
48 changes: 36 additions & 12 deletions test/src/service/v1/accounts/accounts_v1_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1313,9 +1313,11 @@ void main() {
context: context.buildGetStub(
'test',
UserContext.oauth2Only,
'/api/v1/accounts/relationships?id[]=1&id[]=2',
'/api/v1/accounts/relationships',
'test/src/service/v1/accounts/data/lookup_relationships.json',
{},
{
'id[]': ['1', '2'],
},
),
);

Expand All @@ -1325,6 +1327,12 @@ void main() {
expect(response, isA<MastodonResponse>());
expect(response.rateLimit, isA<RateLimit>());
expect(response.data, isA<List<Relationship>>());
expect(
response.request.url,
Uri.parse("https://test/api/v1/accounts/relationships"
"?id[]=1"
"&id[]=2"),
);
});

test('when unauthorized', () async {
Expand All @@ -1333,9 +1341,11 @@ void main() {
context: context.buildGetStub(
'test',
UserContext.oauth2Only,
'/api/v1/accounts/relationships?id[]=1&id[]=2',
'/api/v1/accounts/relationships',
'test/src/service/v1/accounts/data/lookup_relationships.json',
{},
{
'id[]': ['1', '2'],
},
statusCode: 401,
),
);
Expand All @@ -1352,9 +1362,11 @@ void main() {
context: context.buildGetStub(
'test',
UserContext.oauth2Only,
'/api/v1/accounts/relationships?id[]=1&id[]=2',
'/api/v1/accounts/relationships',
'test/src/service/v1/accounts/data/lookup_relationships.json',
{},
{
'id[]': ['1', '2'],
},
statusCode: 429,
),
);
Expand All @@ -1373,9 +1385,11 @@ void main() {
context: context.buildGetStub(
'test',
UserContext.oauth2Only,
'/api/v1/accounts/familiar_followers?id[]=1&id[]=2',
'/api/v1/accounts/familiar_followers',
'test/src/service/v1/accounts/data/lookup_familiar_followers.json',
{},
{
'id[]': ['1', '2'],
},
),
);

Expand All @@ -1385,6 +1399,12 @@ void main() {
expect(response, isA<MastodonResponse>());
expect(response.rateLimit, isA<RateLimit>());
expect(response.data, isA<List<FamiliarFollower>>());
expect(
response.request.url,
Uri.parse("https://test/api/v1/accounts/familiar_followers"
"?id[]=1"
"&id[]=2"),
);
});

test('when unauthorized', () async {
Expand All @@ -1393,9 +1413,11 @@ void main() {
context: context.buildGetStub(
'test',
UserContext.oauth2Only,
'/api/v1/accounts/familiar_followers?id[]=1&id[]=2',
'/api/v1/accounts/familiar_followers',
'test/src/service/v1/accounts/data/lookup_familiar_followers.json',
{},
{
'id[]': ['1', '2'],
},
statusCode: 401,
),
);
Expand All @@ -1412,9 +1434,11 @@ void main() {
context: context.buildGetStub(
'test',
UserContext.oauth2Only,
'/api/v1/accounts/familiar_followers?id[]=1&id[]=2',
'/api/v1/accounts/familiar_followers',
'test/src/service/v1/accounts/data/lookup_familiar_followers.json',
{},
{
'id[]': ['1', '2'],
},
statusCode: 429,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ void main() {
'since_id': '0987',
'min_id': '1234',
'limit': '40',
'types[]': NotificationType.mention.value,
'exclude_types[]': NotificationType.follow.value,
'types[]': [
NotificationType.mention.value,
NotificationType.favourite.value
],
'exclude_types[]': [
NotificationType.follow.value,
NotificationType.poll.value
],
'account_id': '1111',
}),
);
Expand All @@ -42,14 +48,27 @@ void main() {
sinceNotificationId: '0987',
minNotificationId: '1234',
limit: 40,
types: [NotificationType.mention],
excludeTypes: [NotificationType.follow],
types: [NotificationType.mention, NotificationType.favourite],
excludeTypes: [NotificationType.follow, NotificationType.poll],
accountId: '1111',
);

expect(response, isA<MastodonResponse>());
expect(response.rateLimit, isA<RateLimit>());
expect(response.data, isA<List<Notification>>());
expect(
response.request.url,
Uri.parse("https://test/api/v1/notifications"
"?max_id=5678"
"&since_id=0987"
"&min_id=1234"
"&limit=40"
"&types[]=${NotificationType.mention.value}"
"&types[]=${NotificationType.favourite.value}"
"&exclude_types[]=${NotificationType.follow.value}"
"&exclude_types[]=${NotificationType.poll.value}"
"&account_id=1111"),
);
});

test('when unauthorized', () async {
Expand Down