65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package nextcloud
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
baseURL string
|
|
httpClient *http.Client
|
|
adminUser string
|
|
adminPass string
|
|
}
|
|
|
|
func NewClient(baseURL, adminUser, adminPass string) *Client {
|
|
return &Client{
|
|
baseURL: baseURL,
|
|
httpClient: &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
},
|
|
adminUser: adminUser,
|
|
adminPass: adminPass,
|
|
}
|
|
}
|
|
|
|
func (c *Client) doRequest(ctx context.Context, method, path string, body io.Reader, headers map[string]string) (*http.Response, error) {
|
|
url := c.baseURL + path
|
|
req, err := http.NewRequestWithContext(ctx, method, url, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.SetBasicAuth(c.adminUser, c.adminPass)
|
|
req.Header.Set("OCS-APIRequest", "true")
|
|
for k, v := range headers {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
return c.httpClient.Do(req)
|
|
}
|
|
|
|
func (c *Client) DoAsUser(ctx context.Context, method, path string, body io.Reader, userID string, headers map[string]string) (*http.Response, error) {
|
|
url := c.baseURL + path
|
|
req, err := http.NewRequestWithContext(ctx, method, url, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.SetBasicAuth(c.adminUser, c.adminPass)
|
|
req.Header.Set("OCS-APIRequest", "true")
|
|
req.Header.Set("X-NC-User", userID)
|
|
for k, v := range headers {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
return c.httpClient.Do(req)
|
|
}
|
|
|
|
func (c *Client) WebDAVPath(userID, path string) string {
|
|
return fmt.Sprintf("/remote.php/dav/files/%s/%s", userID, path)
|
|
}
|