Skip to content

Commit

Permalink
search: read contact info from API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 15, 2025
1 parent 7932c46 commit f7a32b0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
34 changes: 25 additions & 9 deletions internal/search/api_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,14 @@ func readSearchRequest(r *http.Request) (search.Entity[search.Value], error) {
}
}

req.CryptoAddresses = readCryptoCurrencyAddresses(q["cryptoAddress"])
// contact info // TODO(adam): normalize
req.Contact.EmailAddresses = readStrings(q["email"], q["emailAddress"])
req.Contact.PhoneNumbers = readStrings(q["phone"], q["phoneNumber"])
req.Contact.FaxNumbers = readStrings(q["fax"], q["faxNumber"])
req.Contact.Websites = readStrings(q["website"])

req.Addresses = readAddresses(q["address"])
req.CryptoAddresses = readCryptoCurrencyAddresses(q["cryptoAddress"])

// TODO(adam):
// Affiliations []Affiliation `json:"affiliations"`
Expand Down Expand Up @@ -230,6 +236,24 @@ func readInt(input string) (int, error) {
return int(n), err
}

func readStrings(inputs ...[]string) []string {
var out []string
for _, items := range inputs {
for _, item := range items {
out = append(out, strings.TrimSpace(item))
}
}
return out
}

func readAddresses(inputs []string) []search.Address {
var out []search.Address
for _, input := range inputs {
out = append(out, address.ParseAddress(input))
}
return out
}

func readCryptoCurrencyAddresses(inputs []string) []search.CryptoAddress {
var out []search.CryptoAddress
for _, input := range inputs {
Expand All @@ -244,11 +268,3 @@ func readCryptoCurrencyAddresses(inputs []string) []search.CryptoAddress {
}
return out
}

func readAddresses(inputs []string) []search.Address {
var out []search.Address
for _, input := range inputs {
out = append(out, address.ParseAddress(input))
}
return out
}
23 changes: 23 additions & 0 deletions internal/search/api_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ func TestAPI_readSearchRequest(t *testing.T) {

})

t.Run("contact info", func(t *testing.T) {
address := "/v2/search?type=business&[email protected]&phone=1234567890"
address += "&faxNumber=3334445566&[email protected]&phone=9876543210"
address += "&website=corp.com&website=corp2.com"

req := httptest.NewRequest("GET", address, nil)

query, err := readSearchRequest(req)
require.NoError(t, err)
require.Empty(t, query.Name)

expected := search.ContactInfo{
EmailAddresses: []string{"[email protected]", "[email protected]"},
PhoneNumbers: []string{"1234567890", "9876543210"},
FaxNumbers: []string{"3334445566"},
Websites: []string{"corp.com", "corp2.com"},
}
require.ElementsMatch(t, expected.EmailAddresses, query.Contact.EmailAddresses)
require.ElementsMatch(t, expected.PhoneNumbers, query.Contact.PhoneNumbers)
require.ElementsMatch(t, expected.FaxNumbers, query.Contact.FaxNumbers)
require.ElementsMatch(t, expected.Websites, query.Contact.Websites)
})

t.Run("crypto addresses", func(t *testing.T) {
req := httptest.NewRequest("GET", "/v2/search?type=person&cryptoAddress=xbt:12345&cryptoAddress=eth:54321", nil)

Expand Down

0 comments on commit f7a32b0

Please sign in to comment.