v1.0.4 webapi fixes
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:workouttest_util/model/cache.dart';
|
||||
import 'package:workouttest_util/service/openai_service.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
String _response = "";
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void _fetchData() async {
|
||||
var api = OpenAIApi();
|
||||
String response = await api.getOpenAICompletion("Who wrote the song 'yellow submarine'?");;
|
||||
setState(() {
|
||||
_response = response;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Cache().setLocalBaseUrl();
|
||||
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Example"),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
const Text(
|
||||
'AI answer:',
|
||||
),
|
||||
Text(
|
||||
_response,
|
||||
style: Theme.of(context).textTheme.headline4,
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
FloatingActionButton(
|
||||
onPressed:() => {
|
||||
_fetchData()
|
||||
},
|
||||
tooltip: 'Send question',
|
||||
child: const Icon(Icons.ad_units),
|
||||
),
|
||||
|
||||
],
|
||||
)
|
||||
|
||||
],
|
||||
),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,7 @@ class APIClient with Common, Logging {
|
||||
return webClient.post(endPoint, body);
|
||||
}
|
||||
final url = Cache().getBaseUrl() + endPoint;
|
||||
trace(" ------------ http/post body $body - url: $url ");
|
||||
log(" ------------ http/post body $body - url: $url ");
|
||||
try {
|
||||
String authToken = Cache().getAuthToken();
|
||||
if (authToken.isEmpty) {
|
||||
@@ -91,7 +91,7 @@ class APIClient with Common, Logging {
|
||||
request.write(body);
|
||||
HttpClientResponse result = await request.close();
|
||||
client.close();
|
||||
trace(" ------------post response code: ${result.statusCode}");
|
||||
log(" ------------post response code: ${result.statusCode}");
|
||||
if (result.statusCode == 200) {
|
||||
return await result.transform(utf8.decoder).join();
|
||||
} else if (result.statusCode == 404) {
|
||||
@@ -115,7 +115,7 @@ class APIClient with Common, Logging {
|
||||
}
|
||||
final url = Cache().getBaseUrl() + endPoint + param;
|
||||
try {
|
||||
trace("-------- API get $url");
|
||||
log("-------- API get $url");
|
||||
String authToken = Cache().getAuthToken();
|
||||
if (authToken.isEmpty) {
|
||||
var responseJson = await authenticateUser(Cache.username, Cache.password);
|
||||
@@ -132,7 +132,7 @@ class APIClient with Common, Logging {
|
||||
request.write("");
|
||||
HttpClientResponse result = await request.close();
|
||||
client.close();
|
||||
trace(" ------------get response code: ${result.statusCode}");
|
||||
log(" ------------get response code: ${result.statusCode}");
|
||||
if (result.statusCode == 200) {
|
||||
return await result.transform(utf8.decoder).join();
|
||||
} else if (result.statusCode == 404) {
|
||||
|
||||
@@ -43,7 +43,7 @@ class APIWebClient with Common, Logging {
|
||||
log("Authentication status: ${result.statusCode}, response: $response");
|
||||
final data = jsonDecode(response);
|
||||
return data;
|
||||
} catch (exception) {
|
||||
} on Exception catch (exception) {
|
||||
log(exception.toString());
|
||||
try {
|
||||
await Sentry.captureException(exception);
|
||||
@@ -57,7 +57,7 @@ class APIWebClient with Common, Logging {
|
||||
|
||||
Future<String> post(String endPoint, String body) async {
|
||||
final url = Cache().getBaseUrl() + endPoint;
|
||||
trace(" ------------ http/post body $body - url: $url ");
|
||||
log(" ------------ http/post body $body - url: $url ");
|
||||
try {
|
||||
String authToken = Cache().getAuthToken();
|
||||
if (authToken.isEmpty) {
|
||||
@@ -70,7 +70,7 @@ class APIWebClient with Common, Logging {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": 'Bearer $authToken',
|
||||
});
|
||||
trace(" ------------post response code: ${result.statusCode}");
|
||||
log(" ------------post response code: ${result.statusCode}");
|
||||
if (result.statusCode == 200) {
|
||||
return result.body;
|
||||
} else if (result.statusCode == 404) {
|
||||
@@ -90,7 +90,7 @@ class APIWebClient with Common, Logging {
|
||||
Future<String> get(String endPoint, String param) async {
|
||||
final url = Cache().getBaseUrl() + endPoint + param;
|
||||
try {
|
||||
trace("-------- API get $url");
|
||||
log("-------- API get $url");
|
||||
String authToken = Cache().getAuthToken();
|
||||
if (authToken.isEmpty) {
|
||||
var responseJson = await authenticateUser(Cache.username, Cache.password);
|
||||
@@ -103,7 +103,7 @@ class APIWebClient with Common, Logging {
|
||||
"Authorization": 'Bearer $authToken',
|
||||
});
|
||||
|
||||
trace(" ------------get response code: ${result.statusCode}");
|
||||
log(" ------------get response code: ${result.statusCode}");
|
||||
if (result.statusCode == 200) {
|
||||
return result.body;
|
||||
} else if (result.statusCode == 404) {
|
||||
|
||||
Reference in New Issue
Block a user