go-foursquare/query.go

89 lines
1.6 KiB
Go

package foursquare
import (
"encoding/json"
"fmt"
"github.com/google/go-querystring/query"
"io"
"net/http"
)
type Query struct {
APIKey string
Query string
Categories string
Chains string
}
/*func NewQueryForLocation(apiKey string, lat string, lon string, modifierType string, modifier string) Query {
category := ""
chain := ""
if modifierType == "chain" {
chain = modifier
}
if modifierType == "category" {
category = modifier
}
return Query{
APIKey: apiKey,
Query: lat + "," + lon,
Categories: category,
Chains: chain,
}
}
*/
// go run main.go -key "fsq3ksL8PkGBLhDJ6Bydixoa9Lah6djwvm5a5LIWrtmFexc=" -ll "51.507351,-0.1277" -category "13303"
func (options *Options) NewQueryForLocation() (*FoursquarePlacesv2, error) {
fmt.Println("Foursquare")
//url := "https://api.foursquare.com/v3/places/search?ll=51.507351%2C-0.127758&radius=800&categories=13303"
v, _ := query.Values(options)
fmt.Println(v.Encode())
url := "https://api.foursquare.com/v3/places/search?" + v.Encode()
fmt.Println(url)
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("accept", "application/json")
req.Header.Add("Authorization",options.Key )
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Http failed")
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
fmt.Println(string(body))
if err != nil {
fmt.Println("io readall failed")
return nil, err
}
dataPtr := &FoursquarePlacesv2{}
err = json.Unmarshal(body, dataPtr)
if err != nil {
fmt.Println("Unmarshal failed")
return nil, err
}
return dataPtr, err
}