I have a website with its own front-end and back-end!
But I would like to add Dfinity technology to my project.
I want to write logic on a canister and call these methods from my back-end using command (dfx canister --network ic call <canister_id> method value)
I know what I need for this dfx.json
Can I use the canister like that? What should be for this in dfx.json?
Below is an example of how I on the backend (Golang) make a request to a drive that is created locally.
func makeRequestDfinity(cammand string) string {
//command := fmt.Sprintf("dfx canister call testCrud create '( record {age=%s; name=\"%s\"; email=\"%s\"; balance=%.2f; tokenSimple=%.2f; tokenMiddle=%.2f; tokenExpensive=%.2f; role=\"%s\"; fullName= record{firstName=\"%s\"; lastName=\"%s\";}})'", age, user.Name, user.Email, user.Balance, user.TokenSimple, user.TokenMiddle, user.TokenExpensive, user.Role, user.FullName.FirstName, user.FullName.LastName)
cmd := exec.Command("/bin/sh", "-c", cammand)
cmd.Dir = "/home/ubuntu/Desktop/TestCrud/testCrud"
cmd.Stdin = strings.NewReader("some input")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
//End work Dfinity
//Converted data
result := out.String()
return result
}
1 Like
I think you would be better of using the http API - there’s a rust agent and a JS one (GitHub - dfinity/agent-js: A collection of libraries and tools for building software around the Internet Computer, in JavaScript.) - you could make a golang one, or someone may have one available already? I think that is ultimately cleaner than using your server to proxy the commands.
3 Likes
(dfx canister --network ic call <canister_id> method value)
Will I be able to use this command on another computer to reach my canister or does this command work only in the project folder?
dfx
(as a tool) is for a human developer developing a canister, not for a program to use (Golang program in your case, IIUC) to be the client of one that is deployed, as another program. One exception to this is that dfx
may be used by a project in its testing framework, but there are alternatives to doing this as well. It could be used in the way that you are intending, but doing so is not consistent with its intended purpose, and you will be fighting against that fact as you go, and get help.
If you want to become the client of a canister from a Go program, I would advocate writing a small amount of Rust to create a customized CLI tool that your Go program can call, and not use dfx
for this purpose.
As an example, caniput
is a Rust-based CLI tool that is specific to the CandidSpaces
canister:
I’m advocating that you use that tool/subproject as an example to build a custom tool of your own, much like @stephenandrews is advocating above as well by pointing to the agent libraries in Rust and JS. Indeed, caniput
uses the Rust agent to implement its functionality. It should be straightforward to adapt to another canister’s API.
3 Likes
As an alternative to this DIY approach, perhaps ic-repl
can get a feature or two to help your use case?
(cc @chenyan)
I think the current use case of ic-repl
is for scripting tests, and for live interaction with a human, but it doesn’t mean that you can’t fork it and adapt it for the CLI experience that your Golang backend wants to have for itself?
Here’s the (Rust) code: GitHub - dfinity/ic-repl
1 Like
Caniput looks like what I need, but I don’t understand how to start using it. Please tell me how to get this command in Linux???
Thanks for answers. This is very important for the company I work for.
I think you would need to build caniput
from source using the Rust toolchain.