mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-11-23 22:04:50 +02:00
api_utils: expose missing REST API
REST API are now fully exposed and can be consumed by other packages using the methods in api_utils.go
This commit is contained in:
@@ -180,6 +180,35 @@ func StartQuotaScan(user dataprovider.User, expectedStatusCode int) error {
|
||||
return checkResponse(resp.StatusCode, expectedStatusCode, resp)
|
||||
}
|
||||
|
||||
// GetSFTPConnections returns status and stats for active SFTP connections
|
||||
func GetSFTPConnections(expectedStatusCode int) ([]sftpd.ConnectionStatus, error) {
|
||||
var connections []sftpd.ConnectionStatus
|
||||
resp, err := getHTTPClient().Get(httpBaseURL + activeConnectionsPath)
|
||||
if err != nil {
|
||||
return connections, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
err = checkResponse(resp.StatusCode, expectedStatusCode, resp)
|
||||
if err == nil && expectedStatusCode == http.StatusOK {
|
||||
err = render.DecodeJSON(resp.Body, &connections)
|
||||
}
|
||||
return connections, err
|
||||
}
|
||||
|
||||
// CloseSFTPConnection closes an active SFTP connection identified by connectionID
|
||||
func CloseSFTPConnection(connectionID string, expectedStatusCode int) error {
|
||||
req, err := http.NewRequest(http.MethodDelete, httpBaseURL+activeConnectionsPath+"/"+connectionID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := getHTTPClient().Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return checkResponse(resp.StatusCode, expectedStatusCode, resp)
|
||||
}
|
||||
|
||||
func checkResponse(actual int, expected int, resp *http.Response) error {
|
||||
if expected != actual {
|
||||
return fmt.Errorf("wrong status code: got %v want %v", actual, expected)
|
||||
|
||||
Reference in New Issue
Block a user