resty

Creator: coderz1093

Last updated:

0 purchases

resty Image
resty Images
Add to Cart

Description:

resty

A simple, light and useful http wrapper inspired from zttp for dart and flutter.
Features #

📦 Lightweight.
🚀 Simple use.
✅ Support both dart & flutter.

Usage #
Request #
// https://www.example.com:3000/api/v1/
final resty = Resty(
/// true:https (default if certificate is passed) | false:http (default)
secure: true,
/// load certificate
certificate: await rootBundle.load("assets/certificate.crt"),
/// base-url without http
host: 'www.example.com',
/// secure:443 | not-secure:80 (default)
port: 3000,
/// base path
path: 'api',
/// you can override later in any request
version: 'v1',
/// use to set accept & content-type to `application/json`
json: true,
/// use to enable logger
logger: true,
/// use to send shared headers
headers: {
'foo': 'bar',
},
/// use to specify timeout (default: 30 sec)
timeout: Duration(seconds: 60),
/// listen to request, response and also errors
observers: [
Observer(
onRequest: (request) async {
/// always pass your token before sending request to server
request.headers.add('Authorization', 'Bearer ${YOUR_TOKEN_HERE}');
},
onResponse: (response) {
// TODO
},
onError: (exception) {
// TODO
},
),
],
);

// It as simple as
// https://www.example.com:3000/api/v1/resources
resty.get('resources', query: {'foo': 'bar'});

// you can override your api version whenever you want
// https://www.example.com:3000/api/v2/resources
resty.get('resources', version: 'v2');
copied to clipboard
Response #
final response = await resty.get('resources');

// status code between 200 & 300
response.isOk | response.isSuccess

// status code between 300 & 400
response.isRedirect

// status code between 400 & 500
response.isClientError

// status code >= 500
response.isServerError

// get body
response.body

// auto convert body to json
response.json
copied to clipboard
Progress #
await resty.get('resources', progress: (progress) {
print(progress.sent); // Sent bytes
print(progress.total); // Total bytes
print(progress.percentage); // Percentage value helpful when showing loading indicator
});
copied to clipboard
Authentication #
Basic Auth #
final resty = Resty(
// ...
secure: true,
auth: HttpClientBasicCredentials(
'postman', // username
'password', // password
),
// ...
);
copied to clipboard
Digest Auth #
final resty = Resty(
// ...
secure: true,
auth: HttpClientDigestCredentials(
'postman', // username
'password', // password
),
// ...
);
copied to clipboard
Resty ❤️ Flutter #
You can use any service locator to access resty globally.
For this example we're using get_it.
// models/todo.dart
class Todo {
final int id;
final String title;
final bool compeleted;

Todo.fromMap(Map<String, dynamic> item)
: id = item['id'],
title = item['title'],
compeleted = item['completed'];

static List<Todo> fromList(List<dynamic> items) =>
items.map((item) => Todo.fromMap(item)).toList();
}

// services/todo_service.dart
class TodoService {
final resty = locator<Resty>();

Future<List<Todo>> get all async {
final response = await resty.get('todos');

if (response.isOk) {
return Todo.fromList(response.json);
}

return [];
}
}

// locator.dart
final locator = GetIt.instance;

void setupLocator() {
final resty = const Resty(
secure: true,
host: 'jsonplaceholder.typicode.com',
logger: true,
);

locator.registerLazySingleton(() => resty);
locator.registerLazySingleton(() => TodoService());
}
copied to clipboard
Then simply use
setupLocator();
final todos = locator<TodoService>().all;
print('${todos.length} todos found!');
copied to clipboard
Check full example

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.

Related Products

More From This Creator