Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
feat: ask question api
Browse files Browse the repository at this point in the history
  • Loading branch information
omartinma committed Nov 21, 2023
1 parent 2e1e3b3 commit 2c21e27
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 256 deletions.
2 changes: 0 additions & 2 deletions lib/main_development.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ void main() {
() {
final apiClient = ApiClient(
baseUrl: 'http://localhost:8080',
idTokenStream: const Stream.empty(),
refreshIdToken: () async => Future.value(),
);

return App(
Expand Down
2 changes: 0 additions & 2 deletions lib/main_production.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ void main() {
() {
final apiClient = ApiClient(
baseUrl: 'http://production',
idTokenStream: const Stream.empty(),
refreshIdToken: () async => Future.value(),
);

return App(
Expand Down
11 changes: 11 additions & 0 deletions packages/api_client/example/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:api_client/api_client.dart';

Future<void> main() async {
final apiClient = ApiClient(
baseUrl: '',
);
final questionsResource = apiClient.questionsResource;
final answer = await questionsResource.getVertexResponse('random');
// ignore: avoid_print
print(answer);
}
2 changes: 2 additions & 0 deletions packages/api_client/lib/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
library api_client;

export 'src/api_client.dart';
export 'src/models/models.dart';
export 'src/resources/resources.dart';
35 changes: 4 additions & 31 deletions packages/api_client/lib/src/api_client.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:io';

import 'package:api_client/api_client.dart';
import 'package:http/http.dart' as http;

/// {@template api_client_error}
Expand Down Expand Up @@ -58,8 +59,6 @@ class ApiClient {
/// {@macro api_client}
ApiClient({
required String baseUrl,
required Stream<String?> idTokenStream,
required Future<String?> Function() refreshIdToken,
PostCall postCall = http.post,
PutCall putCall = http.put,
PatchCall patchCall = http.patch,
Expand All @@ -68,44 +67,28 @@ class ApiClient {
_post = postCall,
_put = putCall,
_patch = patchCall,
_get = getCall,
_refreshIdToken = refreshIdToken {
_idTokenSubscription = idTokenStream.listen((idToken) {
_idToken = idToken;
});
}
_get = getCall;

final Uri _base;
final PostCall _post;
final PostCall _put;
final PatchCall _patch;
final GetCall _get;
final Future<String?> Function() _refreshIdToken;

late final StreamSubscription<String?> _idTokenSubscription;
String? _idToken;

Map<String, String> get _headers => {
if (_idToken != null) 'Authorization': 'Bearer $_idToken',
};
/// Questions resource.
late final QuestionsResource questionsResource = const QuestionsResource();

Future<http.Response> _handleUnauthorized(
Future<http.Response> Function() sendRequest,
) async {
final response = await sendRequest();

if (response.statusCode == HttpStatus.unauthorized) {
_idToken = await _refreshIdToken();
return sendRequest();
}
return response;
}

/// Dispose of resources used by this client.
Future<void> dispose() async {
await _idTokenSubscription.cancel();
}

/// Sends a POST request to the specified [path] with the given [body].
Future<http.Response> post(
String path, {
Expand All @@ -119,7 +102,6 @@ class ApiClient {
queryParameters: queryParameters,
),
body: body,
headers: _headers..addContentTypeJson(),
);

return response;
Expand All @@ -139,7 +121,6 @@ class ApiClient {
queryParameters: queryParameters,
),
body: body,
headers: _headers..addContentTypeJson(),
);

return response;
Expand All @@ -155,7 +136,6 @@ class ApiClient {
final response = await _put(
_base.replace(path: path),
body: body,
headers: _headers..addContentTypeJson(),
);

return response;
Expand All @@ -173,16 +153,9 @@ class ApiClient {
path: path,
queryParameters: queryParameters,
),
headers: _headers,
);

return response;
});
}
}

extension on Map<String, String> {
void addContentTypeJson() {
addAll({HttpHeaders.contentTypeHeader: ContentType.json.value});
}
}
3 changes: 3 additions & 0 deletions packages/api_client/lib/src/models/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'vertex_document.dart';
export 'vertex_metadata.dart';
export 'vertex_response.dart';
26 changes: 26 additions & 0 deletions packages/api_client/lib/src/models/vertex_document.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:api_client/api_client.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'vertex_document.g.dart';

/// {@template vertex_document}
/// Vertex document
/// {@endtemplate}
@JsonSerializable(createToJson: false)
class VertexDocument extends Equatable {
/// {@macro vertex_document}
const VertexDocument({
required this.metadata,
});

/// Convert from Map<String, dynamic> to [VertexDocument]
factory VertexDocument.fromJson(Map<String, dynamic> json) =>
_$VertexDocumentFromJson(json);

/// Metadata.
final VertexMetadata metadata;

@override
List<Object?> get props => [metadata];
}
13 changes: 13 additions & 0 deletions packages/api_client/lib/src/models/vertex_document.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions packages/api_client/lib/src/models/vertex_metadata.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:api_client/api_client.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'vertex_metadata.g.dart';

/// {@template vertex_metadata}
/// Vertex metadata inside a [VertexResponse]
/// {@endtemplate}
@JsonSerializable(createToJson: false)
class VertexMetadata extends Equatable {
/// {@macro vertex_metadata}
const VertexMetadata({
required this.url,
required this.title,
required this.description,
});

/// Convert from Map<String, dynamic> to [VertexMetadata]
factory VertexMetadata.fromJson(Map<String, dynamic> json) =>
_$VertexMetadataFromJson(json);

/// Url
final String url;

/// Title
final String title;

/// Description
final String description;

@override
List<Object?> get props => [url, title, description];
}
14 changes: 14 additions & 0 deletions packages/api_client/lib/src/models/vertex_metadata.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions packages/api_client/lib/src/models/vertex_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:api_client/api_client.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'vertex_response.g.dart';

/// {@template vertex_response}
/// Vertex response
/// {@endtemplate}
@JsonSerializable(createToJson: false)
class VertexResponse extends Equatable {
/// {@macro vertex_response}
const VertexResponse({
required this.summary,
required this.documents,
});

/// Convert from Map<String, dynamic> to [VertexResponse]
factory VertexResponse.fromJson(Map<String, dynamic> json) =>
_$VertexResponseFromJson(json);

/// Summary.
final String summary;

/// Documents.
final List<VertexDocument> documents;

@override
List<Object?> get props => [documents];
}
15 changes: 15 additions & 0 deletions packages/api_client/lib/src/models/vertex_response.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2c21e27

Please sign in to comment.