158 lines
2.7 KiB
Go
158 lines
2.7 KiB
Go
package fsq
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/google/go-querystring/query"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func (query Query) SearchRaw() (json string, err error) {
|
|
|
|
url := PlacesURL(query)
|
|
|
|
log.Print(url)
|
|
|
|
bytes, err := download(url, query.APIKey)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(bytes), nil
|
|
}
|
|
|
|
func (query Query) Search() (*FoursquarePlaces, error) {
|
|
|
|
url := PlacesURL(query)
|
|
|
|
log.Print(url)
|
|
|
|
bytes, err := download(url, query.APIKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dataPtr := &FoursquarePlaces{}
|
|
|
|
err = json.Unmarshal(bytes, dataPtr)
|
|
|
|
return dataPtr, err
|
|
}
|
|
|
|
func (detailQuery DetailQuery) GetDetailsRaw() (json string, err error) {
|
|
url := PlacesDetailsURL(detailQuery)
|
|
|
|
log.Print(url)
|
|
|
|
bytes, err := download(url, detailQuery.APIKey)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(bytes), nil
|
|
}
|
|
|
|
func (detailQuery DetailQuery) GetDetails() (*Results4sqPlaces, error) {
|
|
url := PlacesDetailsURL(detailQuery)
|
|
|
|
log.Print(url)
|
|
|
|
bytes, err := download(url, detailQuery.APIKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
datePtr := &Results4sqPlaces{}
|
|
err = json.Unmarshal(bytes, datePtr)
|
|
return datePtr, err
|
|
}
|
|
|
|
func (detailQuery DetailQuery) GetPhotosRaw() (json string, err error) {
|
|
url := PlacesPhotosURL(detailQuery)
|
|
|
|
log.Print(url)
|
|
|
|
bytes, err := download(url, detailQuery.APIKey)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(bytes), nil
|
|
}
|
|
|
|
func (detailQuery DetailQuery) GetPhotos() (*[]Photos4sqPlaces, error) {
|
|
url := PlacesPhotosURL(detailQuery)
|
|
|
|
log.Print(url)
|
|
|
|
bytes, err := download(url, detailQuery.APIKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dataPtr := &[]Photos4sqPlaces{}
|
|
|
|
err = json.Unmarshal(bytes, dataPtr)
|
|
return dataPtr, err
|
|
|
|
}
|
|
|
|
func PlacesURL(q Query) string {
|
|
|
|
v, _ := query.Values(q)
|
|
|
|
fmt.Println(v.Encode())
|
|
url := "https://api.foursquare.com/v3/places/search?" + v.Encode()
|
|
|
|
fmt.Println(url)
|
|
|
|
return url
|
|
}
|
|
|
|
func PlacesDetailsURL(dq DetailQuery) string {
|
|
|
|
v, _ := query.Values(dq)
|
|
|
|
fmt.Println(v.Encode())
|
|
url := "https://api.foursquare.com/v3/places/" + dq.fsqID + "?" + v.Encode()
|
|
|
|
fmt.Println(url)
|
|
|
|
return url
|
|
|
|
}
|
|
func PlacesPhotosURL(dq DetailQuery) string {
|
|
|
|
v, _ := query.Values(dq)
|
|
|
|
fmt.Println(v.Encode())
|
|
url := "https://api.foursquare.com/v3/places/" + dq.fsqID + "/photos"
|
|
|
|
fmt.Println(url)
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
func download(url string, apiKey string) (res []byte, err error) {
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
req.Header.Add("accept", "application/json")
|
|
req.Header.Add("Authorization", apiKey)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
fmt.Println("Http failed")
|
|
return nil, err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return body, nil
|
|
}
|