use http.Client for API key in immich
This commit is contained in:
@@ -28,7 +28,27 @@ type bulkIdResponseDto struct {
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*uploadResponse, error) {
|
||||
type APIKeyTransport struct {
|
||||
APIKey string
|
||||
}
|
||||
|
||||
func (t *APIKeyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req = req.Clone(req.Context())
|
||||
req.Header.Add("x-api-key", t.APIKey)
|
||||
return http.DefaultTransport.RoundTrip(req)
|
||||
}
|
||||
|
||||
func newImmichHttpClient(apiKey string) http.Client {
|
||||
transport := APIKeyTransport{
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
return http.Client{
|
||||
Transport: &transport,
|
||||
}
|
||||
}
|
||||
|
||||
func uploadToImmich(filePath, appID, baseURL, deviceID string, httpClient *http.Client) (*uploadResponse, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open file: %w", err)
|
||||
@@ -66,12 +86,11 @@ func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*uploadR
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
req.Header.Set("x-api-key", apiKey)
|
||||
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
client := &http.Client{Timeout: 60 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("POST to Immich: %w", err)
|
||||
}
|
||||
@@ -90,7 +109,7 @@ func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*uploadR
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func setAssetMetadata(assets []string, description, baseURL, apiKey string) error {
|
||||
func setAssetMetadata(assets []string, description, baseURL string, httpClient http.Client) error {
|
||||
body, _ := json.Marshal(assetPutRequest{
|
||||
Ids: assets,
|
||||
Description: description,
|
||||
@@ -101,11 +120,10 @@ func setAssetMetadata(assets []string, description, baseURL, apiKey string) erro
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("x-api-key", apiKey)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
_, err = http.DefaultClient.Do(req)
|
||||
_, err = httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -113,7 +131,7 @@ func setAssetMetadata(assets []string, description, baseURL, apiKey string) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
func addAssetsToAlbum(assets []string, album, baseURL, apiKey string) error {
|
||||
func addAssetsToAlbum(assets []string, album, baseURL string, httpClient http.Client) error {
|
||||
body, _ := json.Marshal(struct {
|
||||
IDs []string `json:"ids"`
|
||||
}{IDs: assets})
|
||||
@@ -123,11 +141,10 @@ func addAssetsToAlbum(assets []string, album, baseURL, apiKey string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("x-api-key", apiKey)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
res, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user