code cleanup

This commit is contained in:
Philipp Häfelfinger 2019-02-24 21:28:07 +01:00
parent ff00ca9f0a
commit ca43177d49

View File

@ -11,7 +11,6 @@ import (
) )
func Login(context *PiwigoContext) { func Login(context *PiwigoContext) {
logrus.Debugf("Logging in to %s using user %s", context.Url, context.Username) logrus.Debugf("Logging in to %s using user %s", context.Url, context.Username)
if !strings.HasPrefix(context.Url, "https") { if !strings.HasPrefix(context.Url, "https") {
@ -29,10 +28,15 @@ func Login(context *PiwigoContext) {
response, err := client.PostForm(context.Url, formData) response, err := client.PostForm(context.Url, formData)
if err == nil { if err != nil {
logrus.Errorln("The HTTP request failed with error %s", err)
panic(err)
}
var loginResponse LoginResponse var loginResponse LoginResponse
if err := json.NewDecoder(response.Body).Decode(&loginResponse); err != nil { if err := json.NewDecoder(response.Body).Decode(&loginResponse); err != nil {
logrus.Errorln(err) logrus.Errorln(err)
panic(err)
} }
if loginResponse.Status != "ok" { if loginResponse.Status != "ok" {
@ -42,9 +46,6 @@ func Login(context *PiwigoContext) {
} }
logrus.Infof("Login succeeded: %s", loginResponse.Status) logrus.Infof("Login succeeded: %s", loginResponse.Status)
} else {
logrus.Errorln("The HTTP request failed with error %s", err)
}
} }
func Logout(context *PiwigoContext) { func Logout(context *PiwigoContext) {
@ -58,19 +59,20 @@ func Logout(context *PiwigoContext) {
client := http.Client{Jar: context.Cookies} client := http.Client{Jar: context.Cookies}
response, err := client.PostForm(context.Url, formData) response, err := client.PostForm(context.Url, formData)
if err == nil { if err != nil {
logrus.Errorln("The HTTP request failed with error %s", err)
return
}
var statusResponse LogoutResponse var statusResponse LogoutResponse
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil { if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
logrus.Errorln(err) logrus.Errorln(err)
} }
if statusResponse.Status == "ok" { if statusResponse.Status != "ok" {
logrus.Infof("Successfully logged out from %s", context.Url)
} else {
logrus.Errorf("Logout from %s failed", context.Url) logrus.Errorf("Logout from %s failed", context.Url)
}
} else { } else {
logrus.Errorln("The HTTP request failed with error %s", err) logrus.Infof("Successfully logged out from %s", context.Url)
} }
} }
@ -86,7 +88,11 @@ func GetStatus(context *PiwigoContext) *GetStatusResponse {
client := http.Client{Jar: context.Cookies} client := http.Client{Jar: context.Cookies}
response, err := client.PostForm(context.Url, formData) response, err := client.PostForm(context.Url, formData)
if err == nil { if err != nil {
logrus.Errorln("The HTTP request failed with error %s\n", err)
return nil
}
var statusResponse GetStatusResponse var statusResponse GetStatusResponse
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil { if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
logrus.Errorln(err) logrus.Errorln(err)
@ -97,10 +103,6 @@ func GetStatus(context *PiwigoContext) *GetStatusResponse {
} }
return &statusResponse return &statusResponse
} else {
logrus.Errorln("The HTTP request failed with error %s\n", err)
}
return nil
} }
func initializeCookieJarIfRequired(context *PiwigoContext) { func initializeCookieJarIfRequired(context *PiwigoContext) {