Skip to content

Commit

Permalink
Implemented Fallback Request & Synonyms
Browse files Browse the repository at this point in the history
  • Loading branch information
nhathiwala committed Nov 18, 2021
1 parent b70580d commit 7174273
Show file tree
Hide file tree
Showing 15 changed files with 744 additions and 136 deletions.
30 changes: 15 additions & 15 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: CI
name: Health
on:
push:
branches: [ master ]
Expand Down Expand Up @@ -63,17 +63,17 @@ jobs:
run: dart test


- name: Setup credentials
run: |
mkdir -p ~/.pub-cache
cat <<EOF > ~/.pub-cache/credentials.json
{
"accessToken":"${{ secrets.OAUTH_ACCESS_TOKEN }}",
"refreshToken":"${{ secrets.OAUTH_REFRESH_TOKEN }}",
"tokenEndpoint":"https://accounts.google.com/o/oauth2/token",
"scopes": [ "openid", "https://www.googleapis.com/auth/userinfo.email" ],
"expiration": 1570721159347
}
EOF
- name: Publish package
run: pub publish -f
# - name: Setup credentials
# run: |
# mkdir -p ~/.pub-cache
# cat <<EOF > ~/.pub-cache/credentials.json
# {
# "accessToken":"${{ secrets.OAUTH_ACCESS_TOKEN }}",
# "refreshToken":"${{ secrets.OAUTH_REFRESH_TOKEN }}",
# "tokenEndpoint":"https://accounts.google.com/o/oauth2/token",
# "scopes": [ "openid", "https://www.googleapis.com/auth/userinfo.email" ],
# "expiration": 1570721159347
# }
# EOF
# - name: Publish package
# run: pub publish -f
79 changes: 61 additions & 18 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
## [1.0.3] - Bug fixes
# Changelog
## 1.0.4
### Implemented Fallback Request

- [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.)
- [Added] `AlgoliaSynonymsReference` now easily set synonyms with just few lines of code
```dart
// single
algolia.index('contacts').synonyms.save(AlgoliaSynonyms(
objectID: '1',
type: SynonymsType.synonym,
synonyms: ['iphne', 'iphone', 'ipone'],
forwardToReplicas: true,
));
// batch
algolia.index('contacts').synonyms.batch([
AlgoliaSynonyms(
objectID: '1',
type: SynonymsType.synonym,
synonyms: ['iphne', 'iphone', 'ipone'],
forwardToReplicas: true,
),
]);
```
## 1.0.3
### Bug fixes

- [Bug] queryId from QuerySnapshot was made nullable.

## [1.0.2] - Added support for Facet Values & Insights
## 1.0.2
### Added support for Facet Values & Insights

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


## [1.0.1] - Bug-fixes with improved debugging stability
## 1.0.1
### Bug-fixes with improved debugging stability

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

## [1.0.0+1] - Stable release with Null-Safety
## 1.0.0+1
### Stable release with Null-Safety

- Implemented ``analysis_options.yaml``

## [1.0.0] - Stable release with Null-Safety
## 1.0.0
### Stable release with Null-Safety

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

## [0.1.7] - Bug fixes and added a new property
## 0.1.7
### Bug fixes and added a new property

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

## [0.1.6+1] - Improve library health
## 0.1.6+1
### Improve library health

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

## [0.1.6] - Added Multi-Query
## 0.1.6
### Added Multi-Query

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

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

## [0.1.5] - Added New Functionalities
## 0.1.5
### Added New Functionalities

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

## [0.1.4+3] - Improve library health
## 0.1.4+3
### Improve library health

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

## [0.1.4+2] - Added few advance query references and solved bugs
## 0.1.4+2
### Added few advance query references and solved bugs

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

## [0.1.4+1] - Added support facets
## 0.1.4+1
### Added support facets

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

## [0.1.3+2] - Implementation & bug solved
## 0.1.3+2
### Implementation & bug solved

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

## [0.1.3+1] - Added support to manage index settings
## 0.1.3+1
### Added support to manage index settings

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

## [0.1.3] - Added support to manage index settings
## 0.1.3
### Added support to manage index settings

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

## [0.1.2] - Added new query params
## 0.1.2
### Added new query params

- OptionalFilter (Filtering)
- NumericFilter (Filtering)
Expand All @@ -151,11 +192,13 @@
- DisableTypoToleranceOnWords (Typo)
- SeparatorsToIndex (Typo)

## [0.1.1] - Added example
## 0.1.1
### Added example

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

## [0.1.0] - Initial Release
## 0.1.0
### Initial Release

- Initial release.
2 changes: 2 additions & 0 deletions lib/algolia.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ part 'src/index_settings.dart';
part 'src/index_snapshot.dart';
part 'src/object_reference.dart';
part 'src/object_snapshot.dart';
part 'src/synonyms_reference.dart';
part 'src/query.dart';
part 'src/query_snapshot.dart';
part 'src/task.dart';
part 'src/util/json_encode.dart';
part 'src/util/enum_util.dart';
100 changes: 96 additions & 4 deletions lib/src/algolia.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
part of algolia;

enum ApiRequestType {
get,
put,
post,
delete,
patch,
}

class Algolia {
const Algolia.init({
required this.applicationId,
Expand All @@ -24,6 +32,10 @@ class Algolia {
);

String get _host => 'https://$applicationId-dsn.algolia.net/1/';
String get _hostWrite => 'https://$applicationId.algolia.net/1/';
String get _hostFallback1 => 'https://$applicationId-1.algolianet.com/1/';
String get _hostFallback2 => 'https://$applicationId-2.algolianet.com/1/';
String get _hostFallback3 => 'https://$applicationId-3.algolianet.com/1/';
String get _insightsHost => 'https://insights.algolia.io/1/';

Map<String, String> get _headers {
Expand All @@ -36,6 +48,87 @@ class Algolia {
return map;
}

Future<http.Response> _apiCall(ApiRequestType requestType, String url,
{dynamic data}) async {
// ignore: prefer_function_declarations_over_variables
final action = (int retry) {
String host = _hostWrite;
if (requestType == ApiRequestType.get && retry == 0) {
host = _host;
} else if (retry == 1) {
host = _hostFallback1;
} else if (retry == 2) {
host = _hostFallback2;
} else if (retry == 3) {
host = _hostFallback3;
}
switch (requestType) {
case ApiRequestType.get:
return http.get(
Uri.parse('$host$url'),
headers: _headers,
);
case ApiRequestType.post:
return http.post(
Uri.parse('$host$url'),
headers: _headers,
encoding: Encoding.getByName('utf-8'),
body: data != null
? utf8.encode(json.encode(data, toEncodable: jsonEncodeHelper))
: null,
);
case ApiRequestType.put:
return http.put(
Uri.parse('$host$url'),
headers: _headers,
encoding: Encoding.getByName('utf-8'),
body: data != null
? utf8.encode(json.encode(data, toEncodable: jsonEncodeHelper))
: null,
);
case ApiRequestType.patch:
return http.patch(
Uri.parse('$host$url'),
headers: _headers,
encoding: Encoding.getByName('utf-8'),
body: data != null
? utf8.encode(json.encode(data, toEncodable: jsonEncodeHelper))
: null,
);
case ApiRequestType.delete:
return http.delete(
Uri.parse('$host$url'),
headers: _headers,
encoding: Encoding.getByName('utf-8'),
body: data != null
? utf8.encode(json.encode(data, toEncodable: jsonEncodeHelper))
: null,
);
}
};
try {
var response = await action(0);
return json.decode(response.body);
} catch (error) {
try {
var response = await action(1);
return response;
} catch (error) {
try {
var response = await action(2);
return response;
} catch (error) {
try {
var response = await action(3);
return response;
} catch (error) {
throw {'error': error};
}
}
}
}
}

Algolia setHeader(String key, String value) {
var map = extraHeaders;
map[key] = value;
Expand All @@ -54,10 +147,9 @@ class Algolia {
AlgoliaMultiIndexesReference._(this);

Future<AlgoliaIndexesSnapshot> getIndices() async {
var _url = '${_host}indexes';
var response = await http.get(
Uri.parse(_url),
headers: _headers,
var response = await _apiCall(
ApiRequestType.get,
'indexes',
);
Map<String, dynamic> body = json.decode(response.body);

Expand Down
12 changes: 4 additions & 8 deletions lib/src/batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,10 @@ class AlgoliaBatch {

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

var url = '${algolia._host}indexes/$_index/batch';

var response = await http.post(
Uri.parse(url),
headers: algolia._headers,
body: utf8.encode(json
.encode({'requests': actions}, toEncodable: jsonEncodeHelper)),
encoding: Encoding.getByName('utf-8'),
var response = await algolia._apiCall(
ApiRequestType.post,
'indexes/$_index/batch',
data: {'requests': actions},
);

Map<String, dynamic> body = json.decode(response.body);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/event_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension AlgoliaEventTypeExtention on AlgoliaEventType {
///
/// Initital implemention by [@algirdasmac](https://github.com/algirdasmac) committed on 15 Jun
/// [PR](https://github.com/knoxpo/dart_algolia/pull/56/commits/8dc068ed16f7cf0c6747ec28d0e17fcf7b433f7f)
///
///
class AlgoliaEvent {
AlgoliaEvent({
required this.eventType,
Expand Down
Loading

0 comments on commit 7174273

Please sign in to comment.