feat(api_connection): add test request to VBB API and ignore env files

This commit is contained in:
fbachus
2026-05-15 17:09:18 +02:00
parent b8d226fd38
commit 274c27c7e1
5 changed files with 103 additions and 3 deletions
+2
View File
@@ -1,3 +1,5 @@
.env
# Miscellaneous # Miscellaneous
*.class *.class
*.log *.log
+36
View File
@@ -0,0 +1,36 @@
import 'package:http/http.dart' as http;
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'dart:core';
import 'dart:convert';
import 'transport_helper.dart';
class ApiHandler {
// TODO: Split next line for better Error Handling
static final String baseUrl = dotenv.get("VBB_API_URL");
static final String accessID = dotenv.get("VBB_API_KEY");
//static Future<Map<String, dynamic>> findStations(String locationName) async {
static Future<String> findStations(String locationName) async {
String urlPath = '/api/fahrinfo/latest/location.name';
String urlParams = '?input=$locationName';
Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID,
'input': locationName,
'maxNo': '2',
'type': 'S' // Station/Stops only
});
//print(url);
var jsonResponse = await http.get(url,
headers: {
'Accept': 'application/json'
});
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>;
//print(httpPackageJson["stopLocationOrCoordLocation"]);
final id = httpPackageJson["stopLocationOrCoordLocation"][0]["StopLocation"]["id"];
//print("and now just ids: ");
//print(id);
return id;
}
}
+19 -1
View File
@@ -1,6 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'api_handler.dart';
void main() { void main() async {
await dotenv.load();
runApp(const MyApp()); runApp(const MyApp());
} }
@@ -55,6 +58,10 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
int _counter = 0; int _counter = 0;
//String API_KEY = ApiHandler.accessID;
String API_KEY = "no telly";
//Future<Map<String, dynamic>> ?id;
String id = '';
void _incrementCounter() { void _incrementCounter() {
setState(() { setState(() {
@@ -66,6 +73,10 @@ class _MyHomePageState extends State<MyHomePage> {
_counter++; _counter++;
}); });
} }
void show_result(Future<String> future_id) async {
id = await future_id;
setState(() { });
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -109,6 +120,13 @@ class _MyHomePageState extends State<MyHomePage> {
'$_counter', '$_counter',
style: Theme.of(context).textTheme.headlineMedium, style: Theme.of(context).textTheme.headlineMedium,
), ),
Text('API key from ENV is $API_KEY'),
MaterialButton(onPressed: () { show_result(ApiHandler.findStations('S Biesdorf')); setState(() {}); },
child: Text(
'Search for \'S Biesdorf\''
),
),
Text(id)
], ],
), ),
), ),
+42 -2
View File
@@ -62,6 +62,14 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_dotenv:
dependency: "direct main"
description:
name: flutter_dotenv
sha256: d41da11fb497314fbf89811ec30af02d1d898b47980a129f0a8c0a1720460ba2
url: "https://pub.dev"
source: hosted
version: "6.0.1"
flutter_lints: flutter_lints:
dependency: "direct dev" dependency: "direct dev"
description: description:
@@ -75,6 +83,22 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
http:
dependency: "direct main"
description:
name: http
sha256: "87721a4a50b19c7f1d49001e51409bddc46303966ce89a65af4f4e6004896412"
url: "https://pub.dev"
source: hosted
version: "1.6.0"
http_parser:
dependency: transitive
description:
name: http_parser
sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571"
url: "https://pub.dev"
source: hosted
version: "4.1.2"
leak_tracker: leak_tracker:
dependency: transitive dependency: transitive
description: description:
@@ -192,6 +216,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.7.10" version: "0.7.10"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006
url: "https://pub.dev"
source: hosted
version: "1.4.0"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
@@ -204,10 +236,18 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: vm_service name: vm_service
sha256: "046d3928e16fa4dc46e8350415661755ab759d9fc97fc21b5ab295f71e4f0499" sha256: "0016aef94fc66495ac78af5859181e3f3bf2026bd8eecc72b9565601e19ab360"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "15.1.0" version: "15.2.0"
web:
dependency: transitive
description:
name: web
sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"
url: "https://pub.dev"
source: hosted
version: "1.1.1"
sdks: sdks:
dart: ">=3.11.5 <4.0.0" dart: ">=3.11.5 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54" flutter: ">=3.18.0-18.0.pre.54"
+4
View File
@@ -34,6 +34,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8 cupertino_icons: ^1.0.8
flutter_dotenv: ^6.0.1
http: ^1.6.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
@@ -61,6 +63,8 @@ flutter:
# assets: # assets:
# - images/a_dot_burr.jpeg # - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg # - images/a_dot_ham.jpeg
assets:
- .env
# An image asset can refer to one or more resolution-specific "variants", see # An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/to/resolution-aware-images # https://flutter.dev/to/resolution-aware-images