2018-07-25 13:06:31 +00:00
|
|
|
package gonextcloud
|
2018-07-09 10:53:53 +00:00
|
|
|
|
|
|
|
import (
|
2018-07-25 13:06:31 +00:00
|
|
|
"encoding/json"
|
2018-07-09 10:53:53 +00:00
|
|
|
req "github.com/levigross/grequests"
|
2018-10-26 13:35:50 +00:00
|
|
|
"gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types"
|
2018-07-09 10:53:53 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2018-07-24 18:26:12 +00:00
|
|
|
"strings"
|
2018-07-09 10:53:53 +00:00
|
|
|
)
|
|
|
|
|
2018-09-03 10:17:02 +00:00
|
|
|
func (c *Client) baseRequest(method string, route *url.URL, ro *req.RequestOptions, subRoutes ...string) (*req.Response, error) {
|
2018-07-09 10:53:53 +00:00
|
|
|
if !c.loggedIn() {
|
2018-07-30 08:08:35 +00:00
|
|
|
return nil, errUnauthorized
|
2018-07-09 10:53:53 +00:00
|
|
|
}
|
|
|
|
u := c.baseURL.ResolveReference(route)
|
2018-09-03 10:17:02 +00:00
|
|
|
|
|
|
|
for _, sr := range subRoutes {
|
|
|
|
if sr != "" {
|
|
|
|
u.Path = path.Join(u.Path, sr)
|
|
|
|
}
|
2018-07-09 10:53:53 +00:00
|
|
|
}
|
|
|
|
var (
|
|
|
|
res *req.Response
|
|
|
|
err error
|
|
|
|
)
|
2018-09-03 10:17:02 +00:00
|
|
|
switch method {
|
|
|
|
case http.MethodGet:
|
2018-07-09 10:53:53 +00:00
|
|
|
res, err = c.session.Get(u.String(), ro)
|
2018-09-03 10:17:02 +00:00
|
|
|
case http.MethodPost:
|
2018-07-09 10:53:53 +00:00
|
|
|
res, err = c.session.Post(u.String(), ro)
|
2018-09-03 10:17:02 +00:00
|
|
|
case http.MethodPut:
|
2018-07-09 10:53:53 +00:00
|
|
|
res, err = c.session.Put(u.String(), ro)
|
2018-09-03 10:17:02 +00:00
|
|
|
case http.MethodDelete:
|
2018-07-09 10:53:53 +00:00
|
|
|
res, err = c.session.Delete(u.String(), ro)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-25 13:06:31 +00:00
|
|
|
// As we cannot read the ReaderCloser twice, we use the string content
|
|
|
|
js := res.String()
|
|
|
|
var r types.BaseResponse
|
|
|
|
json.Unmarshal([]byte(js), &r)
|
2018-09-03 10:17:02 +00:00
|
|
|
if r.Ocs.Meta.Statuscode == 200 || r.Ocs.Meta.Statuscode == 100 {
|
|
|
|
return res, nil
|
2018-07-25 13:06:31 +00:00
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
err = types.ErrorFromMeta(r.Ocs.Meta)
|
|
|
|
return nil, err
|
2018-07-09 10:53:53 +00:00
|
|
|
}
|
2018-07-24 18:26:12 +00:00
|
|
|
|
|
|
|
func reformatJSON(json string) string {
|
|
|
|
// Nextcloud encode boolean as string
|
|
|
|
json = strings.Replace(json, "\"true\"", "true", -1)
|
|
|
|
json = strings.Replace(json, "\"false\"", "false", -1)
|
|
|
|
// Nextcloud encode quota as an empty array for never connected users
|
|
|
|
json = strings.Replace(json, "\"quota\":[],", "", -1)
|
2019-01-16 14:31:22 +00:00
|
|
|
// Nextcloud send admin unlimited quota as -3, others as "none" : replace with negative value
|
|
|
|
json = strings.Replace(json, "\"quota\":\"none\"", "\"quota\":-3", -1)
|
2018-07-24 18:26:12 +00:00
|
|
|
return json
|
|
|
|
}
|