Skip to content

Instantly share code, notes, and snippets.

@rogerwelin
Last active March 18, 2019 20:31
Show Gist options
  • Select an option

  • Save rogerwelin/1bcc1a5e9a894d592d3113970de0cea7 to your computer and use it in GitHub Desktop.

Select an option

Save rogerwelin/1bcc1a5e9a894d592d3113970de0cea7 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
// remote weather api url
const apiUrl = "https://www.metaweather.com/api/location/"
// message we'll receive from the client
type Event struct {
WoeID string `json:"woeid"`
}
// raw data from the remote weather api
type rawWeatherData struct {
ConsolidatedWeather []struct {
WeatherStateName string `json:"weather_state_name"`
MinTemp float64 `json:"min_temp"`
MaxTemp float64 `json:"max_temp"`
Humidity int `json:"humidity"`
Predictability int `json:"predictability"`
} `json:"consolidated_weather"`
Title string `json:"title"`
LocationType string `json:"location_type"`
Woeid int `json:"woeid"`
LattLong string `json:"latt_long"`
}
// message we will send to the client
type WeatherData struct {
WeatherStateName string `json:"weather_state_name"`
MinTemp float64 `json:"min_temp"`
MaxTemp float64 `json:"max_temp"`
Title string `json:"title"`
LattLong string `json:"latt_long"`
}
func returnHighestPredictability(data *rawWeatherData) (*WeatherData, error) {
highestPredictability := 0
var predictabilityIndex int
for i, p := range data.ConsolidatedWeather {
if p.Predictability > highestPredictability {
highestPredictability = p.Predictability
predictabilityIndex = i
}
}
cleanedData := &WeatherData{
WeatherStateName: data.ConsolidatedWeather[predictabilityIndex].WeatherStateName,
MinTemp: data.ConsolidatedWeather[predictabilityIndex].MinTemp,
MaxTemp: data.ConsolidatedWeather[predictabilityIndex].MaxTemp,
Title: data.Title,
LattLong: data.LattLong,
}
return cleanedData, nil
}
func HandleRequest(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var weather *WeatherData
var event Event
// IMPORTANT! Set Cors headers
resp := events.APIGatewayProxyResponse{Headers: make(map[string]string)}
resp.Headers["Access-Control-Allow-Origin"] = "*"
// This will create a log entry in Cloudwatch
fmt.Println("Request body: " + req.Body)
err := json.Unmarshal([]byte(req.Body), &event)
if err != nil {
resp.StatusCode = 500
resp.Body = err.Error()
return resp, nil
}
response, err := http.Get(apiUrl + event.WoeID)
if err != nil {
resp.StatusCode = 500
resp.Body = err.Error()
return resp, nil
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
resp.StatusCode = 500
resp.Body = err.Error()
return resp, nil
}
defer response.Body.Close()
var data rawWeatherData
if err := json.Unmarshal(body, &data); err != nil {
resp.StatusCode = 500
resp.Body = err.Error()
return resp, nil
}
weather, err = returnHighestPredictability(&data)
if err != nil {
resp.StatusCode = 500
resp.Body = err.Error()
return resp, nil
}
re, err := json.Marshal(*weather)
if err != nil {
resp.StatusCode = 500
resp.Body = err.Error()
return resp, nil
}
resp.StatusCode = 200
resp.Body = string(re)
return resp, nil
}
func main() {
// Start takes a handler and talks to an internal Lambda endpoint to pass requests to the handler
lambda.Start(HandleRequest)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment