Compare commits
3 Commits
8c8b848090
...
b1d01c6915
Author | SHA1 | Date | |
---|---|---|---|
b1d01c6915 | |||
c2995dac6d | |||
4483c1ebb0 |
45
lib/helpers/http.dart
Normal file
45
lib/helpers/http.dart
Normal file
@ -0,0 +1,45 @@
|
||||
// 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(url);
|
||||
|
||||
http.Response response = await http.get(Uri.parse(url));
|
||||
|
||||
String data = "";
|
||||
if (response.statusCode == 200) {
|
||||
data = response.body;
|
||||
var decodedData = jsonDecode(data);
|
||||
if (debugVerbose) debugPrint(decodedData.toString());
|
||||
data = decodedData.toString();
|
||||
} else {
|
||||
if (debug) debugPrint(response.statusCode.toString());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Convert the Future value into a usable value.
|
||||
loadWeather() async {
|
||||
return await hitOpenWeatherZip("47630", "US");
|
||||
}
|
1
lib/helpers/json.dart
Normal file
1
lib/helpers/json.dart
Normal file
@ -0,0 +1 @@
|
||||
// Functions related to parsing JSON strings and objects.
|
@ -1,11 +1,10 @@
|
||||
// Local
|
||||
import '/var/secrets.dart';
|
||||
import 'var/config.dart';
|
||||
import '/var/config.dart';
|
||||
import 'helpers/http.dart';
|
||||
|
||||
// Flutter / Dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:convert';
|
||||
|
||||
void main() {
|
||||
runApp(const MainApp());
|
||||
@ -75,37 +74,7 @@ class _MainAppState extends State<MainApp> {
|
||||
);
|
||||
}
|
||||
|
||||
// How to guide:
|
||||
// https://openweathermap.org/current#zip
|
||||
Future<String> _hitOpenWeather(String zipCode, String? countryCode) async {
|
||||
countryCode ??= "US";
|
||||
|
||||
String url =
|
||||
'https://api.openweathermap.org/data/2.5/forecast'
|
||||
'?zip=$zipCode,$countryCode'
|
||||
'&units=imperial'
|
||||
'&appid=$openWeatherKey';
|
||||
|
||||
if (debug) debugPrint(url);
|
||||
http.Response response = await http.get(Uri.parse(url));
|
||||
|
||||
String data = "";
|
||||
if (response.statusCode == 200) {
|
||||
data = response.body;
|
||||
var decodedData = jsonDecode(data);
|
||||
//if (debug) debugPrint(decodedData.toString());
|
||||
data = decodedData.toString();
|
||||
} else {
|
||||
if (debug) debugPrint(response.statusCode.toString());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
_callOpenWeather() async {
|
||||
weather = await _hitOpenWeather("47630", "US");
|
||||
}
|
||||
|
||||
// Call the
|
||||
_refreshWeather() {
|
||||
var lastReloadSeconds = DateTime.now().difference(lastLoadTime).inSeconds;
|
||||
if (debug) debugPrint("DEBUG: Refresh was $lastReloadSeconds seconds ago.");
|
||||
@ -118,7 +87,7 @@ class _MainAppState extends State<MainApp> {
|
||||
}
|
||||
|
||||
weather = loadText;
|
||||
_callOpenWeather();
|
||||
weather = loadWeather();
|
||||
lastLoadTime = DateTime.now();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,15 @@
|
||||
// This file needs renamed `local.dart` when implemented.
|
||||
|
||||
// Basic
|
||||
// Project-wide configuration variables for both testing and production.
|
||||
const bool debug = false;
|
||||
const bool debug = true;
|
||||
const bool debugVerbose = false;
|
||||
const int limitRefreshSeconds = 60;
|
||||
|
||||
// OpenWeather Constants
|
||||
// Settings for how to use the OpenWeather API.
|
||||
const String openWeatherProtocol = "https://";
|
||||
const String openWeatherHost = "api.openweathermap.org";
|
||||
const String openWeatherURI = "/data/2.5/forecast";
|
||||
const String openWeatherAPI =
|
||||
"$openWeatherProtocol$openWeatherHost$openWeatherURI";
|
||||
|
Reference in New Issue
Block a user