Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support api integration simulation #100

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions packages/rocket_client/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:developer';

import 'package:example/product_model.dart';
import 'package:example/simulation_data.dart';
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:rocket_client/rocket_client.dart';
Expand Down Expand Up @@ -66,6 +68,12 @@ class RocketClientExampleState extends State<RocketClientExample> {
},
child: const Text('Make Request'),
),
ElevatedButton(
onPressed: () {
_makeRequestSimulation(context, "products");
},
child: const Text('Make Request Simulation'),
),
ElevatedButton(
onPressed: () {
// pass wrong endpoint for produce error
Expand All @@ -79,6 +87,40 @@ class RocketClientExampleState extends State<RocketClientExample> {
);
}

Future<void> _makeRequestSimulation(
BuildContext context, String endpoint) async {
isLoading = true;
isFailed = false;
final product = Product();
setState(() {});
// Make a GET request simulation to the /products endpoint
await client.requestSimulation(endpoint,
model: product, target: ['products'], data: productsData);
isLoading = false;
setState(() {});
// Display the model content in a dialog
if (!isFailed) {
showDialog(
// ignore: use_build_context_synchronously
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Request Simulation'),
content: Text(json.encode(product.all)),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('OK'),
),
],
);
},
);
}
}

Future<void> _makeRequest(BuildContext context, String endpoint) async {
isLoading = true;
isFailed = false;
Expand All @@ -90,7 +132,6 @@ class RocketClientExampleState extends State<RocketClientExample> {
retryOptions: RetryOptions(
retries: 2,
retryWhen: (r) => r.statusCode != 200,

),
onError: (response, statusCode) {
isFailed = true;
Expand All @@ -107,8 +148,8 @@ class RocketClientExampleState extends State<RocketClientExample> {
setState(() {});
// Display the response in a dialog
if (!isFailed) {
// ignore: use_build_context_synchronously
showDialog(
// ignore: use_build_context_synchronously
context: context,
builder: (context) {
return AlertDialog(
Expand Down
Loading
Loading