mirror of
https://github.com/sstent/go-garth.git
synced 2026-04-29 14:32:44 +00:00
sync
This commit is contained in:
@@ -2,6 +2,7 @@ package client_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"garmin-connect/garth/testutils"
|
||||
@@ -25,7 +26,9 @@ func TestClient_Login_Success(t *testing.T) {
|
||||
// Create client with test configuration
|
||||
c, err := client.NewClient("example.com")
|
||||
require.NoError(t, err)
|
||||
c.Domain = ssoServer.URL
|
||||
// Set domain to just the host (without scheme)
|
||||
u, _ := url.Parse(ssoServer.URL)
|
||||
c.Domain = u.Host
|
||||
|
||||
// Perform login
|
||||
err = c.Login("test@example.com", "password")
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"garmin-connect/garth/errors"
|
||||
@@ -30,6 +35,13 @@ func NewClient(domain string) (*Client, error) {
|
||||
domain = "garmin.com"
|
||||
}
|
||||
|
||||
// Extract host without scheme if present
|
||||
if strings.Contains(domain, "://") {
|
||||
if u, err := url.Parse(domain); err == nil {
|
||||
domain = u.Host
|
||||
}
|
||||
}
|
||||
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
return nil, &errors.IOError{
|
||||
@@ -63,7 +75,15 @@ func NewClient(domain string) (*Client, error) {
|
||||
|
||||
// Login authenticates to Garmin Connect using SSO
|
||||
func (c *Client) Login(email, password string) error {
|
||||
ssoClient := sso.NewClient(c.Domain)
|
||||
// Extract host without scheme if present
|
||||
host := c.Domain
|
||||
if strings.Contains(host, "://") {
|
||||
if u, err := url.Parse(host); err == nil {
|
||||
host = u.Host
|
||||
}
|
||||
}
|
||||
|
||||
ssoClient := sso.NewClient(host)
|
||||
oauth2Token, mfaContext, err := ssoClient.Login(email, password)
|
||||
if err != nil {
|
||||
return &errors.AuthenticationError{
|
||||
@@ -102,7 +122,7 @@ func (c *Client) Login(email, password string) error {
|
||||
}
|
||||
|
||||
// GetUserProfile retrieves the current user's full profile
|
||||
func (c *Client) GetUserProfile() (*UserProfile, error) {
|
||||
func (c *Client) GetUserProfile() (*types.UserProfile, error) {
|
||||
profileURL := fmt.Sprintf("https://connectapi.%s/userprofile-service/socialProfile", c.Domain)
|
||||
|
||||
req, err := http.NewRequest("GET", profileURL, nil)
|
||||
@@ -146,7 +166,7 @@ func (c *Client) GetUserProfile() (*UserProfile, error) {
|
||||
}
|
||||
}
|
||||
|
||||
var profile UserProfile
|
||||
var profile types.UserProfile
|
||||
if err := json.NewDecoder(resp.Body).Decode(&profile); err != nil {
|
||||
return nil, &errors.IOError{
|
||||
GarthError: errors.GarthError{
|
||||
@@ -159,6 +179,153 @@ func (c *Client) GetUserProfile() (*UserProfile, error) {
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
// ConnectAPI makes a raw API request to the Garmin Connect API
|
||||
func (c *Client) ConnectAPI(path string, method string, params url.Values, body io.Reader) ([]byte, error) {
|
||||
u := &url.URL{
|
||||
Scheme: "https",
|
||||
Host: fmt.Sprintf("connectapi.%s", c.Domain),
|
||||
Path: path,
|
||||
RawQuery: params.Encode(),
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, u.String(), body)
|
||||
if err != nil {
|
||||
return nil, &errors.APIError{
|
||||
GarthHTTPError: errors.GarthHTTPError{
|
||||
GarthError: errors.GarthError{
|
||||
Message: "Failed to create request",
|
||||
Cause: err,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", c.AuthToken)
|
||||
req.Header.Set("User-Agent", "garth-go-client/1.0")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
if body != nil && req.Header.Get("Content-Type") == "" {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, &errors.APIError{
|
||||
GarthHTTPError: errors.GarthHTTPError{
|
||||
GarthError: errors.GarthError{
|
||||
Message: "Request failed",
|
||||
Cause: err,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||
return nil, &errors.APIError{
|
||||
GarthHTTPError: errors.GarthHTTPError{
|
||||
StatusCode: resp.StatusCode,
|
||||
Response: string(bodyBytes),
|
||||
GarthError: errors.GarthError{
|
||||
Message: fmt.Sprintf("API request failed with status %d: %s",
|
||||
resp.StatusCode, tryReadErrorBody(bytes.NewReader(bodyBytes))),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
func tryReadErrorBody(r io.Reader) string {
|
||||
body, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return "failed to read error response"
|
||||
}
|
||||
return string(body)
|
||||
}
|
||||
|
||||
// Upload sends a file to Garmin Connect
|
||||
func (c *Client) Upload(filePath string) error {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return &errors.IOError{
|
||||
GarthError: errors.GarthError{
|
||||
Message: "Failed to open file",
|
||||
Cause: err,
|
||||
},
|
||||
}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
part, err := writer.CreateFormFile("file", filepath.Base(filePath))
|
||||
if err != nil {
|
||||
return &errors.IOError{
|
||||
GarthError: errors.GarthError{
|
||||
Message: "Failed to create form file",
|
||||
Cause: err,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := io.Copy(part, file); err != nil {
|
||||
return &errors.IOError{
|
||||
GarthError: errors.GarthError{
|
||||
Message: "Failed to copy file content",
|
||||
Cause: err,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if err := writer.Close(); err != nil {
|
||||
return &errors.IOError{
|
||||
GarthError: errors.GarthError{
|
||||
Message: "Failed to close multipart writer",
|
||||
Cause: err,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
_, err = c.ConnectAPI("/upload-service/upload", "POST", nil, body)
|
||||
if err != nil {
|
||||
return &errors.APIError{
|
||||
GarthHTTPError: errors.GarthHTTPError{
|
||||
GarthError: errors.GarthError{
|
||||
Message: "File upload failed",
|
||||
Cause: err,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Download retrieves a file from Garmin Connect
|
||||
func (c *Client) Download(activityID string, filePath string) error {
|
||||
params := url.Values{}
|
||||
params.Add("activityId", activityID)
|
||||
|
||||
resp, err := c.ConnectAPI("/download-service/export", "GET", params, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filePath, resp, 0644); err != nil {
|
||||
return &errors.IOError{
|
||||
GarthError: errors.GarthError{
|
||||
Message: "Failed to save file",
|
||||
Cause: err,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetActivities retrieves recent activities
|
||||
func (c *Client) GetActivities(limit int) ([]types.Activity, error) {
|
||||
if limit <= 0 {
|
||||
|
||||
@@ -1,130 +1,4 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"garmin-connect/garth/errors"
|
||||
)
|
||||
|
||||
func (c *Client) ConnectAPI(path, method string, data interface{}) (interface{}, error) {
|
||||
url := fmt.Sprintf("https://connectapi.%s%s", c.Domain, path)
|
||||
|
||||
var body io.Reader
|
||||
if data != nil && (method == "POST" || method == "PUT") {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, &errors.APIError{GarthHTTPError: errors.GarthHTTPError{
|
||||
GarthError: errors.GarthError{Message: "Failed to marshal request data", Cause: err}}}
|
||||
}
|
||||
body = bytes.NewReader(jsonData)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, &errors.APIError{GarthHTTPError: errors.GarthHTTPError{
|
||||
GarthError: errors.GarthError{Message: "Failed to create request", Cause: err}}}
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", c.AuthToken)
|
||||
req.Header.Set("User-Agent", "GCM-iOS-5.7.2.1")
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, &errors.APIError{GarthHTTPError: errors.GarthHTTPError{
|
||||
GarthError: errors.GarthError{Message: "API request failed", Cause: err}}}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == 204 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||
return nil, &errors.APIError{GarthHTTPError: errors.GarthHTTPError{
|
||||
StatusCode: resp.StatusCode,
|
||||
Response: string(bodyBytes),
|
||||
GarthError: errors.GarthError{Message: "API error"}}}
|
||||
}
|
||||
|
||||
var result interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return nil, &errors.IOError{GarthError: errors.GarthError{
|
||||
Message: "Failed to parse response", Cause: err}}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *Client) Download(path string) ([]byte, error) {
|
||||
url := fmt.Sprintf("https://connectapi.%s%s", c.Domain, path)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", c.AuthToken)
|
||||
req.Header.Set("User-Agent", "GCM-iOS-5.7.2.1")
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
func (c *Client) Upload(filePath, uploadPath string) (map[string]interface{}, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, &errors.IOError{GarthError: errors.GarthError{
|
||||
Message: "Failed to open file", Cause: err}}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var b bytes.Buffer
|
||||
writer := multipart.NewWriter(&b)
|
||||
part, err := writer.CreateFormFile("file", filepath.Base(filePath))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = io.Copy(part, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
writer.Close()
|
||||
|
||||
url := fmt.Sprintf("https://connectapi.%s%s", c.Domain, uploadPath)
|
||||
req, err := http.NewRequest("POST", url, &b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", c.AuthToken)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
// This file intentionally left blank.
|
||||
// All HTTP client methods are now implemented in client.go.
|
||||
|
||||
11
garth/client/http_client.go
Normal file
11
garth/client/http_client.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// HTTPClient defines the interface for HTTP operations
|
||||
type HTTPClient interface {
|
||||
ConnectAPI(path string, method string, params url.Values, body io.Reader) ([]byte, error)
|
||||
}
|
||||
Reference in New Issue
Block a user