Set user agent in getting HTTP status code

Close #8
This commit is contained in:
Joshua Ramon Enslin 2025-02-25 22:14:27 +01:00
parent 3cb49d005e
commit 5afc375042
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
4 changed files with 33 additions and 6 deletions

View File

@ -0,0 +1,28 @@
package configloader
import (
"net/http"
)
// Returns the HTTP status code for a url while sending requests using
// a user-agent specific to the app.
func HttpGetStatusCode(url string) (int, error) {
// create HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return 0, err
}
// set User-Agent header
req.Header.Set("User-Agent", "museum-digital-uploader")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
return resp.StatusCode, nil
}

View File

@ -3,7 +3,6 @@ package configloader
import (
"errors"
"net/url"
"net/http"
"strings"
)
@ -29,11 +28,11 @@ func ValidateInstanceLink(instanceUrl string) (string, error) {
Host: parsed.Host,
Path: "/musdb",
}
resp, err := http.Get(musdbUrl.String())
statusCode, err := HttpGetStatusCode(musdbUrl.String())
if err != nil {
return "", err
}
if resp.StatusCode > 399 {
if statusCode > 399 {
return "", errors.New("The museum-digital subdomain does not contain a musdb path")
}

View File

@ -2,7 +2,6 @@ package configloader
import (
"errors"
"net/http"
"strconv"
)
@ -16,12 +15,12 @@ func ValidateInstitutionId(institutionId int, instanceUrl string) (int, error) {
return 0, errors.New("Institution ID cannot be negative")
}
resp, err := http.Get(instanceUrl + "/institution/" + strconv.Itoa(institutionId))
statusCode, err := HttpGetStatusCode(instanceUrl + "/institution/" + strconv.Itoa(institutionId))
if err != nil {
return 0, err
}
if resp.StatusCode != 200 {
if statusCode != 200 {
return 0, errors.New("The institution page does not respond with HTTP 200, the institution does not seem to exist")
}

View File

@ -9,6 +9,7 @@ import (
type MDWebDavUploaderConfig struct {
InstanceLink string `json:"instance"`
Username string `json:"username"`
Mail string `json:"mail"`
WebDavAuthToken string `json:"token"`
InstitutionId int `json:"institution_id"`