Skip to content

Commit

Permalink
fix(dispatch): send source over wire and cleanup attr definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Arqu committed Apr 16, 2021
1 parent e074bf4 commit 8a2ddf8
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 77 deletions.
4 changes: 2 additions & 2 deletions api/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func TestHTTPClient(t *testing.T) {
httpClient.Address = sURL.Host
httpClient.Protocol = "http"

err = httpClient.CallRaw(ctx, lib.AEHome, nil, &bytes.Buffer{})
err = httpClient.CallRaw(ctx, lib.AEHome, "", nil, &bytes.Buffer{})
if err != nil {
t.Fatal(err.Error())
}

res := []dsref.VersionInfo{}
p := lib.ListParams{}
err = httpClient.CallMethod(ctx, lib.AEList, http.MethodPost, p, &res)
err = httpClient.CallMethod(ctx, lib.AEList, http.MethodPost, "", p, &res)
if err != nil {
t.Fatal(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion lib/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (m AccessMethods) Name() string {
// Attributes defines attributes for each method
func (m AccessMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"createauthtoken": {AECreateAuthToken, "GET", "local"},
"createauthtoken": {endpoint: AECreateAuthToken, httpVerb: "GET", defaultSource: "local"},
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/automation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (m AutomationMethods) Name() string {
// Attributes defines attributes for each method
func (m AutomationMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"apply": {AEApply, "POST", ""},
"apply": {endpoint: AEApply, httpVerb: "POST"},
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func (m CollectionMethods) Name() string {
// Attributes defines attributes for each method
func (m CollectionMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"list": {AEList, "POST", ""},
"listrawrefs": {denyRPC, "", ""},
"list": {endpoint: AEList, httpVerb: "POST"},
"listrawrefs": {endpoint: denyRPC},
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func (m ConfigMethods) Name() string {
func (m ConfigMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
// config methods are not allowed over HTTP nor RPC
"getconfig": {denyRPC, "", ""},
"getconfigkeys": {denyRPC, "", ""},
"setconfig": {denyRPC, "", ""},
"getconfig": {endpoint: denyRPC},
"getconfigkeys": {endpoint: denyRPC},
"setconfig": {endpoint: denyRPC},
}
}

Expand Down
28 changes: 14 additions & 14 deletions lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ func (m DatasetMethods) Name() string {
// Attributes defines attributes for each method
func (m DatasetMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"componentstatus": {AEComponentStatus, "POST", ""},
"get": {AEGet, "GET", ""},
// "log": {AELog, "POST"},
"rename": {AERename, "POST", ""},
"save": {AESave, "POST", ""},
"pull": {AEPull, "POST", "network"},
// "push": {AEPush, "POST"},
"render": {AERender, "POST", ""},
"remove": {AERemove, "POST", "local"},
"validate": {AEValidate, "POST", ""},
// "unpack": {AEUnpack, "POST"},
"manifest": {AEManifest, "POST", ""},
"manifestmissing": {AEManifestMissing, "POST", ""},
"daginfo": {AEDAGInfo, "POST", ""},
"componentstatus": {endpoint: AEComponentStatus, httpVerb: "POST"},
"get": {endpoint: AEGet, httpVerb: "GET"},
// "log": {endpoint: AELog, httpVerb: "POST"},
"rename": {endpoint: AERename, httpVerb: "POST", defaultSource: "local"},
"save": {endpoint: AESave, httpVerb: "POST"},
"pull": {endpoint: AEPull, httpVerb: "POST", defaultSource: "network"},
// "push": {endpoint: AEPush, httpVerb: "POST", defaultSource: "local"},
"render": {endpoint: AERender, httpVerb: "POST"},
"remove": {endpoint: AERemove, httpVerb: "POST", defaultSource: "local"},
"validate": {endpoint: AEValidate, httpVerb: "POST", defaultSource: "local"},
// "unpack": {endpoint: AEUnpack, httpVerb: "POST"},
"manifest": {endpoint: AEManifest, httpVerb: "POST"},
"manifestmissing": {endpoint: AEManifestMissing, httpVerb: "POST"},
"daginfo": {endpoint: AEDAGInfo, httpVerb: "POST"},
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (m DiffMethods) Name() string {
// Attributes defines attributes for each method
func (m DiffMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"changes": {AEChanges, "POST", ""},
"diff": {AEDiff, "POST", ""},
"changes": {endpoint: AEChanges, httpVerb: "POST"},
"diff": {endpoint: AEDiff, httpVerb: "POST"},
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,12 @@ func (inst *Instance) dispatchMethodCall(ctx context.Context, method string, par
out := reflect.New(c.OutType)
res = out.Interface()
}
// TODO(dustmop): Send the source across the RPC, using an HTTP header
// TODO(ramfox): dispatch is still unable to give enough details to the url
// (because it doesn't know how or what param information to put into the url or query)
// for it to reliably use GET. All POSTs w/ content type application json work, however.
// we may want to just flat out say that as an RPC layer, dispatch will only ever use
// json POST to communicate.
err = inst.http.CallMethod(ctx, c.Endpoint, "POST", param, res)
err = inst.http.CallMethod(ctx, c.Endpoint, "POST", source, param, res)
if err != nil {
return nil, nil, err
}
Expand Down
18 changes: 9 additions & 9 deletions lib/dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ func (m *animalMethods) Name() string {

func (m *animalMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"cat": {denyRPC, "", ""},
"dog": {denyRPC, "", ""},
"cat": {endpoint: denyRPC},
"dog": {endpoint: denyRPC},
}
}

Expand Down Expand Up @@ -406,12 +406,12 @@ func (m *fruitMethods) Name() string {

func (m *fruitMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"apple": {"/apple", "GET", ""},
"banana": {"/banana", "GET", ""},
"cherry": {"/cherry", "GET", ""},
"date": {"/date", "GET", ""},
"apple": {endpoint: "/apple", httpVerb: "GET"},
"banana": {endpoint: "/banana", httpVerb: "GET"},
"cherry": {endpoint: "/cherry", httpVerb: "GET"},
"date": {endpoint: "/date", httpVerb: "GET"},
// entawak cannot be called over RPC
"entawak": {denyRPC, "", ""},
"entawak": {endpoint: denyRPC},
}
}

Expand Down Expand Up @@ -489,8 +489,8 @@ func (m *getSrcMethods) Name() string {

func (m *getSrcMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"one": {"/one", "GET", ""},
"two": {"/two", "GET", "network"},
"one": {endpoint: "/one", httpVerb: "GET"},
"two": {endpoint: "/two", httpVerb: "GET", defaultSource: "network"},
}
}

Expand Down
18 changes: 9 additions & 9 deletions lib/fsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func (m FSIMethods) Name() string {
// Attributes defines attributes for each method
func (m FSIMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"status": {AEStatus, "POST", ""},
"caninitdatasetworkdir": {AECanInitDatasetWorkDir, "POST", ""},
"init": {AEInit, "POST", ""},
"checkout": {AECheckout, "POST", ""},
"ensureref": {AEEnsureRef, "POST", ""},
"restore": {AERestore, "POST", ""},
"write": {AEFSIWrite, "POST", ""},
"createlink": {AEFSICreateLink, "POST", ""},
"unlink": {AEFSIUnlink, "POST", ""},
"status": {endpoint: AEStatus, httpVerb: "POST"},
"caninitdatasetworkdir": {endpoint: AECanInitDatasetWorkDir, httpVerb: "POST"},
"init": {endpoint: AEInit, httpVerb: "POST"},
"checkout": {endpoint: AECheckout, httpVerb: "POST"},
"ensureref": {endpoint: AEEnsureRef, httpVerb: "POST"},
"restore": {endpoint: AERestore, httpVerb: "POST"},
"write": {endpoint: AEFSIWrite, httpVerb: "POST"},
"createlink": {endpoint: AEFSICreateLink, httpVerb: "POST"},
"unlink": {endpoint: AEFSIUnlink, httpVerb: "POST"},
}
}

Expand Down
25 changes: 15 additions & 10 deletions lib/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
var ErrUnsupportedRPC = errors.New("method is not suported over RPC")

const jsonMimeType = "application/json"
const sourceResolver = "SourceResolver"

// HTTPClient implements the qri http client
type HTTPClient struct {
Expand Down Expand Up @@ -70,35 +71,35 @@ func NewHTTPClientWithProtocol(multiaddr string, protocol string) (*HTTPClient,
}

// Call calls API endpoint and passes on parameters, context info
func (c HTTPClient) Call(ctx context.Context, apiEndpoint APIEndpoint, params interface{}, result interface{}) error {
return c.CallMethod(ctx, apiEndpoint, http.MethodPost, params, result)
func (c HTTPClient) Call(ctx context.Context, apiEndpoint APIEndpoint, source string, params interface{}, result interface{}) error {
return c.CallMethod(ctx, apiEndpoint, http.MethodPost, source, params, result)
}

// CallMethod calls API endpoint and passes on parameters, context info and specific HTTP Method
func (c HTTPClient) CallMethod(ctx context.Context, apiEndpoint APIEndpoint, httpMethod string, params interface{}, result interface{}) error {
func (c HTTPClient) CallMethod(ctx context.Context, apiEndpoint APIEndpoint, httpMethod string, source string, params interface{}, result interface{}) error {
// TODO(arqu): work out mimeType configuration/override per API endpoint
mimeType := jsonMimeType
addr := fmt.Sprintf("%s://%s%s", c.Protocol, c.Address, apiEndpoint)

return c.do(ctx, addr, httpMethod, mimeType, params, result, false)
return c.do(ctx, addr, httpMethod, mimeType, source, params, result, false)
}

// CallRaw calls API endpoint and passes on parameters, context info and returns the []byte result
func (c HTTPClient) CallRaw(ctx context.Context, apiEndpoint APIEndpoint, params interface{}, result interface{}) error {
return c.CallMethodRaw(ctx, apiEndpoint, http.MethodPost, params, result)
func (c HTTPClient) CallRaw(ctx context.Context, apiEndpoint APIEndpoint, source string, params interface{}, result interface{}) error {
return c.CallMethodRaw(ctx, apiEndpoint, http.MethodPost, source, params, result)
}

// CallMethodRaw calls API endpoint and passes on parameters, context info, specific HTTP Method and returns the []byte result
func (c HTTPClient) CallMethodRaw(ctx context.Context, apiEndpoint APIEndpoint, httpMethod string, params interface{}, result interface{}) error {
func (c HTTPClient) CallMethodRaw(ctx context.Context, apiEndpoint APIEndpoint, httpMethod string, source string, params interface{}, result interface{}) error {
// TODO(arqu): work out mimeType configuration/override per API endpoint
mimeType := jsonMimeType
addr := fmt.Sprintf("%s://%s%s", c.Protocol, c.Address, apiEndpoint)
// TODO(arqu): inject context values into headers

return c.do(ctx, addr, httpMethod, mimeType, params, result, true)
return c.do(ctx, addr, httpMethod, mimeType, source, params, result, true)
}

func (c HTTPClient) do(ctx context.Context, addr string, httpMethod string, mimeType string, params interface{}, result interface{}, raw bool) error {
func (c HTTPClient) do(ctx context.Context, addr string, httpMethod string, mimeType string, source string, params interface{}, result interface{}, raw bool) error {
var req *http.Request
var err error

Expand Down Expand Up @@ -134,6 +135,10 @@ func (c HTTPClient) do(ctx context.Context, addr string, httpMethod string, mime
req.Header.Set("Content-Type", mimeType)
req.Header.Set("Accept", mimeType)

if source != "" {
req.Header.Set(sourceResolver, source)
}

req, added := token.AddContextTokenToRequest(ctx, req)
if !added {
log.Debugw("No token was set on an http client request. Unauthenticated requests may fail", "httpMethod", httpMethod, "addr", addr)
Expand Down Expand Up @@ -246,7 +251,7 @@ func NewHTTPRequestHandler(inst *Instance, libMethod string) http.HandlerFunc {

// SourceFromRequest retrieves from the http request the source for resolving refs
func SourceFromRequest(r *http.Request) string {
return r.Header.Get("SourceResolver")
return r.Header.Get(sourceResolver)
}

// DecodeParams decodes a json body into params
Expand Down
8 changes: 4 additions & 4 deletions lib/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func (m LogMethods) Name() string {
// Attributes defines attributes for each method
func (m LogMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"history": {AEHistory, "POST", ""},
"log": {AELog, "POST", ""},
"rawlogbook": {denyRPC, "", ""},
"logbooksummary": {denyRPC, "", ""},
"history": {endpoint: AEHistory, httpVerb: "POST"},
"log": {endpoint: AELog, httpVerb: "POST"},
"rawlogbook": {endpoint: denyRPC},
"logbooksummary": {endpoint: denyRPC},
}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func (m PeerMethods) Name() string { return "peer" }
// Attributes defines attributes for each method
func (m PeerMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"list": {AEPeers, "POST", ""},
"info": {AEPeer, "POST", ""},
"connect": {AEConnect, "POST", ""},
"disconnect": {AEDisconnect, "POST", ""},
"connections": {AEConnections, "POST", ""},
"connectedqriprofiles": {AEConnectedQriProfiles, "POST", ""},
"list": {endpoint: AEPeers, httpVerb: "POST"},
"info": {endpoint: AEPeer, httpVerb: "POST"},
"connect": {endpoint: AEConnect, httpVerb: "POST"},
"disconnect": {endpoint: AEDisconnect, httpVerb: "POST"},
"connections": {endpoint: AEConnections, httpVerb: "POST"},
"connectedqriprofiles": {endpoint: AEConnectedQriProfiles, httpVerb: "POST"},
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func (m ProfileMethods) Name() string {
// Attributes defines attributes for each method
func (m ProfileMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"getprofile": {denyRPC, "", ""},
"setprofile": {denyRPC, "", ""},
"setprofilephoto": {denyRPC, "", ""},
"setposterphoto": {denyRPC, "", ""},
"getprofile": {endpoint: denyRPC},
"setprofile": {endpoint: denyRPC},
"setprofilephoto": {endpoint: denyRPC},
"setposterphoto": {endpoint: denyRPC},
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func (m RegistryClientMethods) Name() string {
// Attributes defines attributes for each method
func (m RegistryClientMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"createprofile": {denyRPC, "", ""},
"proveprofilekey": {denyRPC, "", ""},
"createprofile": {endpoint: denyRPC},
"proveprofilekey": {endpoint: denyRPC},
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func (m RemoteMethods) Name() string {
// Attributes defines attributes for each method
func (m RemoteMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"feeds": {AEFeeds, "POST", ""},
"preview": {AEPreview, "POST", ""},
"push": {AEPush, "POST", "local"},
"remove": {AERemoteRemove, "POST", "network"},
"feeds": {endpoint: AEFeeds, httpVerb: "POST"},
"preview": {endpoint: AEPreview, httpVerb: "POST"},
"push": {endpoint: AEPush, httpVerb: "POST", defaultSource: "local"},
"remove": {endpoint: AERemoteRemove, httpVerb: "POST", defaultSource: "network"},
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (m SearchMethods) Name() string {
// Attributes defines attributes for each method
func (m SearchMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"search": {AESearch, "POST", ""},
"search": {endpoint: AESearch, httpVerb: "POST"},
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (m SQLMethods) Name() string { return "sql" }
// Attributes defines attributes for each method
func (m SQLMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"exec": {AESQL, "POST", ""},
"exec": {endpoint: AESQL, httpVerb: "POST"},
}
}

Expand Down

0 comments on commit 8a2ddf8

Please sign in to comment.