mirror of
https://github.com/GrassrootsEconomics/cic-dw.git
synced 2024-12-22 02:47:32 +01:00
add: cic-meta client
This commit is contained in:
parent
e81a505681
commit
a6f24042f5
113
pkg/meta/meta.go
Normal file
113
pkg/meta/meta.go
Normal file
@ -0,0 +1,113 @@
|
||||
package meta
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
personPointer = ":cic.person"
|
||||
phonePointer = ":cic.phone"
|
||||
preferencesPointer = ":cic.preferences"
|
||||
customPointer = ":cic.custom"
|
||||
)
|
||||
|
||||
type CicMeta struct {
|
||||
httpClient *http.Client
|
||||
baseUrl string
|
||||
}
|
||||
|
||||
type CustomResponse struct {
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
type PreferencesResponse struct {
|
||||
PreferredLanguage string `json:"preferred_language"`
|
||||
}
|
||||
|
||||
type PersonResponse struct {
|
||||
DateRegistered int `json:"date_registered"`
|
||||
VCard string `json:"vcard"`
|
||||
Gender string `json:"gender"`
|
||||
Location Location `json:"location"`
|
||||
Products []string `json:"products"`
|
||||
DateOfBirth DateOfBirth `json:"date_of_birth"`
|
||||
}
|
||||
|
||||
type Location struct {
|
||||
AreaName string `json:"area_name"`
|
||||
}
|
||||
|
||||
type DateOfBirth struct {
|
||||
Year int `json:"year"`
|
||||
}
|
||||
|
||||
func NewCicMeta(metaEndpoint string) *CicMeta {
|
||||
return &CicMeta{
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 3,
|
||||
},
|
||||
baseUrl: metaEndpoint,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CicMeta) GetPhonePointer(phone string) (string, error) {
|
||||
hashedKey := mergeSha256Key([]byte(phone), []byte(phonePointer))
|
||||
|
||||
r, err := requestHandler(c, hashedKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return strings.Trim(string(r), "\""), nil
|
||||
}
|
||||
|
||||
func (c *CicMeta) GetPersonMetadata(address string) (PersonResponse, error) {
|
||||
hashedKey := mergeSha256Key(common.HexToAddress(address).Bytes(), []byte(personPointer))
|
||||
|
||||
respData, err := requestHandler(c, hashedKey)
|
||||
if err != nil {
|
||||
return PersonResponse{}, err
|
||||
}
|
||||
|
||||
metadata, err := jsonUnmarshaler(respData, PersonResponse{})
|
||||
if err != nil {
|
||||
return PersonResponse{}, err
|
||||
}
|
||||
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
func (c *CicMeta) GetPreferencesMetadata(address string) (PreferencesResponse, error) {
|
||||
hashedKey := mergeSha256Key(common.HexToAddress(address).Bytes(), []byte(preferencesPointer))
|
||||
|
||||
respData, err := requestHandler(c, hashedKey)
|
||||
if err != nil {
|
||||
return PreferencesResponse{}, err
|
||||
}
|
||||
|
||||
metadata, err := jsonUnmarshaler(respData, PreferencesResponse{})
|
||||
if err != nil {
|
||||
return PreferencesResponse{}, err
|
||||
}
|
||||
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
func (c *CicMeta) GetCustomMetadata(address string) (CustomResponse, error) {
|
||||
hashedKey := mergeSha256Key(common.HexToAddress(address).Bytes(), []byte(customPointer))
|
||||
|
||||
respData, err := requestHandler(c, hashedKey)
|
||||
if err != nil {
|
||||
return CustomResponse{}, err
|
||||
}
|
||||
|
||||
metadata, err := jsonUnmarshaler(respData, CustomResponse{})
|
||||
if err != nil {
|
||||
return CustomResponse{}, err
|
||||
}
|
||||
|
||||
return metadata, nil
|
||||
}
|
50
pkg/meta/util.go
Normal file
50
pkg/meta/util.go
Normal file
@ -0,0 +1,50 @@
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user