2025-05-14 08:34:23 -07:00

52 lines
1.3 KiB
Dart

// File for helper functions.
import '/var/config.dart';
import '/var/secrets.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
//import 'dart:convert';
// Generic method to hit a GET request and return the response.
Future<String> hitAPI(String url) async {
if (debug) debugPrint("DEBUG: URL is '$url'.");
try {
http.Response response = await http.get(Uri.parse(url));
String data = "";
if (response.statusCode == 200) {
data = response.body;
//var decodedData = jsonDecode(data);
//data = decodedData.toString();
//data = jsonEncode(decodedData);
if (debugVerbose) {
debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n");
}
} else {
if (debug) {
debugPrint(
"DEBUG: Response failed with code '${response.statusCode.toString()}'",
);
}
}
return data;
} catch (e) {
return "Sorry! It seems we have an issue: '${e.toString()}'.";
}
}
// How to guide: https://openweathermap.org/current#zip
Future<String> hitOpenWeatherZip(String zipCode, String? countryCode) async {
countryCode ??= "US";
String url =
'$openWeatherAPI'
'?zip=$zipCode,$countryCode'
'&units=imperial'
'&appid=$openWeatherKey';
String data = await hitAPI(url);
return data;
}