1+ package com .aws .workshop .ai .agent ;
2+
3+ import java .net .URI ;
4+ import java .net .http .HttpClient ;
5+ import java .net .http .HttpRequest ;
6+ import java .net .http .HttpResponse ;
7+ import java .time .LocalDate ;
8+ import java .time .format .DateTimeFormatter ;
9+
10+ import org .json .JSONObject ;
11+ import org .springframework .ai .tool .annotation .Tool ;
12+
13+ public class WeatherTools {
14+
15+ private final HttpClient httpClient = HttpClient .newHttpClient ();
16+
17+ @ Tool (description = "Get weather forecast for a city on a specific date (format: YYYY-MM-DD)" )
18+ String getWeather (String city , String date ) {
19+ try {
20+ // Convert city to coordinates using Geocoding API
21+ String encodedCity = java .net .URLEncoder .encode (city , java .nio .charset .StandardCharsets .UTF_8 );
22+ String geocodingUrl = "https://geocoding-api.open-meteo.com/v1/search?name=" + encodedCity + "&count=1" ;
23+ HttpRequest geocodingRequest = HttpRequest .newBuilder ()
24+ .uri (URI .create (geocodingUrl ))
25+ .GET ()
26+ .build ();
27+
28+ HttpResponse <String > geocodingResponse = httpClient .send (geocodingRequest , HttpResponse .BodyHandlers .ofString ());
29+ JSONObject geocodingJson = new JSONObject (geocodingResponse .body ());
30+
31+ if (geocodingJson .getJSONArray ("results" ).isEmpty ()) {
32+ return "City not found" ;
33+ }
34+
35+ JSONObject location = geocodingJson .getJSONArray ("results" ).getJSONObject (0 );
36+ double latitude = location .getDouble ("latitude" );
37+ double longitude = location .getDouble ("longitude" );
38+
39+ // Parse and validate date
40+ LocalDate forecastDate ;
41+ try {
42+ forecastDate = LocalDate .parse (date , DateTimeFormatter .ISO_LOCAL_DATE );
43+ } catch (Exception e ) {
44+ return "Invalid date format. Please use YYYY-MM-DD format." ;
45+ }
46+
47+ // Get weather data from Open-Meteo API
48+ String weatherUrl = "https://api.open-meteo.com/v1/forecast" +
49+ "?latitude=" + latitude +
50+ "&longitude=" + longitude +
51+ "&daily=temperature_2m_max,temperature_2m_min" +
52+ "&timezone=auto" +
53+ "&start_date=" + date +
54+ "&end_date=" + date ;
55+
56+ HttpRequest weatherRequest = HttpRequest .newBuilder ()
57+ .uri (URI .create (weatherUrl ))
58+ .GET ()
59+ .build ();
60+
61+ HttpResponse <String > weatherResponse = httpClient .send (weatherRequest , HttpResponse .BodyHandlers .ofString ());
62+ JSONObject weatherJson = new JSONObject (weatherResponse .body ());
63+
64+ double maxTemp = weatherJson .getJSONObject ("daily" ).getJSONArray ("temperature_2m_max" ).getDouble (0 );
65+ double minTemp = weatherJson .getJSONObject ("daily" ).getJSONArray ("temperature_2m_min" ).getDouble (0 );
66+ String unit = weatherJson .getJSONObject ("daily_units" ).getString ("temperature_2m_max" );
67+
68+ return "Weather for " + location .getString ("name" ) + " on " + date + ": " +
69+ "Min: " + minTemp + unit + ", Max: " + maxTemp + unit ;
70+
71+ } catch (Exception e ) {
72+ return "Error fetching weather data: " + e .getMessage ();
73+ }
74+ }
75+ }
0 commit comments