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

Remote client package #241

Merged
merged 7 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:meta/meta.dart';
import 'package:mq_either/mq_either.dart';
import 'package:mq_remote_client/mq_remote_client.dart';
import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/app/app.dart';
import 'package:my_quran/config/config.dart';
Expand All @@ -15,7 +16,7 @@ final class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
required this.isIntegrationTest,
});

final MqDio client;
final MqRemoteClient client;
final PreferencesStorage storage;
final SoccialAuth soccialAuth;
final bool isIntegrationTest;
Expand Down
7 changes: 5 additions & 2 deletions app/lib/app/presentation/view/app_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/scheduler.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:just_audio/just_audio.dart';
import 'package:loader_overlay/loader_overlay.dart';
import 'package:mq_remote_client/mq_remote_client.dart';
import 'package:mq_storage/mq_storage.dart';

import 'package:my_quran/app/app.dart';
Expand Down Expand Up @@ -51,7 +52,7 @@ class MyApp extends StatelessWidget {
remoteDataSource: isMockData
? const AuthRemoteDataSourceMock()
: AuthRemoteDataSourceImpl(
client: context.read<MqDio>(),
client: context.read<MqRemoteClient>(),
storage: context.read<PreferencesStorage>(),
soccialAuth: context.read<SoccialAuth>(),
isIntegrationTest: context.read<AppConfig>().isIntegrationTest,
Expand All @@ -77,7 +78,9 @@ class MyApp extends StatelessWidget {
isMockData
? const HomeLocalDataSourceMock()
: HomeLocalDataSourceImpl(context.read<PreferencesStorage>()),
isMockData ? const HomeRemoteDataSourceMock() : HomeRemoteDataSourceImpl(context.read<MqDio>()),
isMockData
? const HomeRemoteDataSourceMock()
: HomeRemoteDataSourceImpl(context.read<MqRemoteClient>()),
),
),
),
Expand Down
3 changes: 0 additions & 3 deletions app/lib/core/core.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export 'auth/soccial_auth.dart';
export 'client/network_client.dart';
export 'client/http_exception.dart';
export 'client/mq_dio.dart';
export 'crashlytics/mq_crashlytics.dart';
export 'exceptions/network_exception.dart';
export 'exceptions/server_exception.dart';
Expand Down
5 changes: 3 additions & 2 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:just_audio_background/just_audio_background.dart';
import 'package:mq_remote_client/mq_remote_client.dart';
import 'package:mq_storage/mq_storage.dart';

import 'package:my_quran/app/app.dart';
Expand Down Expand Up @@ -73,8 +74,8 @@ Future<void> main({AppConfig? appConfig}) async {
create: (context) => NetworkClient(Connectivity()),
),
RepositoryProvider<MqRemoteConfig>(create: (context) => remoteConfig),
RepositoryProvider<MqDio>(
create: (context) => MqDio(
RepositoryProvider<MqRemoteClient>(
create: (context) => MqRemoteClient(
dio: Dio(),
network: context.read<NetworkClient>(),
language: () => storage.readString(key: StorageKeys.localeKey),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';

import 'package:mq_remote_client/mq_remote_client.dart';
import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/modules/modules.dart';
Expand All @@ -11,7 +12,7 @@ class HatimRemoteDataSourceImpl implements HatimRemoteDataSource {
required this.remoteClient,
});

final MqDio remoteClient;
final MqRemoteClient remoteClient;

late final WebSocketChannel channel;
bool isInitilized = false;
Expand Down
3 changes: 2 additions & 1 deletion app/lib/modules/hatim/presentation/view/hatim_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:mq_ci_keys/mq_ci_keys.dart';
import 'package:mq_remote_client/mq_remote_client.dart';

import 'package:my_quran/app/app.dart';
import 'package:my_quran/config/config.dart';
Expand All @@ -21,7 +22,7 @@ class HatimView extends StatelessWidget {
repo: HatimReadRepositoryImpl(
dataSource: context.read<AppConfig>().isMockData
? HatimRemoteDataSourceMock()
: HatimRemoteDataSourceImpl(remoteClient: context.read<MqDio>()),
: HatimRemoteDataSourceImpl(remoteClient: context.read<MqRemoteClient>()),
),
token: context.read<AuthCubit>().state.user!.accessToken,
)..add(const GetInitailDataEvent()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:flutter/material.dart';
import 'package:mq_remote_client/mq_remote_client.dart';
import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/modules/modules.dart';

@immutable
final class HomeRemoteDataSourceImpl implements HomeRemoteDataSource {
const HomeRemoteDataSourceImpl(this.remoteClient);

final MqDio remoteClient;
final MqRemoteClient remoteClient;

@override
Future<HomeModelResponse> getRemoteData() async {
Expand Down
1 change: 1 addition & 0 deletions app/lib/modules/quran_audio/logic/quran_audio_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:just_audio/just_audio.dart';
import 'package:just_audio_background/just_audio_background.dart';
import 'package:mq_remote_client/mq_remote_client.dart';

import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:meta/meta.dart';
import 'package:mq_remote_client/mq_remote_client.dart';
import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/modules/modules.dart';

@immutable
final class ReadRemoteDataSourceImpl implements ReadRemoteDataSource {
const ReadRemoteDataSourceImpl(this.remoteClient);

final MqDio remoteClient;
final MqRemoteClient remoteClient;

@override
Future<QuranPageResponse?> fetchPage(int page, String quranFmt) async {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/modules/read/presentation/view/read_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:mq_ci_keys/mq_ci_keys.dart';
import 'package:mq_remote_client/mq_remote_client.dart';
import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/config/config.dart';
import 'package:my_quran/constants/contants.dart';
Expand Down Expand Up @@ -33,7 +34,7 @@ class ReadView extends StatelessWidget {
ReadRepositoryImpl(
context.read<AppConfig>().isMockData
? ReadRemoteDataSourceMock()
: ReadRemoteDataSourceImpl(context.read<MqDio>()),
: ReadRemoteDataSourceImpl(context.read<MqRemoteClient>()),
ReadLocalDataSourceImpl(context.read<PreferencesStorage>()),
),
),
Expand Down
7 changes: 7 additions & 0 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,13 @@ packages:
relative: true
source: path
version: "0.1.0+1"
mq_remote_client:
dependency: "direct main"
description:
path: "../packages/mq_remote_client"
relative: true
source: path
version: "0.1.0+1"
mq_storage:
dependency: "direct main"
description:
Expand Down
10 changes: 6 additions & 4 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ dependencies:
just_audio_background: ^0.0.1-beta.12
meta: ^1.12.0 # 1.15.0
loader_overlay: ^4.0.0
mq_storage:
path: ../packages/mq_storage/
mq_ci_keys:
path: ../packages/mq_ci_keys/
path: ../packages/mq_ci_keys
mq_either:
path: ../packages/mq_either/
path: ../packages/mq_either
mq_remote_client:
path: ../packages/mq_remote_client
mq_storage:
path: ../packages/mq_storage
# flutter_native_splash: ^2.4.0

dev_dependencies:
Expand Down
3 changes: 3 additions & 0 deletions app/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ dependency_overrides:
path: ../packages/mq_ci_keys
mq_either:
path: ../packages/mq_either
mq_remote_client:
path: ../packages/mq_remote_client
mq_storage:
path: ../packages/mq_storage

3 changes: 2 additions & 1 deletion app/test/mocks/app_mocks.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:mq_remote_client/mq_remote_client.dart';
import 'package:package_info_plus/package_info_plus.dart';

import 'package:mocktail/mocktail.dart';
Expand All @@ -8,7 +9,7 @@ import 'package:my_quran/modules/modules.dart';

final class MockPreferencesStorage extends Mock implements PreferencesStorage {}

final class MockRemoteClient extends Mock implements MqDio {}
final class MockRemoteClient extends Mock implements MqRemoteClient {}

final class MockSccialAuth extends Mock implements SoccialAuth {}

Expand Down
98 changes: 98 additions & 0 deletions packages/mq_remote_client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# MqRemoteClient

MqRemoteClient is a Dart package that simplifies HTTP requests using the Dio library, providing a convenient and consistent way to handle network operations with error handling and custom token management.

## Features

- HTTP GET, POST, PUT, PATCH methods
- Custom headers including token and language
- Error handling using custom exceptions
- Easily mockable for testing

## Installation

Add `mq_remote_client` to your `pubspec.yaml`:

## Usage

### Initialization

Create an instance of `MqRemoteClient` and initialize it:

```dart
import 'package:dio/dio.dart';
import 'package:mq_remote_client/mq_remote_client.dart';

final dio = Dio();
final networkClient = NetworkClient();

final mqRemoteClient = MqRemoteClient(
dio: dio,
network: networkClient,
language: () => 'en',
token: () => 'your_token',
oldToken: () => 'your_old_token',
);

mqRemoteClient.initilize();
```

## Making Requests

### GET Request

```dart
final result = await mqRemoteClient.get<Map<String, dynamic>>(
'https://example.com/api/resource',
fromJson: (json) => json,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fromJson: (json) => json, not need to await mqRemoteClient.get

);

result.fold(
(left) => print('Error: $left'),
(right) => print('Data: $right'),
);
```

### POST Request

```dart
final result = await mqRemoteClient.post<Map<String, dynamic>>(
'https://example.com/api/resource',
fromJson: (json) => json,
body: {'key': 'value'},
);

result.fold(
(left) => print('Error: $left'),
(right) => print('Data: $right'),
);
```
### PUT Request

```dart
final result = await mqRemoteClient.put<Map<String, dynamic>>(
'https://example.com/api/resource',
fromJson: (json) => json,
body: {'key': 'value'},
);

result.fold(
(left) => print('Error: $left'),
(right) => print('Data: $right'),
);
```

### PATCH Request

```dart
final result = await mqRemoteClient.patch<Map<String, dynamic>>(
'https://example.com/api/resource',
fromJson: (json) => json,
body: {'key': 'value'},
);

result.fold(
(left) => print('Error: $left'),
(right) => print('Data: $right'),
);
```
7 changes: 7 additions & 0 deletions packages/mq_remote_client/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include: package:very_good_analysis/analysis_options.yaml

linter:
rules:
public_member_api_docs: false
lines_longer_than_80_chars: false
sort_pub_dependencies: false
6 changes: 6 additions & 0 deletions packages/mq_remote_client/lib/mq_remote_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// MQ remote client package
library;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add mq_remote_client


export 'src/http_exception.dart';
export 'src/mq_remote_client.dart';
export 'src/network_client.dart';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add documantation

File renamed without changes.
Loading
Loading