Skip to content

Commit 273079f

Browse files
authored
Merge pull request #27 from headlines-toolkit/refactor_api
Refactor api
2 parents 35d3f02 + 14b64af commit 273079f

File tree

2 files changed

+18
-54
lines changed

2 files changed

+18
-54
lines changed

lib/src/config/app_dependencies.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,10 @@ class AppDependencies {
170170
// Configure the HTTP client for SendGrid.
171171
// The HtHttpClient's AuthInterceptor will use the tokenProvider to add
172172
// the 'Authorization: Bearer <SENDGRID_API_KEY>' header.
173+
final sendGridApiBase =
174+
EnvironmentConfig.sendGridApiUrl ?? 'https://api.sendgrid.com';
173175
final sendGridHttpClient = HtHttpClient(
174-
baseUrl:
175-
EnvironmentConfig.sendGridApiUrl ?? 'https://api.sendgrid.com/v3',
176+
baseUrl: '$sendGridApiBase/v3',
176177
tokenProvider: () async => EnvironmentConfig.sendGridApiKey,
177178
isWeb: false, // This is a server-side implementation.
178179
logger: Logger('HtEmailSendgridClient'),

lib/src/config/environment_config.dart

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -60,41 +60,30 @@ abstract final class EnvironmentConfig {
6060
return env; // Return even if fallback
6161
}
6262

63+
static String _getRequiredEnv(String key) {
64+
final value = _env[key];
65+
if (value == null || value.isEmpty) {
66+
_log.severe('$key not found in environment variables.');
67+
throw StateError('FATAL: $key environment variable is not set.');
68+
}
69+
return value;
70+
}
71+
6372
/// Retrieves the database connection URI from the environment.
6473
///
6574
/// The value is read from the `DATABASE_URL` environment variable.
6675
///
6776
/// Throws a [StateError] if the `DATABASE_URL` environment variable is not
6877
/// set, as the application cannot function without it.
69-
static String get databaseUrl {
70-
final dbUrl = _env['DATABASE_URL'];
71-
if (dbUrl == null || dbUrl.isEmpty) {
72-
_log.severe('DATABASE_URL not found in environment variables.');
73-
throw StateError(
74-
'FATAL: DATABASE_URL environment variable is not set. '
75-
'The application cannot start without a database connection.',
76-
);
77-
}
78-
return dbUrl;
79-
}
78+
static String get databaseUrl => _getRequiredEnv('DATABASE_URL');
8079

8180
/// Retrieves the JWT secret key from the environment.
8281
///
8382
/// The value is read from the `JWT_SECRET_KEY` environment variable.
8483
///
8584
/// Throws a [StateError] if the `JWT_SECRET_KEY` environment variable is not
8685
/// set, as the application cannot function without it.
87-
static String get jwtSecretKey {
88-
final jwtKey = _env['JWT_SECRET_KEY'];
89-
if (jwtKey == null || jwtKey.isEmpty) {
90-
_log.severe('JWT_SECRET_KEY not found in environment variables.');
91-
throw StateError(
92-
'FATAL: JWT_SECRET_KEY environment variable is not set. '
93-
'The application cannot start without a JWT secret.',
94-
);
95-
}
96-
return jwtKey;
97-
}
86+
static String get jwtSecretKey => _getRequiredEnv('JWT_SECRET_KEY');
9887

9988
/// Retrieves the current environment mode (e.g., 'development').
10089
///
@@ -127,44 +116,18 @@ abstract final class EnvironmentConfig {
127116
/// Retrieves the SendGrid API key from the environment.
128117
///
129118
/// Throws a [StateError] if the `SENDGRID_API_KEY` is not set.
130-
static String get sendGridApiKey {
131-
final apiKey = _env['SENDGRID_API_KEY'];
132-
if (apiKey == null || apiKey.isEmpty) {
133-
_log.severe('SENDGRID_API_KEY not found in environment variables.');
134-
throw StateError(
135-
'FATAL: SENDGRID_API_KEY environment variable is not set.',
136-
);
137-
}
138-
return apiKey;
139-
}
119+
static String get sendGridApiKey => _getRequiredEnv('SENDGRID_API_KEY');
140120

141121
/// Retrieves the default sender email from the environment.
142122
///
143123
/// Throws a [StateError] if the `DEFAULT_SENDER_EMAIL` is not set.
144-
static String get defaultSenderEmail {
145-
final email = _env['DEFAULT_SENDER_EMAIL'];
146-
if (email == null || email.isEmpty) {
147-
_log.severe('DEFAULT_SENDER_EMAIL not found in environment variables.');
148-
throw StateError(
149-
'FATAL: DEFAULT_SENDER_EMAIL environment variable is not set.',
150-
);
151-
}
152-
return email;
153-
}
124+
static String get defaultSenderEmail =>
125+
_getRequiredEnv('DEFAULT_SENDER_EMAIL');
154126

155127
/// Retrieves the SendGrid OTP template ID from the environment.
156128
///
157129
/// Throws a [StateError] if the `OTP_TEMPLATE_ID` is not set.
158-
static String get otpTemplateId {
159-
final templateId = _env['OTP_TEMPLATE_ID'];
160-
if (templateId == null || templateId.isEmpty) {
161-
_log.severe('OTP_TEMPLATE_ID not found in environment variables.');
162-
throw StateError(
163-
'FATAL: OTP_TEMPLATE_ID environment variable is not set.',
164-
);
165-
}
166-
return templateId;
167-
}
130+
static String get otpTemplateId => _getRequiredEnv('OTP_TEMPLATE_ID');
168131

169132
/// Retrieves the SendGrid API URL from the environment, if provided.
170133
///

0 commit comments

Comments
 (0)