Skip to content

Instantly share code, notes, and snippets.

@joeypiccola
Created October 24, 2023 19:17
Show Gist options
  • Save joeypiccola/55cb6773811d31356643b8aabdd9169c to your computer and use it in GitHub Desktop.
Save joeypiccola/55cb6773811d31356643b8aabdd9169c to your computer and use it in GitHub Desktop.
go api exercise
// this is what main.go returns
{
"main": {
"temp": 296.52,
"pressure": 1002,
"humidity": 23
}
}
type AutoGenerated struct {
Coord struct {
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
} `json:"coord"`
Weather []struct {
ID int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
} `json:"weather"`
Base string `json:"base"`
Main struct {
Temp float64 `json:"temp"`
FeelsLike float64 `json:"feels_like"`
TempMin float64 `json:"temp_min"`
TempMax float64 `json:"temp_max"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
} `json:"main"`
Visibility int `json:"visibility"`
Wind struct {
Speed float64 `json:"speed"`
Deg int `json:"deg"`
Gust float64 `json:"gust"`
} `json:"wind"`
Clouds struct {
All int `json:"all"`
} `json:"clouds"`
Dt int `json:"dt"`
Sys struct {
Type int `json:"type"`
ID int `json:"id"`
Country string `json:"country"`
Sunrise int `json:"sunrise"`
Sunset int `json:"sunset"`
} `json:"sys"`
Timezone int `json:"timezone"`
ID int `json:"id"`
Name string `json:"name"`
Cod int `json:"cod"`
}
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
type OwmParams struct {
Token string `json:"token"`
Zip_code int `json:"zip_code"`
}
type weather struct {
Main struct {
Temperature float64 `json:"temp"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
} `json:"main"`
}
func owm(c *gin.Context) {
var ap OwmParams
if err := c.ShouldBindJSON(&ap); err != nil {
c.JSON(400, gin.H{"error with ShouldBindJSON": err.Error()})
return
}
uri := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?zip=%s,us&appid=%s", strconv.Itoa(ap.Zip_code), ap.Token)
res, err := http.Get(uri)
if err != nil {
c.JSON(res.StatusCode, gin.H{"error with http.Get": err.Error()})
return
}
defer res.Body.Close() // run last
bs, err := io.ReadAll(res.Body)
var w weather
if err := json.Unmarshal(bs, &w); err != nil {
c.JSON(400, gin.H{"error with json.Unmarshal": err.Error()})
return
}
j, _ := json.Marshal(w)
c.Data(200, "application/json", j)
}
func main() {
router := gin.Default()
router.POST("/owm/", owm)
router.Run(":8080")
}
{
"coord": {
"lon": -104.9847,
"lat": 39.7392
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 299.56,
"feels_like": 299.56,
"temp_min": 295.76,
"temp_max": 301.73,
"pressure": 1002,
"humidity": 14
},
"visibility": 10000,
"wind": {
"speed": 1.79,
"deg": 248,
"gust": 4.92
},
"clouds": {
"all": 4
},
"dt": 1698093960,
"sys": {
"type": 2,
"id": 2002746,
"country": "US",
"sunrise": 1698067095,
"sunset": 1698106201
},
"timezone": -21600,
"id": 0,
"name": "Denver",
"cod": 200
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment