Skip to content

Commit

Permalink
Merge pull request #1132 from mesg-foundation/feature/api-create-exeu…
Browse files Browse the repository at this point in the history
…ction

 Implement execution create api
  • Loading branch information
krhubert authored Jun 28, 2019
2 parents 881fefd + 433016c commit f8307a9
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 26 deletions.
199 changes: 173 additions & 26 deletions protobuf/api/execution.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions protobuf/api/execution.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ package api;
// The source file of this API is hosted on [GitHub](https://github.com/mesg-foundation/engine/blob/master/protobuf/api/execution.proto).

service Execution {
// Create creates a single Execution specified in a request.
rpc Create(CreateExecutionRequest) returns (CreateExecutionResponse) {}

// Get returns a single Execution specified in a request.
rpc Get(GetExecutionRequest) returns (types.Execution) {}

Expand All @@ -22,6 +25,20 @@ service Execution {
rpc Update(UpdateExecutionRequest) returns (UpdateExecutionResponse) {}
}

// CreateExecutionRequest defines request to create a single execution.
message CreateExecutionRequest {
string instanceHash = 1;
string taskKey = 2;
string inputs = 3;
repeated string tags = 4;
}

// CreateExecutionResponse defines response for execution creation.
message CreateExecutionResponse {
// Execution's hash.
string hash = 1;
}

// GetExecutionRequest defines request to retrieve a single execution.
message GetExecutionRequest {
// Execution's hash to fetch.
Expand Down
23 changes: 23 additions & 0 deletions server/grpc/api/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/mesg-foundation/core/execution"
"github.com/mesg-foundation/core/hash"
Expand All @@ -27,6 +28,28 @@ func NewExecutionServer(sdk *sdk.SDK) *ExecutionServer {
return &ExecutionServer{sdk: sdk}
}

// Create creates an execution.
func (s *ExecutionServer) Create(ctx context.Context, req *api.CreateExecutionRequest) (*api.CreateExecutionResponse, error) {
hash, err := hash.Decode(req.InstanceHash)
if err != nil {
return nil, err
}

var inputs map[string]interface{}
if err := json.Unmarshal([]byte(req.Inputs), &inputs); err != nil {
return nil, fmt.Errorf("cannot parse execution's inputs (JSON format): %s", err)
}

executionHash, err := s.sdk.Execution.Execute(hash, req.TaskKey, inputs, req.Tags)
if err != nil {
return nil, err
}

return &api.CreateExecutionResponse{
Hash: executionHash.String(),
}, nil
}

// Get returns execution from given hash.
func (s *ExecutionServer) Get(ctx context.Context, req *api.GetExecutionRequest) (*types.Execution, error) {
hash, err := hash.Decode(req.Hash)
Expand Down

0 comments on commit f8307a9

Please sign in to comment.