diff --git a/plugin/digitizer/digitize_command.go b/plugin/digitizer/digitize_command.go index 958b91b..6b847be 100644 --- a/plugin/digitizer/digitize_command.go +++ b/plugin/digitizer/digitize_command.go @@ -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 @@ -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 { @@ -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 { @@ -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 { diff --git a/plugin/digitizer/digitizer_plugin_test.go b/plugin/digitizer/digitizer_plugin_test.go index 61e339d..d60129c 100644 --- a/plugin/digitizer/digitizer_plugin_test.go +++ b/plugin/digitizer/digitizer_plugin_test.go @@ -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: @@ -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"))