-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DHClient can have a dhstore URL or API
DHashClient can be given a dhstore API interface when the client within dhstore, This is so that dhstore can give it a set of internal functions to call to do dh multihash and metadata lookups. Otherwise, a client that is not in dhstore uses the dhstore URL to perfrom remote dhstore lookups.
- Loading branch information
Showing
3 changed files
with
136 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/ipni/go-libipni/apierror" | ||
"github.com/ipni/go-libipni/dhash" | ||
"github.com/ipni/go-libipni/find/model" | ||
b58 "github.com/mr-tron/base58/base58" | ||
"github.com/multiformats/go-multihash" | ||
) | ||
|
||
type dhstoreHTTP struct { | ||
c *http.Client | ||
dhFindURL *url.URL | ||
dhMetadataURL *url.URL | ||
} | ||
|
||
func (d *dhstoreHTTP) FindMultihash(ctx context.Context, dhmh multihash.Multihash) ([]model.EncryptedMultihashResult, error) { | ||
u := d.dhFindURL.JoinPath(dhmh.B58String()) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("Accept", "application/json") | ||
|
||
resp, err := d.c.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
defer resp.Body.Close() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, apierror.FromResponse(resp.StatusCode, body) | ||
} | ||
|
||
encResponse := &model.FindResponse{} | ||
err = json.Unmarshal(body, encResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return encResponse.EncryptedMultihashResults, nil | ||
} | ||
|
||
func (d *dhstoreHTTP) FindMetadata(ctx context.Context, vk []byte) ([]byte, error) { | ||
u := d.dhMetadataURL.JoinPath(b58.Encode(dhash.SHA256(vk, nil))) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("Accept", "application/json") | ||
|
||
resp, err := d.c.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
defer resp.Body.Close() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, apierror.FromResponse(resp.StatusCode, body) | ||
} | ||
|
||
type GetMetadataResponse struct { | ||
EncryptedMetadata []byte `json:"EncryptedMetadata"` | ||
} | ||
|
||
findResponse := &GetMetadataResponse{} | ||
err = json.Unmarshal(body, findResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return findResponse.EncryptedMetadata, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters