Files
tinder-api-wrapper/cmd/server/handlers/core.go

77 lines
1.8 KiB
Go

package handlers
import (
"fmt"
"net/http"
"strings"
tinder "tinder-api-wrapper"
)
// HandleLike handles the /like/{userId} endpoint
func HandleLike(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
path := strings.TrimPrefix(r.URL.Path, "/like/")
if path == "" {
http.Error(w, "User ID is required", http.StatusBadRequest)
return
}
resp, err := client.LikeUser(path)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}
// HandlePass handles the /pass/{userId} endpoint
func HandlePass(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
path := strings.TrimPrefix(r.URL.Path, "/pass/")
if path == "" {
http.Error(w, "User ID is required", http.StatusBadRequest)
return
}
resp, err := client.PassUser(path)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}
// HandleGetRecs handles the /v2/recs/core endpoint
func HandleGetRecs(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
locale := r.URL.Query().Get("locale")
resp, err := client.GetRecommendations(locale)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}