Files
tinder-api-wrapper/client.go

144 lines
3.7 KiB
Go

package tinder
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
// Client represents a Tinder API client
type Client struct {
baseURL *url.URL
httpClient *http.Client
authToken string
}
// NewClient creates a new Tinder API client with the specified endpoint
func NewClient(endpoint string) (*Client, error) {
baseURL, err := url.Parse(endpoint)
if err != nil {
return nil, fmt.Errorf("invalid API endpoint: %v", err)
}
return &Client{
baseURL: baseURL,
httpClient: &http.Client{},
}, nil
}
// WithAuthToken sets the authentication token for the client
func (c *Client) WithAuthToken(token string) *Client {
c.authToken = token
return c
}
// doRequest performs an HTTP request with proper authentication headers
func (c *Client) doRequest(method, path string, body interface{}) (*http.Response, error) {
endpoint := c.baseURL.ResolveReference(&url.URL{Path: path})
var req *http.Request
var err error
if body != nil {
jsonData, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %v", err)
}
req, err = http.NewRequest(method, endpoint.String(), bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
} else {
req, err = http.NewRequest(method, endpoint.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
}
if c.authToken != "" {
req.Header.Set("X-Auth-Token", c.authToken)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("API request failed: %v", err)
}
return resp, nil
}
// LikeUser sends a like action to a specific user
func (c *Client) LikeUser(userID string) (*LikeResponse, error) {
resp, err := c.doRequest(http.MethodGet, fmt.Sprintf("/like/%s", userID), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("API error: %s (status code: %d)", body, resp.StatusCode)
}
var likeResp LikeResponse
if err := json.NewDecoder(resp.Body).Decode(&likeResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %v", err)
}
return &likeResp, nil
}
// PassUser sends a pass action for a specific user
func (c *Client) PassUser(userID string) (*PassResponse, error) {
resp, err := c.doRequest(http.MethodGet, fmt.Sprintf("/pass/%s", userID), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("API error: %s (status code: %d)", body, resp.StatusCode)
}
var passResp PassResponse
if err := json.NewDecoder(resp.Body).Decode(&passResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %v", err)
}
return &passResp, nil
}
// GetRecommendations retrieves a list of recommended user profiles
func (c *Client) GetRecommendations(locale string) (*RecsResponse, error) {
endpoint := c.baseURL.ResolveReference(&url.URL{Path: "/v2/recs/core"})
if locale != "" {
query := endpoint.Query()
query.Set("locale", locale)
endpoint.RawQuery = query.Encode()
}
resp, err := c.doRequest(http.MethodGet, endpoint.Path+"?"+endpoint.RawQuery, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("API error: %s (status code: %d)", body, resp.StatusCode)
}
var recsResp RecsResponse
if err := json.NewDecoder(resp.Body).Decode(&recsResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %v", err)
}
return &recsResp, nil
}