Skip to content

Commit

Permalink
refactor: examples/alice_http
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Jun 16, 2024
1 parent 14f7c83 commit 39506e5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 98 deletions.
22 changes: 9 additions & 13 deletions examples/alice_http/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
1include: package:very_good_analysis/analysis_options.yaml
analyzer:
exclude:
- "lib/generated_plugin_registrant.dart"
- "**.g.dart"
- "**.mocks.dart"
- "**.gen.dart"

include: package:flutter_lints/flutter.yaml

linter:
rules:
public_member_api_docs: false
flutter_style_todos: false
avoid_final_parameters: false
sort_constructors_first: false
avoid_function_literals_in_foreach_calls: false
avoid_positional_boolean_parameters: false
use_if_null_to_convert_nulls_to_bools: false
use_build_context_synchronously: false
prefer_constructors_over_static_methods: false
use_setters_to_change_properties: false
avoid_print: false
sort_pub_dependencies: false
#library_annotations: false
168 changes: 83 additions & 85 deletions examples/alice_http/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@

import 'package:alice/alice.dart';
import 'package:alice_http/alice_http_adapter.dart';
import 'package:alice_http/alice_http_extensions.dart';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());
void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
_MyAppState createState() => _MyAppState();
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
late Alice _alice;
late AliceHttpAdapter _aliceHttpAdapter;
late final AliceHttpAdapter _aliceHttpAdapter = AliceHttpAdapter();

@override
void initState() {
_alice = Alice(
showNotification: true,
showInspectorOnShake: true,
maxCallsCount: 1000,
);
_aliceHttpAdapter = AliceHttpAdapter();
_alice.addAdapter(_aliceHttpAdapter);

super.initState();
}
late final Alice _alice = Alice(
showNotification: true,
showInspectorOnShake: true,
maxCallsCount: 1000,
)..addAdapter(_aliceHttpAdapter);

@override
Widget build(BuildContext context) {

return MaterialApp(
navigatorKey: _alice.getNavigatorKey(),
debugShowCheckedModeBanner: false,
Expand All @@ -41,25 +32,33 @@ class _MyAppState extends State<MyApp> {
title: const Text('Alice + HTTP package - Example'),
),
body: Container(
padding: EdgeInsets.all(16),
padding: const EdgeInsets.all(16),
child: ListView(
children: [
const SizedBox(height: 8),
_getTextWidget(
'Welcome to example of Alice Http Inspector. Click buttons below to generate sample data.'),

const Text(
style: TextStyle(fontSize: 14),
'Welcome to example of Alice Http Inspector. '
'Click buttons below to generate sample data.',
),
ElevatedButton(
child: Text(
onPressed: _runHttpHttpRequests,
child: const Text(
'Run http/http HTTP Requests',
),
onPressed: _runHttpHttpRequests,
),

const SizedBox(height: 8),
const Text(
style: TextStyle(fontSize: 14),
'After clicking on buttons above, you should receive notification.'
' Click on it to show inspector. '
'You can also shake your device or click button below.',
),
ElevatedButton(
child: Text(
onPressed: _runHttpInspector,
child: const Text(
'Run HTTP Inspector',
),
onPressed: _runHttpInspector,
)
],
),
Expand All @@ -68,107 +67,106 @@ class _MyAppState extends State<MyApp> {
);
}

Widget _getTextWidget(String text) {
return Text(
text,
style: TextStyle(fontSize: 14),
textAlign: TextAlign.center,
);
}


void _runHttpHttpRequests() async {
Map<String, String> body = <String, String>{
final Map<String, String> body = {
'title': 'foo',
'body': 'bar',
'userId': '1'
};

http
.post(Uri.tryParse('https://jsonplaceholder.typicode.com/posts')!,
body: body)
.post(Uri.https('jsonplaceholder.typicode.com', '/posts'), body: body)
.interceptWithAlice(_aliceHttpAdapter, body: body);

http
.get(Uri.tryParse('https://jsonplaceholder.typicode.com/posts')!)
.get(Uri.https('jsonplaceholder.typicode.com', '/posts'))
.interceptWithAlice(_aliceHttpAdapter);

http
.put(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!,
body: body)
.put(Uri.https('jsonplaceholder.typicode.com', '/posts/1'), body: body)
.interceptWithAlice(_aliceHttpAdapter, body: body);

http
.patch(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!,
body: body)
.patch(
Uri.https('jsonplaceholder.typicode.com', '/posts/1'),
body: body,
)
.interceptWithAlice(_aliceHttpAdapter, body: body);

http
.delete(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!)
.delete(Uri.https('jsonplaceholder.typicode.com', '/posts/1'))
.interceptWithAlice(_aliceHttpAdapter, body: body);

http
.get(Uri.tryParse('https://jsonplaceholder.typicode.com/test/test')!)
.get(Uri.https('jsonplaceholder.typicode.com', '/test/test'))
.interceptWithAlice(_aliceHttpAdapter);

http
.post(Uri.tryParse('https://jsonplaceholder.typicode.com/posts')!,
body: body)
.then((response) {
_aliceHttpAdapter.onResponse(response, body: body);
});
.post(Uri.https('jsonplaceholder.typicode.com', '/posts'), body: body)
.then((response) => _aliceHttpAdapter.onResponse(response, body: body));

http
.get(Uri.tryParse('https://jsonplaceholder.typicode.com/posts')!)
.then((response) {
_aliceHttpAdapter.onResponse(response);
});
.get(Uri.https('jsonplaceholder.typicode.com', '/posts'))
.then((response) => _aliceHttpAdapter.onResponse(response));

http
.put(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!,
body: body)
.then((response) {
_aliceHttpAdapter.onResponse(response, body: body);
});
.put(Uri.https('jsonplaceholder.typicode.com', '/posts/1'), body: body)
.then((response) => _aliceHttpAdapter.onResponse(response, body: body));

http
.patch(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!,
body: body)
.then((response) {
_aliceHttpAdapter.onResponse(response, body: body);
});
.patch(
Uri.https('jsonplaceholder.typicode.com', '/posts/1'),
body: body,
)
.then((response) => _aliceHttpAdapter.onResponse(response, body: body));

http
.delete(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!)
.then((response) {
_aliceHttpAdapter.onResponse(response);
});
.delete(Uri.https('jsonplaceholder.typicode.com', '/posts/1'))
.then((response) => _aliceHttpAdapter.onResponse(response));

http
.get(Uri.tryParse('https://jsonplaceholder.typicode.com/test/test')!)
.then((response) {
_aliceHttpAdapter.onResponse(response);
});
.get(Uri.https('jsonplaceholder.typicode.com', '/test/test'))
.then((response) => _aliceHttpAdapter.onResponse(response));

http
.post(
Uri.tryParse(
'https://jsonplaceholder.typicode.com/posts?key1=value1')!,
body: body)
Uri.https(
'jsonplaceholder.typicode.com',
'/posts',
{'key1': 'value1'},
),
body: body,
)
.interceptWithAlice(_aliceHttpAdapter, body: body);

http
.post(
Uri.tryParse(
'https://jsonplaceholder.typicode.com/posts?key1=value1&key2=value2&key3=value3')!,
body: body)
Uri.https(
'jsonplaceholder.typicode.com',
'/posts',
{
'key1': 'value1',
'key2': 'value2',
'key3': 'value3',
},
),
body: body,
)
.interceptWithAlice(_aliceHttpAdapter, body: body);

http
.get(Uri.tryParse(
'https://jsonplaceholder.typicode.com/test/test?key1=value1&key2=value2&key3=value3')!)
.then((response) {
_aliceHttpAdapter.onResponse(response);
});
.get(
Uri.https(
'jsonplaceholder.typicode.com',
'/test/test',
{
'key1': 'value1',
'key2': 'value2',
'key3': 'value3',
},
),
)
.then((response) => _aliceHttpAdapter.onResponse(response));
}

void _runHttpInspector() {
Expand Down
3 changes: 3 additions & 0 deletions examples/alice_http/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ dependencies:
alice_http:
http: ^1.2.1

dev_dependencies:
flutter_lints: ^4.0.0

dependency_overrides:
alice:
path: ../../packages/alice
Expand Down

0 comments on commit 39506e5

Please sign in to comment.