Skip to content

Commit

Permalink
Merge pull request #6 from yeikel16/api_client_interface
Browse files Browse the repository at this point in the history
refactor: support for more than one http client
  • Loading branch information
yeikel16 authored Jan 27, 2022
2 parents bd1f039 + 66e0f44 commit c73d79b
Show file tree
Hide file tree
Showing 47 changed files with 2,001 additions and 342 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
.packages
build/
pubspec.lock
coverage/
coverage/
packages/http_client_deta_api/test/.test_coverage.dart
26 changes: 0 additions & 26 deletions lib/src/exceptions.dart

This file was deleted.

8 changes: 8 additions & 0 deletions packages/client_deta_api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# See https://www.dartlang.org/guides/libraries/private-files

# Files and directories created by pub
.dart_tool/
.packages
build/
coverage/
pubspec.lock
14 changes: 14 additions & 0 deletions packages/client_deta_api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# CHANGELOG

## 0.0.1

* docs: emprube documentations

## 0.0.2-dev.1

* refactor: implement `Exception` in `DetaError`.
* refactor: add named parameters in `DetaReponse` constructor.

## 0.0.2

* docs: update **README.md**
8 changes: 8 additions & 0 deletions packages/client_deta_api/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# MIT License

Copyright (c) 2022 Yeikel Uriarte Arteaga
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 changes: 20 additions & 0 deletions packages/client_deta_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# client_deta_api

[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
[![License: MIT][license_badge]][license_link]

Basic interface of requests from a client to interact with the Deta API.

## This package is implemented in

* [dio_client_deta_api](https://pub.dev/packages/dio_client_deta_api) used by the HTTP client of the [dio](https://pub.dev/packages/dio) package
* [http_client_deta_api](https://pub.dev/packages/http_client_deta_api) used by the HTTP client of the [http](https://pub.dev/packages/http) package

---

A Very Good Project created by Very Good CLI.

[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
[license_link]: https://opensource.org/licenses/MIT
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis
File renamed without changes.
5 changes: 5 additions & 0 deletions packages/client_deta_api/lib/client_deta_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library client_deta_api;

export 'src/client_deta_api.dart';
export 'src/deta_error.dart';
export 'src/deta_response.dart';
44 changes: 44 additions & 0 deletions packages/client_deta_api/lib/src/client_deta_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:client_deta_api/src/deta_response.dart';

/// {@template client_deta_api}
/// Basic interface of requests from a client to interact with the Deta API.
/// {@endtemplate}
abstract class ClientDetaApi {
/// {@endtemplate}
/// {@macro client_deta_api}
const ClientDetaApi();

/// Make http `GET` request.
Future<DetaResponse<T>> get<T>(
Uri url, {
Map<String, String> headers = const {},
});

/// Make http `POST` request.
Future<DetaResponse<T>> post<T>(
Uri url, {
Map<String, String> headers = const {},
Object? data,
});

/// Make http `PUT` request.
Future<DetaResponse<T>> put<T>(
Uri url, {
Map<String, String> headers = const {},
Object? data,
});

/// Make http `DELETE` request.
Future<DetaResponse<T>> delete<T>(
Uri url, {
Object? data,
Map<String, String> headers = const {},
});

/// Make http `PATCH` request.
Future<DetaResponse<T>> patch<T>(
Uri url, {
Map<String, String> headers = const {},
Object? data,
});
}
20 changes: 20 additions & 0 deletions packages/client_deta_api/lib/src/deta_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:client_deta_api/src/deta_response.dart';

///{@template deta_error}
/// Class for error in the request.
/// {@endtemplate}
class DetaError implements Exception {
/// {@macro deta_error}
DetaError({this.response});

/// Response info, it may be `null` if the request can't reach to
/// the http server, for example, occurring a dns error,
/// network is not available.
DetaResponse? response;

@override
String toString() {
return 'DetaError(response: Response(body: ${response?.body}, '
'statusCode: ${response?.statusCode}))';
}
}
13 changes: 13 additions & 0 deletions packages/client_deta_api/lib/src/deta_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// {@template deta_response}
/// Contains the information from the http response.
/// {@endtemplate}
class DetaResponse<T> {
/// {@macro deta_response}
DetaResponse({this.body, this.statusCode});

/// Response body.
final T? body;

/// Http status code.
final int? statusCode;
}
11 changes: 11 additions & 0 deletions packages/client_deta_api/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: client_deta_api
description: Basic interface of requests from a client to interact with the Deta API.
version: 0.0.2
repository: https://github.com/yeikel16/deta-dart
homepage: https://github.com/yeikel16/deta-dart

environment:
sdk: ">=2.14.0 <3.0.0"

dev_dependencies:
very_good_analysis: ^2.4.0
8 changes: 8 additions & 0 deletions packages/deta/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# See https://www.dartlang.org/guides/libraries/private-files

# Files and directories created by pub
.dart_tool/
.packages
build/
pubspec.lock
coverage/
File renamed without changes.
8 changes: 8 additions & 0 deletions packages/deta/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# MIT License

Copyright (c) 2022 Yeikel Uriarte Arteaga
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
173 changes: 173 additions & 0 deletions packages/deta/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# Deta

[![codecov][coverage_badge]][codecov_link] [![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link] [![pub package][pub_badge]][pub_link] [![License: MIT][license_badge]][license_link]

---

A Dart package to interact with the [HTTP API](https://docs.deta.sh/) of the free services of the [Deta](https://deta.sh/) plataform.

## Getting Started

Check the full example [here](https://github.com/yeikel16/deta-dart/blob/main/example/deta_base_example.dart).

### Install

Add to dependencies on `pubspec.yaml`:

```yaml
dependencies:
deta: <version>
```
### Usege
We declare class **Deta**, which receives our private credential as a parameter.
The `client` parameter can receive two different implementations `DioClientDetaApi` or `HttpClientDetaApi`, you need to add to its dependencies the one you prefer.

* [DioClientDetaApi](https://pub.dev/packages/dio_client_deta_api) used by the HTTP client of the [dio](https://pub.dev/packages/dio) package.
* [HttpClientDetaApi](https://pub.dev/packages/http_client_deta_api) used by the HTTP client of the [http](https://pub.dev/packages/http) package.

```dart
final deta = Deta(projectKey: 'projectKey', client: DioClientDetaApi(dio: Dio()));
```

🚨 **WARNING** 🚨
Your `projectKey` is confidential and meant to be used by you. Anyone who has your project key can access your database. Please, do not share it or commit it in your code.

### DetaBase

`DetaBase` is a fully-managed, fast, scalable and secure NoSQL database with a focus on end-user simplicity.

We define our `DetaBase`, with witch we are going to interact through the `base` method that receives the name of the `DetaBase` as a parameter. In case not exist it will be created instantly on first use, you can create as many `DetaBase` as you need.

A `DetaBase` instance is a collection of data, not unlike a Key-Value store, a MongoDB collection or a PostgreSQL/MySQL table.

```dart
final detabase = deta.base('lenguages');
```

#### Methods

##### put

Save an item. It will update an item if the key already exists.

```dart
await detabase.put({
'key': 'dart-g',
'name': 'Dart',
'description':
'Dart is a general-purpose programming language that adds strong '
'support for modularity, co-variant return types, and a strong '
'emphasis on type safety.',
'creator': 'Google',
'year': 2012,
});
```

##### putMany

Saves a list the elements, this list can only have a maximum of 20 element.

```dart
await detabase.putMany(
items: lenguages.map((lenguage) => lenguage.toJson()).toList(),
);
```

##### insert

Saves an element like `put`, with the difference that if this element exists in `DetaBase` it will throw an `DetaObjectException`. The `key` required that are part of the elemet to be saved.

```dart
await detabase.insert({
'key': 'r-f',
'name': 'R',
'description': 'R is a programming language and software environment '
'for statistical computing and graphics.',
'creator': 'R Foundation',
'year': 1995,
});
```

##### update

Update the element from the supplied `key`, you have to pass the whole element, both the updated and unchanged parameters.

```dart
await detabase.update(
key: 'ruby-ym',
item: <String, dynamic>{
'key': 'ruby-ym',
'name': 'Ruby',
'description': 'Ruby is a dynamic, open source, general-purpose '
'programming language with a focus on simplicity and productivity.',
'creator': 'Yukihiro Matsumoto',
'year': 1995,
},
);
```

##### get

Get a spesific element form the key.

```dart
final item = await detabase.get('dart-g');
```

##### delete

Delete a spesific element from the key.

```dart
final wasDeleted = await detabase.delete('ruby');
```

##### fetch

Return all saved items if no `query` is specified.

```dart
final all = await detabase.fetch();
```

Return all element that matched the indicated `query`.

```dart
final result = await detabase.fetch(
query: [DetaQuery('year').lessThanOrEqualTo(2000).and('name').prefix('C')],
);
```

---

## Running Tests 🧪

To run all unit tests use the following command:

```sh
flutter test --coverage --test-randomize-ordering-seed random
```

To view the generated coverage report you can use [coverde](https://pub.dev/packages/coverde).

```sh
# Generate Coverage Report
$ coverde report
```

---

A Very Good Project created by [Very Good CLI](https://github.com/VeryGoodOpenSource/very_good_cli).

[codecov_link]: https://codecov.io/gh/yeikel16/deta-dart
[coverage_badge]: https://codecov.io/gh/yeikel16/deta-dart/branch/main/graph/badge.svg
[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
[pub_badge]: https://img.shields.io/pub/v/deta.svg
[pub_link]: https://pub.dartlang.org/packages/deta
[license_link]: https://opensource.org/licenses/MIT
[logo]: https://docs.deta.sh/img/logo.svg
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis
1 change: 1 addition & 0 deletions packages/deta/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:very_good_analysis/analysis_options.2.4.0.yaml
Loading

0 comments on commit c73d79b

Please sign in to comment.