Skip to content

Commit

Permalink
refactor(api): update apigateway (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutsu3 authored Dec 7, 2024
1 parent 7ad4b88 commit 50a707f
Show file tree
Hide file tree
Showing 29 changed files with 2,361 additions and 624 deletions.
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SENTRY_DSN=
ENABLE_SENTRY=false
# Select one of the following: debug, info, warning, error
LOG_LEVEL=debug
LOG_LEVEL=info
46 changes: 46 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Dart Tests

on:
push:
branches:
- main
tags:
- "v[0-9]+.[0-9]+.[0-9]+*"
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: stable
flutter-version: ${{ env.flutter_version }}
cache: true
# optional parameters follow
cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:" # optional, change this to force refresh cache
cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:" # optional, change this to specify the cache path
pub-cache-key: "flutter-pub:os:-:channel:-:version:-:arch:-:hash:" # optional, change this to force refresh cache of dart pub get dependencies
pub-cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:" # optional, change this to specify the cache path

- name: Install dependencies
run: flutter pub get

- name: Prepare env file
run: cp .env.sample .env

- name: Run tests
run: flutter test --coverage

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ app.*.map.json
untranslated.txt

# Env file
.env
.env

coverage/
13 changes: 9 additions & 4 deletions lib/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:animations/animations.dart';
import 'package:pi_hole_client/models/gateways.dart';
import 'package:provider/provider.dart';

import 'package:pi_hole_client/widgets/navigation_rail.dart';
Expand Down Expand Up @@ -54,11 +55,15 @@ class _BaseState extends State<Base> with WidgetsBindingObserver {
final result = await Future.wait(
[apiGateway!.realtimeStatus(), apiGateway.fetchOverTimeData()]);

if (result[0]['result'] == 'success' && result[1]['result'] == 'success') {
statusProvider.setRealtimeStatus(result[0]['data']);
statusProvider.setOvertimeData(result[1]['data']);
final realtimeStatusResponse = result[0] as RealtimeStatusResponse;
final overTimeDataResponse = result[1] as FetchOverTimeDataResponse;

if (realtimeStatusResponse.result == APiResponseType.success &&
overTimeDataResponse.result == APiResponseType.success) {
statusProvider.setRealtimeStatus(realtimeStatusResponse.data!);
statusProvider.setOvertimeData(overTimeDataResponse.data!);
serversProvider.updateselectedServerStatus(
result[0]['data'].status == 'enabled' ? true : false);
realtimeStatusResponse.data!.status == 'enabled' ? true : false);

statusProvider.setOvertimeDataLoadingStatus(1);
statusProvider.setStatusLoading(LoadStatus.loaded);
Expand Down
4 changes: 4 additions & 0 deletions lib/constants/api_versions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class SupportedApiVersions {
static const String v5 = 'v5';
static const String v6 = 'v6';
}
7 changes: 7 additions & 0 deletions lib/functions/convert.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:pi_hole_client/models/domain.dart';

List<Domain> parseDomainList(dynamic jsonList) {
return (jsonList as List<dynamic>)
.map((item) => Domain.fromJson(item as Map<String, dynamic>))
.toList();
}
9 changes: 5 additions & 4 deletions lib/functions/refresh_server_status.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:pi_hole_client/models/gateways.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

Expand All @@ -19,12 +20,12 @@ Future refreshServerStatus(BuildContext context) async {

final result = await apiGateway?.realtimeStatus();
if (!context.mounted) return;
if (result['result'] == "success") {
if (result?.result == APiResponseType.success) {
serversProvider.updateselectedServerStatus(
result['data'].status == 'enabled' ? true : false);
result!.data?.status == 'enabled' ? true : false);
statusProvider.setIsServerConnected(true);
statusProvider.setRealtimeStatus(result['data']);
} else if (result['result'] == 'ssl_error') {
statusProvider.setRealtimeStatus(result.data!);
} else if (result?.result == APiResponseType.sslError) {
statusProvider.setIsServerConnected(false);
if (statusProvider.getStatusLoading == LoadStatus.loading) {
statusProvider.setStatusLoading(LoadStatus.error);
Expand Down
5 changes: 3 additions & 2 deletions lib/functions/server_management.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:pi_hole_client/models/gateways.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

Expand All @@ -19,7 +20,7 @@ void enableServer(BuildContext context) async {
process.open(AppLocalizations.of(context)!.enablingServer);
final result = await apiGateway?.enableServerRequest();
process.close();
if (result['result'] == 'success') {
if (result?.result == APiResponseType.success) {
serversProvider.updateselectedServerStatus(true);
showSnackBar(
appConfigProvider: appConfigProvider,
Expand All @@ -44,7 +45,7 @@ void disableServer(int time, BuildContext context) async {
final result = await apiGateway?.disableServerRequest(time);
process.close();
if (!context.mounted) return;
if (result['result'] == 'success') {
if (result?.result == APiResponseType.success) {
serversProvider.updateselectedServerStatus(false);
showSnackBar(
appConfigProvider: appConfigProvider,
Expand Down
13 changes: 7 additions & 6 deletions lib/functions/status_updater.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:pi_hole_client/models/gateways.dart';
import 'package:pi_hole_client/providers/status_provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -36,10 +37,10 @@ class StatusUpdater {
final apiGateway = serversProvider.selectedApiGateway;
String selectedUrlBefore = serversProvider.selectedServer!.address;
final statusResult = await apiGateway?.realtimeStatus();
if (statusResult['result'] == 'success') {
if (statusResult?.result == APiResponseType.success) {
serversProvider.updateselectedServerStatus(
statusResult['data'].status == 'enabled' ? true : false);
statusProvider.setRealtimeStatus(statusResult['data']);
statusResult!.data!.status == 'enabled' ? true : false);
statusProvider.setRealtimeStatus(statusResult.data!);
if (statusProvider.isServerConnected == false) {
statusProvider.setIsServerConnected(true);
}
Expand Down Expand Up @@ -73,9 +74,9 @@ class StatusUpdater {
final apiGateway = serversProvider.selectedApiGateway;
String statusUrlBefore = serversProvider.selectedServer!.address;
final statusResult = await apiGateway?.fetchOverTimeData();
if (statusResult['result'] == 'success') {
statusProvider.setOvertimeData(statusResult['data']);
List<dynamic> clients = statusResult['data'].clients.map((client) {
if (statusResult?.result == APiResponseType.success) {
statusProvider.setOvertimeData(statusResult!.data!);
List<dynamic> clients = statusResult.data!.clients.map((client) {
if (client.name != '') {
return client.name.toString();
} else {
Expand Down
18 changes: 18 additions & 0 deletions lib/gateways/api_gateway_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:pi_hole_client/gateways/api_gateway_interface.dart';
import 'package:pi_hole_client/gateways/v5/api_gateway_v5.dart';
// import 'package:pi_hole_client/gateways/v5/api_gateway_v6.dart';
import 'package:pi_hole_client/models/server.dart';

class ApiGatewayFactory {
static ApiGateway create(Server server) {
final version = server.apiVersion;
if (version == 'v5') {
return ApiGatewayV5(server);
} else if (version == 'v6') {
// return ApiGatewayV6(server);
throw Exception('Not implemented yet');
} else {
throw Exception('Unsupported server version: $version');
}
}
}
Loading

0 comments on commit 50a707f

Please sign in to comment.