Skip to content

Commit

Permalink
Add support to digitize using the default project
Browse files Browse the repository at this point in the history
Making the project id optional and setting it to the default
project if not provided. This makes it easier to digitize files.
  • Loading branch information
thschmitt committed Dec 18, 2024
1 parent 162994f commit 0e492b3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 32 deletions.
30 changes: 18 additions & 12 deletions plugin/digitizer/digitize_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ func (c DigitizeCommand) Command() plugin.Command {
return *plugin.NewCommand("du").
WithCategory("digitization", "Document Digitization", "Digitizes a document, extracting its Document Object Model (DOM) and text.").
WithOperation("digitize", "Digitize file", "Digitize the given file").
WithParameter("project-id", plugin.ParameterTypeString, "The project id", true).
WithParameter("project-id", plugin.ParameterTypeString, "The project id", false).
WithParameter("file", plugin.ParameterTypeBinary, "The file to digitize", true).
WithParameter("content-type", plugin.ParameterTypeString, "The content type", false)
}

func (c DigitizeCommand) Execute(context plugin.ExecutionContext, writer output.OutputWriter, logger log.Logger) error {
if context.Organization == "" {
return errors.New("Organization is not set")
}
if context.Tenant == "" {
return errors.New("Tenant is not set")
}
documentId, err := c.startDigitization(context, logger)
if err != nil {
return err
Expand Down Expand Up @@ -118,16 +124,8 @@ func (c DigitizeCommand) waitForDigitization(documentId string, context plugin.E
}

func (c DigitizeCommand) createDigitizeRequest(context plugin.ExecutionContext, uploadBar *utils.ProgressBar, requestError chan error) (*http.Request, error) {
if context.Organization == "" {
return nil, errors.New("Organization is not set")
}
if context.Tenant == "" {
return nil, errors.New("Tenant is not set")
}
projectId, _ := c.getParameter("project-id", context.Parameters)
if projectId == "" {
return nil, errors.New("ProjectId is not set")
}
projectId := c.getProjectId(context.Parameters)

var err error
file := context.Input
if file == nil {
Expand Down Expand Up @@ -184,7 +182,7 @@ func (c DigitizeCommand) formatUri(baseUri url.URL, org string, tenant string, p
}

func (c DigitizeCommand) createDigitizeStatusRequest(documentId string, context plugin.ExecutionContext) (*http.Request, error) {
projectId, _ := c.getParameter("project-id", context.Parameters)
projectId := c.getProjectId(context.Parameters)
uri := c.formatUri(context.BaseUri, context.Organization, context.Tenant, projectId) + fmt.Sprintf("/digitization/result/%s?api-version=1", documentId)
request, err := http.NewRequest("GET", uri, &bytes.Buffer{})
if err != nil {
Expand Down Expand Up @@ -263,6 +261,14 @@ func (c DigitizeCommand) sendRequest(request *http.Request, insecure bool) (*htt
return client.Do(request)
}

func (c DigitizeCommand) getProjectId(parameters []plugin.ExecutionParameter) string {
projectId, _ := c.getParameter("project-id", parameters)
if projectId == "" {
projectId = "00000000-0000-0000-0000-000000000000"
}
return projectId
}

func (c DigitizeCommand) getParameter(name string, parameters []plugin.ExecutionParameter) (string, error) {
for _, p := range parameters {
if p.Name == name {
Expand Down
66 changes: 46 additions & 20 deletions plugin/digitizer/digitizer_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@ import (
"github.com/UiPath/uipathcli/test"
)

func TestDigitizeWithoutProjectIdParameterShowsValidationError(t *testing.T) {
definition := `
paths:
/digitize:
get:
operationId: digitize
`

context := test.NewContextBuilder().
WithDefinition("du", definition).
WithCommandPlugin(DigitizeCommand{}).
Build()

result := test.RunCli([]string{"du", "digitization", "digitize", "--file", "myfile"}, context)

if !strings.Contains(result.StdErr, "Argument --project-id is missing") {
t.Errorf("Expected stderr to show that project-id parameter is missing, but got: %v", result.StdErr)
}
}

func TestDigitizeWithoutFileParameterShowsValidationError(t *testing.T) {
definition := `
paths:
Expand Down Expand Up @@ -180,6 +160,52 @@ paths:
}
}

func TestDigitizeWithoutProjectIdUsesDefaultProject(t *testing.T) {
path := createFile(t)
writeFile(path, []byte("hello-world"))

config := `profiles:
- name: default
organization: my-org
tenant: my-tenant
`

definition := `
servers:
- url: https://cloud.uipath.com/{organization}/{tenant}/du_/api/framework
description: The production url
variables:
organization:
description: The organization name (or id)
default: my-org
tenant:
description: The tenant name (or id)
default: my-tenant
paths:
/digitize:
get:
operationId: digitize
`

context := test.NewContextBuilder().
WithDefinition("du", definition).
WithConfig(config).
WithCommandPlugin(DigitizeCommand{}).
WithResponse(202, `{"documentId":"648ea1c2-7dbe-42a8-b112-6474d07e61c1"}`).
WithUrlResponse("/my-org/my-tenant/du_/api/framework/projects/00000000-0000-0000-0000-000000000000/digitization/result/648ea1c2-7dbe-42a8-b112-6474d07e61c1?api-version=1", 200, `{"status":"Done"}`).
Build()

result := test.RunCli([]string{"du", "digitization", "digitize", "--file", path}, context)

expectedResult := `{
"status": "Done"
}
`
if result.StdOut != expectedResult {
t.Errorf("Expected stdout to show the digitize result, but got: %v", result.StdOut)
}
}

func TestDigitizeSuccessfully(t *testing.T) {
path := createFile(t)
writeFile(path, []byte("hello-world"))
Expand Down

0 comments on commit 0e492b3

Please sign in to comment.