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

feat: adding support for getChart #9

Merged
merged 3 commits into from
Nov 28, 2023
Merged
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
84 changes: 49 additions & 35 deletions lib/app_center_ratings_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import 'dart:async';
import 'package:grpc/grpc.dart';
import 'package:meta/meta.dart';

import 'src/app.dart' as app;
import 'src/chart.dart';
import 'src/generated/google/protobuf/empty.pb.dart';
import 'src/generated/ratings_features_app.pbgrpc.dart';
import 'src/generated/ratings_features_user.pbgrpc.dart';
import 'src/user.dart' as user;
import 'src/generated/ratings_features_app.pbgrpc.dart' as appPb;
import 'src/generated/ratings_features_chart.pbgrpc.dart' as chartPb;
import 'src/generated/ratings_features_user.pbgrpc.dart' as userPb;
import 'src/ratings.dart';
import 'src/user.dart';

class RatingsClient {
late AppClient _appClient;
late UserClient _userClient;
late appPb.AppClient _appClient;
late userPb.UserClient _userClient;
late chartPb.ChartClient _chartClient;

RatingsClient(String serverUrl, int port) {
final channel = ClientChannel(
Expand All @@ -21,22 +24,46 @@ class RatingsClient {
credentials: ChannelCredentials.insecure(),
),
);
_appClient = AppClient(channel);
_userClient = UserClient(channel);
_appClient = appPb.AppClient(channel);
_userClient = userPb.UserClient(channel);
_chartClient = chartPb.ChartClient(channel);
}

// Additional constructor for testing
@visibleForTesting
RatingsClient.withClients(
this._appClient,
this._userClient,
this._chartClient,
);

Future<app.Rating> getRating(
Future<String> authenticate(String id) async {
final request = userPb.AuthenticateRequest(id: id);
final grpcResponse = await _userClient.authenticate(request);
return grpcResponse.token;
}

Future<void> delete(String token) async {
final request = Empty();
final callOptions =
CallOptions(metadata: {'authorization': 'Bearer $token'});
await _userClient.delete(request, options: callOptions);
}

Future<List<ChartData>> getChart(Timeframe timeframe, String token) async {
final request = chartPb.GetChartRequest(timeframe: timeframe.toDTO());
final callOptions =
CallOptions(metadata: {'authorization': 'Bearer $token'});
final grpcResponse =
await _chartClient.getChart(request, options: callOptions);
return grpcResponse.orderedChartData.map((data) => data.fromDTO()).toList();
}

Future<Rating> getRating(
String snapId,
String token,
) async {
final request = GetRatingRequest(snapId: snapId);
final request = appPb.GetRatingRequest(snapId: snapId);
final callOptions =
CallOptions(metadata: {'authorization': 'Bearer $token'});
final grpcResponse = await _appClient.getRating(
Expand All @@ -46,14 +73,19 @@ class RatingsClient {
return grpcResponse.rating.fromDTO();
}

Future<String> authenticate(String id) async {
final request = AuthenticateRequest(id: id);
final grpcResponse = await _userClient.authenticate(request);
return grpcResponse.token;
Future<List<Vote>> getSnapVotes(String snap_id, String token) async {
final request = userPb.GetSnapVotesRequest(snapId: snap_id);
final callOptions =
CallOptions(metadata: {'authorization': 'Bearer $token'});
final grpcResponse = await _userClient.getSnapVotes(
request,
options: callOptions,
);
return grpcResponse.votes.map((vote) => vote.fromDTO()).toList();
}

Future<List<user.Vote>> listMyVotes(String snapIdFilter, String token) async {
final request = ListMyVotesRequest(snapIdFilter: snapIdFilter);
Future<List<Vote>> listMyVotes(String snapIdFilter, String token) async {
final request = userPb.ListMyVotesRequest(snapIdFilter: snapIdFilter);
final callOptions =
CallOptions(metadata: {'authorization': 'Bearer $token'});
final grpcResponse = await _userClient.listMyVotes(
Expand All @@ -65,7 +97,7 @@ class RatingsClient {

Future<void> vote(
String snapId, int snapRevision, bool voteUp, String token) async {
final request = VoteRequest(
final request = userPb.VoteRequest(
snapId: snapId,
snapRevision: snapRevision,
voteUp: voteUp,
Expand All @@ -74,22 +106,4 @@ class RatingsClient {
CallOptions(metadata: {'authorization': 'Bearer $token'});
await _userClient.vote(request, options: callOptions);
}

Future<void> delete(String token) async {
final request = Empty();
final callOptions =
CallOptions(metadata: {'authorization': 'Bearer $token'});
await _userClient.delete(request, options: callOptions);
}

Future<List<user.Vote>> getSnapVotes(String snap_id, String token) async {
final request = GetSnapVotesRequest(snapId: snap_id);
final callOptions =
CallOptions(metadata: {'authorization': 'Bearer $token'});
final grpcResponse = await _userClient.getSnapVotes(
request,
options: callOptions,
);
return grpcResponse.votes.map((vote) => vote.fromDTO()).toList();
}
}
167 changes: 0 additions & 167 deletions lib/src/app.freezed.dart

This file was deleted.

50 changes: 50 additions & 0 deletions lib/src/chart.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import 'generated/ratings_features_chart.pb.dart' as pb;
import 'ratings.dart' as common;

part 'chart.freezed.dart';

@freezed
class ChartData with _$ChartData {
const factory ChartData({
required double rawRating,
required common.Rating rating,
}) = _ChartData;
}

enum Timeframe {
unspecified,
week,
month,
}

extension ChartDataFromDTO on pb.ChartData {
ChartData fromDTO() {
return ChartData(
rating: this.rating.fromDTO(),
rawRating: this.rawRating,
);
}
}

extension TimeframeFromDTO on pb.Timeframe {
Timeframe fromDTO() {
return switch (this) {
pb.Timeframe.TIMEFRAME_UNSPECIFIED => Timeframe.unspecified,
pb.Timeframe.TIMEFRAME_WEEK => Timeframe.week,
pb.Timeframe.TIMEFRAME_MONTH => Timeframe.month,
_ => Timeframe.unspecified,
};
}
}

extension TimeframeToDTO on Timeframe {
pb.Timeframe toDTO() {
return switch (this) {
Timeframe.unspecified => pb.Timeframe.TIMEFRAME_UNSPECIFIED,
Timeframe.week => pb.Timeframe.TIMEFRAME_WEEK,
Timeframe.month => pb.Timeframe.TIMEFRAME_MONTH,
};
}
}
Loading