Skip to content

Commit

Permalink
feat: add hint function environment variable (#566)
Browse files Browse the repository at this point in the history
* chore: add missing hint repository workflow

* fix: hint repository tests

* feat: add hint function url

* chore: update workflows to support hint function url env var
  • Loading branch information
jsgalarraga authored Jun 12, 2024
1 parent 897cfd3 commit 1a7bf1c
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 4 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/api_hint_repository.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: api_hint_repository

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
pull_request:
paths:
- "api/packages/hint_repository/**"
- ".github/workflows/api_hint_repository.yaml"
branches:
- main

jobs:
build:
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1
with:
dart_sdk: stable
working_directory: api/packages/hint_repository
2 changes: 2 additions & 0 deletions .github/workflows/deploy_api_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ env:
GAME_URL: "*"
INITIALS_BLACKLIST_ID: T1ilfCwjDpLS7iaFzenA
FB_STORAGE_BUCKET: io-crossword-dev.appspot.com
HINT_FUNCTION_URL: gethintkit-sea6y22h5q-uc.a.run.app

jobs:
deploy:
Expand Down Expand Up @@ -61,6 +62,7 @@ jobs:
GAME_URL=${{ env.GAME_URL }}
INITIALS_BLACKLIST_ID=${{ env.INITIALS_BLACKLIST_ID }}
FB_STORAGE_BUCKET=${{ env.FB_STORAGE_BUCKET }}
HINT_FUNCTION_URL=${{ env.HINT_FUNCTION_URL }}
- name: Show Output
run: echo ${{ steps.deploy.outputs.url }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy_api_prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ env:
GAME_URL: https://prod-9961b3776301c37db86d9038f.web.app
INITIALS_BLACKLIST_ID: CRORRJZBdf0rLLkHDr9j
FB_STORAGE_BUCKET: io-crossword.appspot.com
HINT_FUNCTION_URL: gethintkit-u3emptgwka-uc.a.run.app

jobs:
deploy:
Expand Down Expand Up @@ -54,6 +55,7 @@ jobs:
GAME_URL=${{ env.GAME_URL }}
INITIALS_BLACKLIST_ID=${{ env.INITIALS_BLACKLIST_ID }}
FB_STORAGE_BUCKET=${{ env.FB_STORAGE_BUCKET }}
HINT_FUNCTION_URL=${{ env.HINT_FUNCTION_URL }}
- name: Show Output
run: echo ${{ steps.deploy.outputs.url }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy_api_staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ env:
GAME_URL: https://stg-e7b2896c65fabc0480edc6c0e.web.app
INITIALS_BLACKLIST_ID: T1ilfCwjDpLS7iaFzenA
FB_STORAGE_BUCKET: io-crossword-dev.appspot.com
HINT_FUNCTION_URL: gethintkit-sea6y22h5q-uc.a.run.app

jobs:
deploy:
Expand Down Expand Up @@ -54,6 +55,7 @@ jobs:
GAME_URL=${{ env.GAME_URL }}
INITIALS_BLACKLIST_ID=${{ env.INITIALS_BLACKLIST_ID }}
FB_STORAGE_BUCKET=${{ env.FB_STORAGE_BUCKET }}
HINT_FUNCTION_URL=${{ env.HINT_FUNCTION_URL }}
- name: Show Output
run: echo ${{ steps.deploy.outputs.url }}
Expand Down
9 changes: 9 additions & 0 deletions api/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Future<void> init(InternetAddress ip, int port) async {
crosswordRepository = CrosswordRepository(dbClient: dbClient);
hintRepository = HintRepository(
dbClient: dbClient,
getHintUrl: _getHintUrl,
);
boardRenderer = const BoardRenderer();

Expand Down Expand Up @@ -86,4 +87,12 @@ String get _gameUrl {
return value;
}

String get _getHintUrl {
final value = Platform.environment['HINT_FUNCTION_URL'];
if (value == null) {
throw ArgumentError('HINT_FUNCTION_URL is required to run the API');
}
return value;
}

String get projectId => _appId;
5 changes: 4 additions & 1 deletion api/packages/hint_repository/lib/src/hint_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class HintRepository {
/// {@macro hint_repository}
HintRepository({
required DbClient dbClient,
required String getHintUrl,
Dio? httpClient,
}) : _dbClient = dbClient,
_getHintUrl = getHintUrl,
_httpClient = httpClient ?? Dio();

final DbClient _dbClient;
final String _getHintUrl;
final Dio _httpClient;

static const _answersCollection = 'answers';
Expand Down Expand Up @@ -70,7 +73,7 @@ class HintRepository {
required String userToken,
}) async {
try {
final url = Uri.https('gethintkit-sea6y22h5q-uc.a.run.app');
final url = Uri.https(_getHintUrl);
final body = {
'data': {
'word': wordAnswer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void main() {
httpClient = _MockHttpClient();
hintRepository = HintRepository(
dbClient: dbClient,
getHintUrl: 'example.com',
httpClient: httpClient,
);
});
Expand All @@ -34,6 +35,7 @@ void main() {
expect(
HintRepository(
dbClient: dbClient,
getHintUrl: 'example.com',
),
isNotNull,
);
Expand Down Expand Up @@ -218,7 +220,7 @@ void main() {

group('getPreviousHints', () {
test('returns an empty list when no hints are found', () async {
when(() => dbClient.getById('answers2/wordId/hints', 'userId'))
when(() => dbClient.getById('answers/wordId/hints', 'userId'))
.thenAnswer((_) async => null);

final hints = await hintRepository.getPreviousHints(
Expand All @@ -230,7 +232,7 @@ void main() {
});

test('returns a list of hints', () async {
when(() => dbClient.getById('answers2/wordId/hints', 'userId'))
when(() => dbClient.getById('answers/wordId/hints', 'userId'))
.thenAnswer(
(_) async => DbEntityRecord(
id: 'userId',
Expand All @@ -239,6 +241,7 @@ void main() {
{
'question': 'question',
'response': 'yes',
'readableResponse': 'Yes, that is correct!',
},
],
},
Expand Down Expand Up @@ -285,7 +288,7 @@ void main() {

verify(
() => dbClient.set(
'answers2/wordId/hints',
'answers/wordId/hints',
DbEntityRecord(
id: 'userId',
data: const {
Expand Down
1 change: 1 addition & 0 deletions scripts/start_local_api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export GAME_URL=http://localhost:24514
export USE_EMULATOR=false
export INITIALS_BLACKLIST_ID=T1ilfCwjDpLS7iaFzenA
export FB_STORAGE_BUCKET=io-crossword-dev.appspot.com
export HINT_FUNCTION_URL=gethintkit-sea6y22h5q-uc.a.run.app

echo ' ######################## '
echo ' ## Starting dart frog ## '
Expand Down

0 comments on commit 1a7bf1c

Please sign in to comment.