Skip to content

Commit

Permalink
use http callable
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sidhdhi-p committed Sep 16, 2024
1 parent 57eebbb commit eafa672
Show file tree
Hide file tree
Showing 35 changed files with 543 additions and 221 deletions.
44 changes: 23 additions & 21 deletions data/.flutter-plugins
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# This is a generated file; do not edit or check into version control.
cloud_firestore=/home/mayank/.pub-cache/hosted/pub.dev/cloud_firestore-5.2.0/
cloud_firestore_web=/home/mayank/.pub-cache/hosted/pub.dev/cloud_firestore_web-4.1.0/
device_info_plus=/home/mayank/.pub-cache/hosted/pub.dev/device_info_plus-10.1.1/
firebase_auth=/home/mayank/.pub-cache/hosted/pub.dev/firebase_auth-5.1.3/
firebase_auth_web=/home/mayank/.pub-cache/hosted/pub.dev/firebase_auth_web-5.12.5/
firebase_core=/home/mayank/.pub-cache/hosted/pub.dev/firebase_core-3.3.0/
firebase_core_web=/home/mayank/.pub-cache/hosted/pub.dev/firebase_core_web-2.17.4/
firebase_messaging=/home/mayank/.pub-cache/hosted/pub.dev/firebase_messaging-15.0.4/
firebase_messaging_web=/home/mayank/.pub-cache/hosted/pub.dev/firebase_messaging_web-3.8.12/
firebase_storage=/home/mayank/.pub-cache/hosted/pub.dev/firebase_storage-12.1.2/
firebase_storage_web=/home/mayank/.pub-cache/hosted/pub.dev/firebase_storage_web-3.9.12/
flutter_timezone=/home/mayank/.pub-cache/hosted/pub.dev/flutter_timezone-2.1.0/
package_info_plus=/home/mayank/.pub-cache/hosted/pub.dev/package_info_plus-8.0.1/
path_provider_linux=/home/mayank/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/
path_provider_windows=/home/mayank/.pub-cache/hosted/pub.dev/path_provider_windows-2.3.0/
shared_preferences=/home/mayank/.pub-cache/hosted/pub.dev/shared_preferences-2.3.1/
shared_preferences_android=/home/mayank/.pub-cache/hosted/pub.dev/shared_preferences_android-2.3.0/
shared_preferences_foundation=/home/mayank/.pub-cache/hosted/pub.dev/shared_preferences_foundation-2.5.0/
shared_preferences_linux=/home/mayank/.pub-cache/hosted/pub.dev/shared_preferences_linux-2.4.0/
shared_preferences_web=/home/mayank/.pub-cache/hosted/pub.dev/shared_preferences_web-2.4.1/
shared_preferences_windows=/home/mayank/.pub-cache/hosted/pub.dev/shared_preferences_windows-2.4.0/
cloud_firestore=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/cloud_firestore-5.0.2/
cloud_firestore_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/cloud_firestore_web-4.0.2/
cloud_functions=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/cloud_functions-5.1.1/
cloud_functions_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/cloud_functions_web-4.10.0/
device_info_plus=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/device_info_plus-10.1.2/
firebase_auth=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_auth-5.1.1/
firebase_auth_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_auth_web-5.13.0/
firebase_core=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_core-3.4.1/
firebase_core_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_core_web-2.18.0/
firebase_messaging=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_messaging-15.0.2/
firebase_messaging_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_messaging_web-3.9.0/
firebase_storage=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_storage-12.1.0/
firebase_storage_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_storage_web-3.10.0/
flutter_timezone=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/flutter_timezone-2.0.1/
package_info_plus=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/package_info_plus-8.0.2/
path_provider_linux=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/
path_provider_windows=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/path_provider_windows-2.2.1/
shared_preferences=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/shared_preferences-2.2.3/
shared_preferences_android=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/shared_preferences_android-2.2.3/
shared_preferences_foundation=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/shared_preferences_foundation-2.4.0/
shared_preferences_linux=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/shared_preferences_linux-2.3.2/
shared_preferences_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/shared_preferences_web-2.2.1/
shared_preferences_windows=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/shared_preferences_windows-2.3.2/
2 changes: 1 addition & 1 deletion data/.flutter-plugins-dependencies

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions data/lib/api/network/client.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../errors/app_error.dart';
import '../../service/auth/auth_service.dart';
import '../../utils/constant/firestore_constant.dart';

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

import 'endpoint.dart';
import 'interceptor/auth_client.dart';

final httpProvider = Provider((ref) {
final client = http.Client();
return AuthHttpClient(client, ref.read(firebaseAuthProvider));
});

final rawDioProvider = Provider((ref) {
return Dio()
..options.connectTimeout = const Duration(seconds: 30)
..options.sendTimeout = const Duration(seconds: 30)
..options.receiveTimeout = const Duration(seconds: 30);
});

extension HttpExtensions on http.Client {
Future<http.Response> req(Endpoint endpoint) async {

final request = http.Request(
endpoint.method.name,
Uri.parse('${DataConfig.instance.apiBaseUrl}${endpoint.path}'),
)..headers.addAll(endpoint.headers);

if (endpoint.data != null) {
request.headers['content-type'] = 'application/json';
request.body = jsonEncode(endpoint.data);
}

http.Response response;

try {
response = await http.Response.fromStream(await send(request));

if (response.statusCode >= 200 && response.statusCode < 300) {
return response;
}
} catch (error) {
if (error is SocketException) {
throw const NoConnectionError();
} else if (error is TimeoutException) {
throw const NoConnectionError();
} else {
rethrow;
}
}

throw SomethingWentWrongError(
statusCode: response.statusCode.toString(),
message: response.body,
);
}
}

extension HttpResponseExtensions on http.Response {
Map<String, dynamic> get data {
return body.isNotEmpty ? jsonDecode(utf8.decode(bodyBytes)) : {};
}
}
22 changes: 22 additions & 0 deletions data/lib/api/network/endpoint.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:flutter/material.dart';

enum HttpMethod { get, post, put, delete, patch }

@immutable
abstract class Endpoint {
HttpMethod get method => HttpMethod.get;

String get path;

Map<String, String>? get queryParameters => null;

Map<String, String> get headers => const {};

dynamic get data => null;

bool get needsAuth => true;

String? get baseUrl => null;

String? get contentType => null;
}
23 changes: 23 additions & 0 deletions data/lib/api/network/interceptor/auth_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:async';

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

class AuthHttpClient extends http.BaseClient {
final http.Client inner;
final FirebaseAuth firebaseAuth;

AuthHttpClient(this.inner, this.firebaseAuth);

@override
Future<http.StreamedResponse> send(http.BaseRequest request) async {
final user = firebaseAuth.currentUser;

if (user != null) {
final idToken = await user.getIdToken();
request.headers['Authorization'] = 'Bearer $idToken';
}

return inner.send(request);
}
}
1 change: 0 additions & 1 deletion data/lib/api/user/user_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

import '../../converter/timestamp_json_converter.dart';

part 'user_models.freezed.dart';

Expand Down
Loading

0 comments on commit eafa672

Please sign in to comment.