Skip to content

Commit ca3390e

Browse files
committed
feat: Add basic services implementing Dio and Hive.
1 parent b0a3743 commit ca3390e

8 files changed

Lines changed: 175 additions & 0 deletions

File tree

lib/data/config/config.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
class Environment {
2+
/// URL that is common to most outgoing requests.
3+
static const String baseUrl = String.fromEnvironment(
4+
'BASE_URL',
5+
defaultValue: 'https://codephile.mdg.iitr.ac.in/v1/',
6+
);
7+
8+
/// Project identifier for Sentry error logging.
29
static const String sentryDSN = String.fromEnvironment('SENTRY_DSN');
310
}

lib/data/constants/strings.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
class AppStrings {
22
static const String hiveBoxName = 'app_box';
3+
4+
static const String authTokenKey = 'auth_token';
35
}

lib/data/services/local/SAMPLE

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'package:hive_flutter/hive_flutter.dart';
2+
3+
import '../../constants/strings.dart';
4+
5+
class StorageService {
6+
/// Service initializer
7+
static void init() => _box = Hive.box(AppStrings.hiveBoxName);
8+
9+
// Data
10+
/// The Hive [Box] which contains all data stored by the app.
11+
static late final Box _box;
12+
13+
// CRUD methods
14+
static T? _get<T>(String key) => _box.get(key);
15+
16+
static void _set<T>(String key, T? value) => _box.put(key, value);
17+
18+
/// Remove a key from storage.
19+
static T? delete<T>(String key) {
20+
final T? value = _box.get(key);
21+
_box.delete(key);
22+
return value;
23+
}
24+
25+
/// Safely check whether a key exists in storage.
26+
/// Optionally check if the value is non-null.
27+
static bool exists(String key, {bool checkForNull = false}) {
28+
if (!_box.containsKey(key)) return false;
29+
return !checkForNull || _box.get(key) != null;
30+
}
31+
32+
/// Get a stream of [BoxEvent]s performed on a particular [key].
33+
static Stream<BoxEvent> stream(String key) => _box.watch(key: key);
34+
35+
// Specific getters and setters
36+
/// Authorization token for API requests.
37+
static String? get authToken => _get<String>(AppStrings.authTokenKey);
38+
39+
/// Authorization token for API requests.
40+
static set authToken(String? token) =>
41+
_set<String>(AppStrings.authTokenKey, token);
42+
}

lib/data/services/remote/SAMPLE

Whitespace-only changes.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import 'package:dio/dio.dart';
2+
import 'package:flutter/foundation.dart';
3+
4+
import '../../config/config.dart';
5+
import '../local/storage_service.dart';
6+
7+
class ApiService {
8+
/// Service initializer
9+
static void init() => _channel = Dio();
10+
11+
// Data
12+
/// The [Dio] channel through which all requests will be routed.
13+
static late final Dio _channel;
14+
15+
/// Safe method to send GET request to an endpoint **below** [Environment.baseUrl].
16+
static Future<Map<String, dynamic>> get(
17+
String endpoint, {
18+
Map<String, dynamic>? query,
19+
}) async {
20+
Response? response;
21+
try {
22+
response = await _channel.get(
23+
Environment.baseUrl + endpoint,
24+
queryParameters: query,
25+
options: Options(
26+
validateStatus: (status) {
27+
return status! < 500;
28+
},
29+
headers: {'authorization': StorageService.authToken},
30+
),
31+
);
32+
return {
33+
'status_code': response.statusCode ?? 0,
34+
'data': response.data ?? 'null',
35+
};
36+
} on Exception catch (exception, stacktrace) {
37+
debugPrint(
38+
'ERROR: Failed during a GET request.\n'
39+
'Endpoint: $endpoint\nQuery: $query\n'
40+
'Exception: $exception\nStacktrace: $stacktrace',
41+
);
42+
}
43+
return {
44+
'status_code': response?.statusCode ?? 0,
45+
'data': response?.data ?? 'null',
46+
};
47+
}
48+
49+
/// Safe method to send POST request to an endpoint **below** [Environment.baseUrl].
50+
static Future<Map<String, dynamic>> post(
51+
String endpoint, {
52+
required Map<String, dynamic> data,
53+
}) async {
54+
Response? response;
55+
try {
56+
response = await _channel.post(
57+
Environment.baseUrl + endpoint,
58+
data: data,
59+
options: Options(
60+
validateStatus: (status) {
61+
return status! < 500;
62+
},
63+
headers: {'authorization': StorageService.authToken},
64+
),
65+
);
66+
return {
67+
'status_code': response.statusCode ?? 0,
68+
'data': response.data ?? 'null',
69+
};
70+
} on Exception catch (exception, stacktrace) {
71+
debugPrint(
72+
'ERROR: Failed during a POST request.\n'
73+
'Endpoint: $endpoint\nData: $data\n'
74+
'Exception: $exception\nStacktrace: $stacktrace',
75+
);
76+
}
77+
return {
78+
'status_code': response?.statusCode ?? 0,
79+
'data': response?.data ?? 'null',
80+
};
81+
}
82+
83+
/// Safe method to send PUT request to an endpoint **below** [Environment.baseUrl].
84+
static Future<Map<String, dynamic>> put(
85+
String endpoint, {
86+
required Map<String, dynamic> data,
87+
}) async {
88+
Response? response;
89+
try {
90+
response = await _channel.put(
91+
Environment.baseUrl + endpoint,
92+
data: data,
93+
options: Options(
94+
validateStatus: (status) {
95+
return status! < 500;
96+
},
97+
headers: {'authorization': StorageService.authToken},
98+
),
99+
);
100+
return {
101+
'status_code': response.statusCode ?? 0,
102+
'data': response.data ?? 'null',
103+
};
104+
} on Exception catch (exception, stacktrace) {
105+
debugPrint(
106+
'ERROR: Failed during a POST request.\n'
107+
'Endpoint: $endpoint\nData: $data\n'
108+
'Exception: $exception\nStacktrace: $stacktrace',
109+
);
110+
}
111+
return {
112+
'status_code': response?.statusCode ?? 0,
113+
'data': response?.data ?? 'null',
114+
};
115+
}
116+
}

pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ packages:
169169
url: "https://pub.dartlang.org"
170170
source: hosted
171171
version: "2.2.1"
172+
dio:
173+
dependency: "direct main"
174+
description:
175+
name: dio
176+
url: "https://pub.dartlang.org"
177+
source: hosted
178+
version: "4.0.4"
172179
fake_async:
173180
dependency: transitive
174181
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ environment:
99
sdk: ">=2.12.0 <3.0.0"
1010

1111
dependencies:
12+
dio: ^4.0.4
1213
firebase_core: ^1.11.0
1314
firebase_crashlytics: ^2.4.5
1415
firebase_messaging: ^11.2.5

0 commit comments

Comments
 (0)