Skip to content
Snippets Groups Projects
Commit 85bc267d authored by Jono Wenger's avatar Jono Wenger
Browse files

Implement restlike.go and restlikeSingle.go

parent ba72390c
No related branches found
No related tags found
1 merge request!60Revert "Fail a test to be sure it works"
......@@ -94,6 +94,16 @@ func main() {
// bindings/group.go
js.Global().Set("NewGroupChat", js.FuncOf(wasm.NewGroupChat))
// bindings/restlike.go
js.Global().Set("RestlikeRequest", js.FuncOf(wasm.RestlikeRequest))
js.Global().Set("RestlikeRequestAuth", js.FuncOf(wasm.RestlikeRequestAuth))
// bindings/restlikeSingle.go
js.Global().Set("RequestRestLike",
js.FuncOf(wasm.RequestRestLike))
js.Global().Set("AsyncRequestRestLike",
js.FuncOf(wasm.AsyncRequestRestLike))
<-make(chan bool)
os.Exit(0)
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
////////////////////////////////////////////////////////////////////////////////
//go:build js && wasm
package wasm
import (
"gitlab.com/elixxir/client/bindings"
"syscall/js"
)
// RestlikeRequest performs a normal restlike request.
//
// Parameters:
// - args[0] - ID of Cmix object in tracker (int).
// - args[1] - ID of Connection object in tracker (int).
// - args[2] - JSON of [bindings.RestlikeMessage] (Uint8Array).
// - args[3] - JSON of [xxdk.E2EParams] (Uint8Array).
//
// Returns:
// - JSON of [bindings.RestlikeMessage] (Uint8Array).
// - Throws a TypeError if parsing the parameters or making the request fails.
func RestlikeRequest(_ js.Value, args []js.Value) interface{} {
cmixId := args[0].Int()
connectionID := args[1].Int()
request := CopyBytesToGo(args[2])
e2eParamsJSON := CopyBytesToGo(args[3])
msg, err := bindings.RestlikeRequest(
cmixId, connectionID, request, e2eParamsJSON)
if err != nil {
Throw(TypeError, err.Error())
return nil
}
return CopyBytesToJS(msg)
}
// RestlikeRequestAuth performs an authenticated restlike request.
//
// Parameters:
// - args[0] - ID of Cmix object in tracker (int).
// - args[1] - ID of AuthenticatedConnection object in tracker (int).
// - args[2] - JSON of [bindings.RestlikeMessage] (Uint8Array).
// - args[3] - JSON of [xxdk.E2EParams] (Uint8Array).
//
// Returns:
// - JSON of [bindings.RestlikeMessage] (Uint8Array).
// - Throws a TypeError if parsing the parameters or making the request fails.
func RestlikeRequestAuth(_ js.Value, args []js.Value) interface{} {
cmixId := args[0].Int()
authConnectionID := args[1].Int()
request := CopyBytesToGo(args[2])
e2eParamsJSON := CopyBytesToGo(args[3])
msg, err := bindings.RestlikeRequestAuth(
cmixId, authConnectionID, request, e2eParamsJSON)
if err != nil {
Throw(TypeError, err.Error())
return nil
}
return CopyBytesToJS(msg)
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
////////////////////////////////////////////////////////////////////////////////
//go:build js && wasm
package wasm
import (
"gitlab.com/elixxir/client/bindings"
"syscall/js"
)
// RestlikeCallback wraps Javascript callbacks to adhere to the
// [bindings.RestlikeCallback] interface.
type restlikeCallback struct {
callback func(args ...interface{}) js.Value
}
func (rlc *restlikeCallback) Callback(payload []byte, err error) {
rlc.callback(CopyBytesToJS(payload), err.Error())
}
// RequestRestLike sends a restlike request to a given contact.
//
// Parameters:
// - args[0] - ID of E2e object in tracker (int).
// - args[1] - marshalled recipient [contact.Contact] (Uint8Array).
// - args[2] - JSON of [bindings.RestlikeMessage] (Uint8Array).
// - args[3] - JSON of [single.RequestParams] (Uint8Array).
//
// Returns:
// - JSON of [restlike.Message] (Uint8Array).
// - Throws a TypeError if parsing the parameters or making the request fails.
func RequestRestLike(_ js.Value, args []js.Value) interface{} {
e2eID := args[0].Int()
recipient := CopyBytesToGo(args[1])
request := CopyBytesToGo(args[2])
paramsJSON := CopyBytesToGo(args[3])
msg, err := bindings.RequestRestLike(e2eID, recipient, request, paramsJSON)
if err != nil {
Throw(TypeError, err.Error())
return nil
}
return CopyBytesToJS(msg)
}
// AsyncRequestRestLike sends an asynchronous restlike request to a given
// contact.
//
// The RestlikeCallback will be called with the results of JSON marshalling the
// response when received.
//
// Parameters:
// - args[0] - ID of E2e object in tracker (int).
// - args[1] - marshalled recipient [contact.Contact] (Uint8Array).
// - args[2] - JSON of [bindings.RestlikeMessage] (Uint8Array).
// - args[3] - JSON of [single.RequestParams] (Uint8Array).
// - args[4] - Javascript object that has functions that implement the
// [bindings.RestlikeCallback] interface.
//
// Returns:
// - Throws a TypeError if parsing the parameters or making the request fails.
func AsyncRequestRestLike(_ js.Value, args []js.Value) interface{} {
e2eID := args[0].Int()
recipient := CopyBytesToGo(args[1])
request := CopyBytesToGo(args[2])
paramsJSON := CopyBytesToGo(args[3])
cb := &restlikeCallback{args[4].Get("Callback").Invoke}
err := bindings.AsyncRequestRestLike(
e2eID, recipient, request, paramsJSON, cb)
if err != nil {
Throw(TypeError, err.Error())
return nil
}
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment