Skip to content

Commit

Permalink
refactor: examples/alice_http_client (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse authored Jun 22, 2024
1 parent c41aa79 commit 209f61c
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 83 deletions.
22 changes: 9 additions & 13 deletions examples/alice_http_client/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
186 changes: 116 additions & 70 deletions examples/alice_http_client/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,26 @@ import 'package:alice_http_client/alice_http_client_adapter.dart';
import 'package:alice_http_client/alice_http_client_extensions.dart';
import 'package:flutter/material.dart';

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 HttpClient _httpClient;
late AliceHttpClientAdapter _httpClientAdapter;
late final HttpClient _httpClient = HttpClient();

@override
void initState() {
_alice = Alice(
showNotification: true,
showInspectorOnShake: true,
maxCallsCount: 1000,
);
_httpClient = HttpClient();
_httpClientAdapter = AliceHttpClientAdapter();
_alice.addAdapter(_httpClientAdapter);
late final AliceHttpClientAdapter _httpClientAdapter =
AliceHttpClientAdapter();

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

@override
Widget build(BuildContext context) {
Expand All @@ -42,27 +37,27 @@ class _MyAppState extends State<MyApp> {
title: const Text('Alice + HTTP Client - Example'),
),
body: Container(
padding: EdgeInsets.all(16),
padding: const EdgeInsets.all(16),
child: ListView(
children: [
const SizedBox(height: 8),
Text(
const Text(
'Welcome to example of Alice Http Inspector. Click buttons below to generate sample data.'),
ElevatedButton(
child: Text(
onPressed: _runHttpHttpClientRequests,
child: const Text(
'Run HttpClient Requests',
),
onPressed: _runHttpHttpClientRequests,
),
const SizedBox(height: 8),
Text(
const Text(
'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 @@ -72,79 +67,130 @@ class _MyAppState extends State<MyApp> {
}

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

_httpClient
.getUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts'))
.getUrl(Uri.https('jsonplaceholder.typicode.com', '/posts'))
.interceptWithAlice(_httpClientAdapter);

_httpClient
.postUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts'))
.interceptWithAlice(_httpClientAdapter,
body: body, headers: <String, dynamic>{});
.postUrl(Uri.https('jsonplaceholder.typicode.com', '/posts'))
.interceptWithAlice(_httpClientAdapter, body: body, headers: {});

_httpClient
.putUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts/1'))
.putUrl(Uri.https('jsonplaceholder.typicode.com', '/posts/1'))
.interceptWithAlice(_httpClientAdapter, body: body);

_httpClient
.getUrl(Uri.parse('https://jsonplaceholder.typicode.com/test/test/'))
.getUrl(Uri.https('jsonplaceholder.typicode.com', '/test/test/'))
.interceptWithAlice(_httpClientAdapter);

_httpClient
.postUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts'))
.then((request) async {
_httpClientAdapter.onRequest(request, body: body);
request.write(body);
var httpResponse = await request.close();
var responseBody = await utf8.decoder.bind(httpResponse).join();
_httpClientAdapter.onResponse(httpResponse, request, body: responseBody);
});
.postUrl(Uri.https('jsonplaceholder.typicode.com', '/posts'))
.then(
(HttpClientRequest request) async {
_httpClientAdapter.onRequest(request, body: body);

request.write(body);

final HttpClientResponse httpResponse = await request.close();

final String responseBody =
await utf8.decoder.bind(httpResponse).join();

_httpClientAdapter.onResponse(
httpResponse,
request,
body: responseBody,
);
},
);

_httpClient
.putUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts/1'))
.then((request) async {
_httpClientAdapter.onRequest(request, body: body);
request.write(body);
var httpResponse = await request.close();
var responseBody = await utf8.decoder.bind(httpResponse).join();
_httpClientAdapter.onResponse(httpResponse, request, body: responseBody);
});
.putUrl(Uri.https('jsonplaceholder.typicode.com', '/posts/1'))
.then(
(HttpClientRequest request) async {
_httpClientAdapter.onRequest(request, body: body);

request.write(body);

final HttpClientResponse httpResponse = await request.close();

final String responseBody =
await utf8.decoder.bind(httpResponse).join();

_httpClientAdapter.onResponse(
httpResponse,
request,
body: responseBody,
);
},
);

_httpClient
.patchUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts/1'))
.then((request) async {
_httpClientAdapter.onRequest(request, body: body);
request.write(body);
var httpResponse = await request.close();
var responseBody = await utf8.decoder.bind(httpResponse).join();
_httpClientAdapter.onResponse(httpResponse, request, body: responseBody);
});
.patchUrl(Uri.https('jsonplaceholder.typicode.com', '/posts/1'))
.then(
(HttpClientRequest request) async {
_httpClientAdapter.onRequest(request, body: body);

request.write(body);

final HttpClientResponse httpResponse = await request.close();
final String responseBody =
await utf8.decoder.bind(httpResponse).join();

_httpClientAdapter.onResponse(
httpResponse,
request,
body: responseBody,
);
},
);

_httpClient
.deleteUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts/1'))
.then((request) async {
_httpClientAdapter.onRequest(request);
var httpResponse = await request.close();
var responseBody = await utf8.decoder.bind(httpResponse).join();
_httpClientAdapter.onResponse(httpResponse, request, body: responseBody);
});
.deleteUrl(Uri.https('jsonplaceholder.typicode.com', '/posts/1'))
.then(
(HttpClientRequest request) async {
_httpClientAdapter.onRequest(request);

final HttpClientResponse httpResponse = await request.close();

final String responseBody =
await utf8.decoder.bind(httpResponse).join();

_httpClientAdapter.onResponse(
httpResponse,
request,
body: responseBody,
);
},
);

_httpClient
.getUrl(Uri.parse('https://jsonplaceholder.typicode.com/test/test/'))
.then((request) async {
_httpClientAdapter.onRequest(request);
var httpResponse = await request.close();
var responseBody = await utf8.decoder.bind(httpResponse).join();
_httpClientAdapter.onResponse(httpResponse, request, body: responseBody);
});
.getUrl(Uri.https('jsonplaceholder.typicode.com', '/test/test/'))
.then(
(HttpClientRequest request) async {
_httpClientAdapter.onRequest(request);

final HttpClientResponse httpResponse = await request.close();

final String responseBody =
await utf8.decoder.bind(httpResponse).join();

_httpClientAdapter.onResponse(
httpResponse,
request,
body: responseBody,
);
},
);
}

void _runHttpInspector() {
_alice.showInspector();
}
}

3 changes: 3 additions & 0 deletions examples/alice_http_client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ dependencies:
alice:
alice_http_client:

dev_dependencies:
flutter_lints: ^4.0.0

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

0 comments on commit 209f61c

Please sign in to comment.