mirror of
https://github.com/grassrootseconomics/cic-go.git
synced 2024-11-24 22:56:45 +01:00
Mohamed Sohail
79634affb8
module: - cic-go packages: - cic/meta - cic/net other fixes: - nonce is now a settable value - package init outside test runner scope
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package meta
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
)
|
|
|
|
func generateMetaUrl(metaBaseUrl string, key string) string {
|
|
return fmt.Sprintf("%s/%s", metaBaseUrl, key)
|
|
}
|
|
|
|
func mergeSha256Key(x []byte, y []byte) string {
|
|
h := sha256.New()
|
|
|
|
h.Write(x)
|
|
h.Write(y)
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
func requestHandler(cicMeta *CicMeta, metadataKey string) ([]byte, error) {
|
|
resp, err := cicMeta.httpClient.Get(generateMetaUrl(cicMeta.baseUrl, metadataKey))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("error fetching metadata for key %s: %s", metadataKey, resp.Status)
|
|
}
|
|
|
|
respData, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return respData, nil
|
|
}
|
|
|
|
func jsonUnmarshaler[T PersonResponse | PreferencesResponse | CustomResponse](respBody []byte, binding T) (T, error) {
|
|
if err := json.Unmarshal(respBody, &binding); err != nil {
|
|
return binding, err
|
|
}
|
|
|
|
return binding, nil
|
|
}
|