refactor: deduplicate building api urls

This commit is contained in:
Derrick Hammer 2023-05-22 17:14:32 -04:00
parent 4b712a3a80
commit 6d5b9d880b
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 38 additions and 4 deletions

View File

@ -65,7 +65,7 @@ func Upload(r io.ReadSeeker, size int64, hash []byte) (model.Upload, error) {
}
}
objectExistsResult, err := client.R().Get(fmt.Sprintf("/worker/objects/%s", hashHex))
objectExistsResult, err := client.R().Get(getBusObjectUrl(hashHex))
if err != nil {
shared.GetLogger().Error("Failed query object", zap.Error(err))
@ -84,7 +84,7 @@ func Upload(r io.ReadSeeker, size int64, hash []byte) (model.Upload, error) {
objectStatusCode = 404
}
proofExistsResult, err := client.R().Get(fmt.Sprintf("/worker/objects/%s.obao", hashHex))
proofExistsResult, err := client.R().Get(getBusProofUrl(hashHex))
if err != nil {
shared.GetLogger().Error("Failed query object proof", zap.Error(err))
@ -109,14 +109,14 @@ func Upload(r io.ReadSeeker, size int64, hash []byte) (model.Upload, error) {
return upload, errors.New(msg)
}
ret, err := client.R().SetBody(r).Put(fmt.Sprintf("/worker/objects/%s", hashHex))
ret, err := client.R().SetBody(r).Put(getWorkerObjectUrl(hashHex))
if ret.StatusCode() != 200 {
shared.GetLogger().Error("Failed uploading object", zap.String("error", ret.String()))
err = errors.New(ret.String())
return upload, err
}
ret, err = client.R().SetBody(tree).Put(fmt.Sprintf("/worker/objects/%s.obao", hashHex))
ret, err = client.R().SetBody(tree).Put(getWorkerProofUrl(hashHex))
if ret.StatusCode() != 200 {
shared.GetLogger().Error("Failed uploading proof", zap.String("error", ret.String()))
err = errors.New(ret.String())
@ -172,3 +172,37 @@ func Download(hash string) (io.Reader, error) {
return nil, errors.New("invalid file")
}
}
func objectUrlBuilder(hash string, bus bool, proof bool) string {
path := []string{}
if bus {
path = append(path, "bus")
} else {
path = append(path, "worker")
}
path = append(path, "objects")
name := "%s"
if proof {
name = name + ".obao"
}
path = append(path, name)
return fmt.Sprintf(strings.Join(path, "/"), hash)
}
func getBusObjectUrl(hash string) string {
return objectUrlBuilder(hash, true, false)
}
func getWorkerObjectUrl(hash string) string {
return objectUrlBuilder(hash, false, false)
}
func getWorkerProofUrl(hash string) string {
return objectUrlBuilder(hash, false, true)
}
func getBusProofUrl(hash string) string {
return objectUrlBuilder(hash, true, true)
}