Skip to content

Commit 7174273

Browse files
committed
Implemented Fallback Request & Synonyms
1 parent b70580d commit 7174273

15 files changed

+744
-136
lines changed

.github/workflows/dart.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# They are provided by a third-party and are governed by
33
# separate terms of service, privacy policy, and support
44
# documentation.
5-
name: CI
5+
name: Health
66
on:
77
push:
88
branches: [ master ]
@@ -63,17 +63,17 @@ jobs:
6363
run: dart test
6464

6565

66-
- name: Setup credentials
67-
run: |
68-
mkdir -p ~/.pub-cache
69-
cat <<EOF > ~/.pub-cache/credentials.json
70-
{
71-
"accessToken":"${{ secrets.OAUTH_ACCESS_TOKEN }}",
72-
"refreshToken":"${{ secrets.OAUTH_REFRESH_TOKEN }}",
73-
"tokenEndpoint":"https://accounts.google.com/o/oauth2/token",
74-
"scopes": [ "openid", "https://www.googleapis.com/auth/userinfo.email" ],
75-
"expiration": 1570721159347
76-
}
77-
EOF
78-
- name: Publish package
79-
run: pub publish -f
66+
# - name: Setup credentials
67+
# run: |
68+
# mkdir -p ~/.pub-cache
69+
# cat <<EOF > ~/.pub-cache/credentials.json
70+
# {
71+
# "accessToken":"${{ secrets.OAUTH_ACCESS_TOKEN }}",
72+
# "refreshToken":"${{ secrets.OAUTH_REFRESH_TOKEN }}",
73+
# "tokenEndpoint":"https://accounts.google.com/o/oauth2/token",
74+
# "scopes": [ "openid", "https://www.googleapis.com/auth/userinfo.email" ],
75+
# "expiration": 1570721159347
76+
# }
77+
# EOF
78+
# - name: Publish package
79+
# run: pub publish -f

CHANGELOG.md

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
1-
## [1.0.3] - Bug fixes
1+
# Changelog
2+
## 1.0.4
3+
### Implemented Fallback Request
4+
5+
- [Added] You can now worry less about retry action action on failure, as it just do it for you. In order to guarantee a very high availability, we implemented recommended retry strategy for all API calls on all your read and write actions. (Currently, fallback request is not supported for insight.)
6+
- [Added] `AlgoliaSynonymsReference` now easily set synonyms with just few lines of code
7+
```dart
8+
// single
9+
algolia.index('contacts').synonyms.save(AlgoliaSynonyms(
10+
objectID: '1',
11+
type: SynonymsType.synonym,
12+
synonyms: ['iphne', 'iphone', 'ipone'],
13+
forwardToReplicas: true,
14+
));
15+
16+
// batch
17+
algolia.index('contacts').synonyms.batch([
18+
AlgoliaSynonyms(
19+
objectID: '1',
20+
type: SynonymsType.synonym,
21+
synonyms: ['iphne', 'iphone', 'ipone'],
22+
forwardToReplicas: true,
23+
),
24+
]);
25+
```
26+
## 1.0.3
27+
### Bug fixes
228

329
- [Bug] queryId from QuerySnapshot was made nullable.
430

5-
## [1.0.2] - Added support for Facet Values & Insights
31+
## 1.0.2
32+
### Added support for Facet Values & Insights
633

734
- [Added] Facet values `AlgoliaFacetValueSnapshot`:
835
Now you can get list of all facet value to implement advance filtering options.
@@ -25,7 +52,8 @@
2552
- [Improved] Improved concurrency of snapshot interface by making constructor base multiple mapped value to a getter parameters.
2653

2754

28-
## [1.0.1] - Bug-fixes with improved debugging stability
55+
## 1.0.1
56+
### Bug-fixes with improved debugging stability
2957

3058
- [Bug] faulty assert resolved for checking empty values [#40](https://github.com/knoxpo/dart_algolia/issues/40)
3159
- [Bug] Fixed all enum valued query and setting methods.
@@ -39,11 +67,13 @@
3967
- [Added] Improved stability for debugging use ``.toString()`` to get working variables of the interface (applicable for all Algolia classes).
4068
- [Added] ``.toMap()`` to all data dictionary classes.
4169

42-
## [1.0.0+1] - Stable release with Null-Safety
70+
## 1.0.0+1
71+
### Stable release with Null-Safety
4372

4473
- Implemented ``analysis_options.yaml``
4574

46-
## [1.0.0] - Stable release with Null-Safety
75+
## 1.0.0
76+
### Stable release with Null-Safety
4777

4878
- [Bug] [#26](https://github.com/knoxpo/dart_algolia/issues/26)
4979
- [Added] Add support of ``Null-safety``
@@ -65,16 +95,19 @@
6595
- [Added] Add new error handling class ``AlgoliaError``
6696
- [Upgrade] Bumped up ``http`` version
6797

68-
## [0.1.7] - Bug fixes and added a new property
98+
## 0.1.7
99+
### Bug fixes and added a new property
69100

70101
- [Bug] [#14](https://github.com/knoxpo/dart_algolia/issues/14) Solved few health suggestion, to improve the health of the code.
71102
- [Added] Add support for ``facets_stats`` property returned by Algolia query
72103

73-
## [0.1.6+1] - Improve library health
104+
## 0.1.6+1
105+
### Improve library health
74106

75107
- [Bug] Solved few health suggestion, to improve the health of the code.
76108

77-
## [0.1.6] - Added Multi-Query
109+
## 0.1.6
110+
### Added Multi-Query
78111

79112
- [Added] PR implementation of ``multipleQueries``
80113

@@ -94,43 +127,51 @@
94127
await algolia.multipleQueries.addQueries([queryA, queryB]).getObjects();
95128
```
96129

97-
## [0.1.5] - Added New Functionalities
130+
## 0.1.5
131+
### Added New Functionalities
98132

99133
- [Bug] Solved a technical reported bug [#11](https://github.com/knoxpo/dart_algolia/issues/11)
100134
- [Added] Copy, Move Index functionalities.
101135
- [Added] PR implementation of ``replaceAllObjects()``
102136

103-
## [0.1.4+3] - Improve library health
137+
## 0.1.4+3
138+
### Improve library health
104139

105140
- [Bug] Solved few health suggestion, to improve the health of the code.
106141

107-
## [0.1.4+2] - Added few advance query references and solved bugs
142+
## 0.1.4+2
143+
### Added few advance query references and solved bugs
108144

109145
- [Bug] `.setFacetFilter(dynamic value)` can now accept String or List<String> value.
110146
- [Added] AttributeForDistinct (Advance)
111147
- [Added] Distinct (Advance)
112148
- [Added] GetRankingInfo (Advance)
113149
- [Added] ClickAnalytics (Advance)
114150

115-
## [0.1.4+1] - Added support facets
151+
## 0.1.4+1
152+
### Added support facets
116153

117154
- Added `facets` to ``AlgoliaQuerySnapshot`` to list facets name with hits count.
118155

119-
## [0.1.3+2] - Implementation & bug solved
156+
## 0.1.3+2
157+
### Implementation & bug solved
120158

121159
- highlightResult [Bug] (commit ref: 0d76d24fe8aa347a0933920afe5ded43bdcbd68b)
122160
- snippetResult [Implementation] (commit ref: 0d76d24fe8aa347a0933920afe5ded43bdcbd68b)
123161

124-
## [0.1.3+1] - Added support to manage index settings
162+
## 0.1.3+1
163+
### Added support to manage index settings
125164

126165
- Updated `example.dart`: Added index settings example.
127166
- Updated index `.setSettings()` response to `AlgoliaTask`.
128167

129-
## [0.1.3] - Added support to manage index settings
168+
## 0.1.3
169+
### Added support to manage index settings
130170

131171
- Added support to manage index settings (Get & Set), limited to 24 settings parameters, more to be added in newer releases.
132172

133-
## [0.1.2] - Added new query params
173+
## 0.1.2
174+
### Added new query params
134175

135176
- OptionalFilter (Filtering)
136177
- NumericFilter (Filtering)
@@ -151,11 +192,13 @@
151192
- DisableTypoToleranceOnWords (Typo)
152193
- SeparatorsToIndex (Typo)
153194

154-
## [0.1.1] - Added example
195+
## 0.1.1
196+
### Added example
155197

156198
- Bug fixes.
157199
- Removed Flutter direct dependency to support universal dart projects.
158200

159-
## [0.1.0] - Initial Release
201+
## 0.1.0
202+
### Initial Release
160203

161204
- Initial release.

lib/algolia.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ part 'src/index_settings.dart';
1616
part 'src/index_snapshot.dart';
1717
part 'src/object_reference.dart';
1818
part 'src/object_snapshot.dart';
19+
part 'src/synonyms_reference.dart';
1920
part 'src/query.dart';
2021
part 'src/query_snapshot.dart';
2122
part 'src/task.dart';
2223
part 'src/util/json_encode.dart';
24+
part 'src/util/enum_util.dart';

lib/src/algolia.dart

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
part of algolia;
22

3+
enum ApiRequestType {
4+
get,
5+
put,
6+
post,
7+
delete,
8+
patch,
9+
}
10+
311
class Algolia {
412
const Algolia.init({
513
required this.applicationId,
@@ -24,6 +32,10 @@ class Algolia {
2432
);
2533

2634
String get _host => 'https://$applicationId-dsn.algolia.net/1/';
35+
String get _hostWrite => 'https://$applicationId.algolia.net/1/';
36+
String get _hostFallback1 => 'https://$applicationId-1.algolianet.com/1/';
37+
String get _hostFallback2 => 'https://$applicationId-2.algolianet.com/1/';
38+
String get _hostFallback3 => 'https://$applicationId-3.algolianet.com/1/';
2739
String get _insightsHost => 'https://insights.algolia.io/1/';
2840

2941
Map<String, String> get _headers {
@@ -36,6 +48,87 @@ class Algolia {
3648
return map;
3749
}
3850

51+
Future<http.Response> _apiCall(ApiRequestType requestType, String url,
52+
{dynamic data}) async {
53+
// ignore: prefer_function_declarations_over_variables
54+
final action = (int retry) {
55+
String host = _hostWrite;
56+
if (requestType == ApiRequestType.get && retry == 0) {
57+
host = _host;
58+
} else if (retry == 1) {
59+
host = _hostFallback1;
60+
} else if (retry == 2) {
61+
host = _hostFallback2;
62+
} else if (retry == 3) {
63+
host = _hostFallback3;
64+
}
65+
switch (requestType) {
66+
case ApiRequestType.get:
67+
return http.get(
68+
Uri.parse('$host$url'),
69+
headers: _headers,
70+
);
71+
case ApiRequestType.post:
72+
return http.post(
73+
Uri.parse('$host$url'),
74+
headers: _headers,
75+
encoding: Encoding.getByName('utf-8'),
76+
body: data != null
77+
? utf8.encode(json.encode(data, toEncodable: jsonEncodeHelper))
78+
: null,
79+
);
80+
case ApiRequestType.put:
81+
return http.put(
82+
Uri.parse('$host$url'),
83+
headers: _headers,
84+
encoding: Encoding.getByName('utf-8'),
85+
body: data != null
86+
? utf8.encode(json.encode(data, toEncodable: jsonEncodeHelper))
87+
: null,
88+
);
89+
case ApiRequestType.patch:
90+
return http.patch(
91+
Uri.parse('$host$url'),
92+
headers: _headers,
93+
encoding: Encoding.getByName('utf-8'),
94+
body: data != null
95+
? utf8.encode(json.encode(data, toEncodable: jsonEncodeHelper))
96+
: null,
97+
);
98+
case ApiRequestType.delete:
99+
return http.delete(
100+
Uri.parse('$host$url'),
101+
headers: _headers,
102+
encoding: Encoding.getByName('utf-8'),
103+
body: data != null
104+
? utf8.encode(json.encode(data, toEncodable: jsonEncodeHelper))
105+
: null,
106+
);
107+
}
108+
};
109+
try {
110+
var response = await action(0);
111+
return json.decode(response.body);
112+
} catch (error) {
113+
try {
114+
var response = await action(1);
115+
return response;
116+
} catch (error) {
117+
try {
118+
var response = await action(2);
119+
return response;
120+
} catch (error) {
121+
try {
122+
var response = await action(3);
123+
return response;
124+
} catch (error) {
125+
throw {'error': error};
126+
}
127+
}
128+
}
129+
}
130+
}
131+
39132
Algolia setHeader(String key, String value) {
40133
var map = extraHeaders;
41134
map[key] = value;
@@ -54,10 +147,9 @@ class Algolia {
54147
AlgoliaMultiIndexesReference._(this);
55148

56149
Future<AlgoliaIndexesSnapshot> getIndices() async {
57-
var _url = '${_host}indexes';
58-
var response = await http.get(
59-
Uri.parse(_url),
60-
headers: _headers,
150+
var response = await _apiCall(
151+
ApiRequestType.get,
152+
'indexes',
61153
);
62154
Map<String, dynamic> body = json.decode(response.body);
63155

lib/src/batch.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,10 @@ class AlgoliaBatch {
6363

6464
var actions = _actions.map((a) => a.toMap()).toList();
6565

66-
var url = '${algolia._host}indexes/$_index/batch';
67-
68-
var response = await http.post(
69-
Uri.parse(url),
70-
headers: algolia._headers,
71-
body: utf8.encode(json
72-
.encode({'requests': actions}, toEncodable: jsonEncodeHelper)),
73-
encoding: Encoding.getByName('utf-8'),
66+
var response = await algolia._apiCall(
67+
ApiRequestType.post,
68+
'indexes/$_index/batch',
69+
data: {'requests': actions},
7470
);
7571

7672
Map<String, dynamic> body = json.decode(response.body);

lib/src/event_snapshot.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension AlgoliaEventTypeExtention on AlgoliaEventType {
1515
///
1616
/// Initital implemention by [@algirdasmac](https://github.com/algirdasmac) committed on 15 Jun
1717
/// [PR](https://github.com/knoxpo/dart_algolia/pull/56/commits/8dc068ed16f7cf0c6747ec28d0e17fcf7b433f7f)
18-
///
18+
///
1919
class AlgoliaEvent {
2020
AlgoliaEvent({
2121
required this.eventType,

0 commit comments

Comments
 (0)