Skip to content

Commit

Permalink
feat: mark response data with cached flag
Browse files Browse the repository at this point in the history
  • Loading branch information
rIIh committed Nov 16, 2020
1 parent 023d267 commit 2b3ba4d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/oxford_dict.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ export 'src/entries/model/semantic_classes.dart';
export 'src/entries/model/sense.dart';
export 'src/entities/word.dart';
export 'src/lemmas/entity/lemmas.dart';
export 'src/lemmas/model/inflection_of_data.dart';

export 'src/common/exception/cache_exception.dart';
10 changes: 10 additions & 0 deletions lib/src/common/cache/map_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:chopper/chopper.dart';
import 'package:oxford_dictionary/src/common/exception/cache_exception.dart';
import 'package:oxford_dictionary/src/utils/io/cached_response.dart';

Response<T> mapCachedResponse<T>(Response<T> response) {
if (response.body == null && response.base is CachedResponse) {
throw CachedValueIsEmptyException();
}
return response;
}
3 changes: 3 additions & 0 deletions lib/src/common/exception/cache_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CachedValueIsEmptyException extends Error implements Exception {

}
3 changes: 2 additions & 1 deletion lib/src/entities/word.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class Word {
final Map<remote.LexicalCategory, List<Variant>> variants;
final Map<remote.LexicalCategory, List<String>> phrases;
final remote.DictionaryEntries rawResponse;
final bool isCached;

Word.fromEntry(remote.DictionaryEntries entry)
Word.fromEntry(remote.DictionaryEntries entry, [this.isCached])
: id = entry.id,
word = entry.word,
language = entry.results?.first?.language,
Expand Down
11 changes: 8 additions & 3 deletions lib/src/entries/service/entries_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:chopper/chopper.dart';
import 'package:oxford_dictionary/src/common/cache/map_response.dart';
import 'package:oxford_dictionary/src/entities/word.dart';
import 'package:oxford_dictionary/src/entries/model/dictionary_entry.dart';
import 'package:oxford_dictionary/src/utils/io/cached_response.dart';
Expand All @@ -25,8 +26,9 @@ class EntriesServiceImpl extends CachedEntriesService {
word,
)
.then(mapResponse)
.then(mapCachedResponse)
.then(
(value) => Word.fromEntry(value),
(response) => Word.fromEntry(response.body, response.base is CachedResponse),
);
}

Expand All @@ -43,11 +45,14 @@ class CachedEntriesService extends _$EntriesService {
return cache.get(key).then(
(value) => Response(
CachedResponse(),
DictionaryEntries.fromJson(value),
value != null ? DictionaryEntries.fromJson(value) : null,
),
);
}
final response = await super._search(language, word);
final response = await super._search(language, word).catchError((error) {
cache.put(key, null);
return error;
});
cache.put(key, response.body);
return response;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/lemmas/entity/lemmas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class Lemmas {
final String word;
final Map<LexicalCategory, List<InflectionOfData>> inflections;
final LemmaResponse rawResponse;
final bool isCached;

Lemmas.fromEntry(LemmaResponse lemmaResponse)
Lemmas.fromEntry(LemmaResponse lemmaResponse, [this.isCached])
: rawResponse = lemmaResponse,
word = lemmaResponse.results.firstWhere((element) => element.word != null).word,
inflections = Map.fromEntries(
Expand Down
11 changes: 8 additions & 3 deletions lib/src/lemmas/service/lemmas_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:chopper/chopper.dart';
import 'package:oxford_dictionary/src/common/cache/map_response.dart';
import 'package:oxford_dictionary/src/lemmas/entity/lemmas.dart';
import 'package:oxford_dictionary/src/lemmas/model/lemma_response.dart';
import 'package:oxford_dictionary/src/utils/io/cached_response.dart';
Expand All @@ -25,8 +26,9 @@ class LemmasServiceImpl extends CachedLemmasService {
word,
)
.then(mapResponse)
.then(mapCachedResponse)
.then(
(value) => Lemmas.fromEntry(value),
(response) => Lemmas.fromEntry(response.body, response.base is CachedResponse),
);
}

Expand All @@ -43,11 +45,14 @@ class CachedLemmasService extends _$LemmasService {
return cache.get(key).then(
(value) => Response(
CachedResponse(),
LemmaResponse.fromJson(value),
value != null ? LemmaResponse.fromJson(value) : null,
),
);
}
final response = await super._search(language, word);
final response = await super._search(language, word).catchError((error) {
cache.put(key, null);
return error;
});
cache.put(key, response.body);
return response;
}
Expand Down
7 changes: 3 additions & 4 deletions lib/src/utils/response.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'package:chopper/chopper.dart';

T mapResponse<T>(Response<T> response) {
Response<T> mapResponse<T>(Response<T> response) {
if (response.isSuccessful) {
return response.body;
} else {
throw RequestFailedException(response.statusCode);
return response;
}
throw RequestFailedException(response.statusCode);
}

class RequestFailedException implements Exception {
Expand Down

0 comments on commit 2b3ba4d

Please sign in to comment.