Skip to content

Commit

Permalink
feat: setup api key for sudoku backend
Browse files Browse the repository at this point in the history
  • Loading branch information
thisissandipp committed Jul 25, 2024
1 parent 83e5161 commit d61b784
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
API_BASE_URL = <API_BASE_URL>
API_KEY = <YOUR_API_KEY>
6 changes: 6 additions & 0 deletions lib/api/dio/sudoku_dio_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:sudoku/api/api.dart';
import 'package:sudoku/api/dtos/dtos.dart';
import 'package:sudoku/env/env.dart';
import 'package:sudoku/models/difficulty.dart';
import 'package:sudoku/models/sudoku.dart';

Expand All @@ -20,6 +21,10 @@ class SudokuDioClient extends SudokuAPI {
@visibleForTesting
final Dio dioClient;

Map<String, String> get _headers => {
'x-api-key': Env.apiKey,
};

/// HTTP request path for creating sudoku
static const createSudokuPath = '/createSudokuFlow';

Expand All @@ -33,6 +38,7 @@ class SudokuDioClient extends SudokuAPI {
).toJson(),
options: Options(
contentType: Headers.jsonContentType,
headers: _headers,
),
);

Expand Down
3 changes: 3 additions & 0 deletions lib/env/env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ part 'env.g.dart';
abstract class Env {
@EnviedField(varName: 'API_BASE_URL')
static final String apiBaseUrl = _Env.apiBaseUrl;

@EnviedField(varName: 'API_KEY')
static final String apiKey = _Env.apiKey;
}
2 changes: 1 addition & 1 deletion lib/storage/storage_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class StorageAPI {
Future<void> storePuzzle({required Puzzle puzzle});

/// Provides the stored puzzle.
///
///
/// Returns null, if there is no puzzle stored.
Future<Puzzle?> getPuzzle();

Expand Down
11 changes: 11 additions & 0 deletions test/env/env_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:sudoku/env/env.dart';

void main() {
group('Env', () {
test('sets up environment variables correctly', () {
expect(Env.apiBaseUrl, isA<String>());
expect(Env.apiKey, isA<String>());
});
});
}

0 comments on commit d61b784

Please sign in to comment.