• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

UiPath / uipathcli / 13944852368

19 Mar 2025 10:56AM UTC coverage: 90.44% (+0.3%) from 90.112%
13944852368

push

github

thschmitt
Add new api package with orchestrator and du client

- Moved the orchestrator client to new api package.

- Extracted client-related logic from the DigitizeCommand and
  Upload/DownloadCommand into the new api package.

- Changed auth module to only return the auth token and no more
  additional headers to simplify the auth module.

260 of 291 new or added lines in 21 files covered. (89.35%)

1 existing line in 1 file now uncovered.

5894 of 6517 relevant lines covered (90.44%)

1.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/plugin/digitizer/digitize_command.go
1
package digitzer
2

3
import (
4
        "errors"
5
        "fmt"
6
        "net/url"
7
        "strings"
8
        "time"
9

10
        "github.com/UiPath/uipathcli/log"
11
        "github.com/UiPath/uipathcli/output"
12
        "github.com/UiPath/uipathcli/plugin"
13
        "github.com/UiPath/uipathcli/utils/api"
14
        "github.com/UiPath/uipathcli/utils/stream"
15
        "github.com/UiPath/uipathcli/utils/visualization"
16
)
17

18
// The DigitizeCommand is a convenient wrapper over the async digitizer API
19
// to make it seem like it is a single sync call.
20
type DigitizeCommand struct{}
21

22
func (c DigitizeCommand) Command() plugin.Command {
1✔
23
        return *plugin.NewCommand("du").
1✔
24
                WithCategory("digitization", "Document Digitization", "Digitizes a document, extracting its Document Object Model (DOM) and text.").
1✔
25
                WithOperation("digitize", "Digitize file", "Digitize the given file").
1✔
26
                WithParameter("project-id", plugin.ParameterTypeString, "The project id", false).
1✔
27
                WithParameter("file", plugin.ParameterTypeBinary, "The file to digitize", true).
1✔
28
                WithParameter("content-type", plugin.ParameterTypeString, "The content type", false)
1✔
29
}
1✔
30

31
func (c DigitizeCommand) Execute(ctx plugin.ExecutionContext, writer output.OutputWriter, logger log.Logger) error {
1✔
32
        if ctx.Organization == "" {
2✔
33
                return errors.New("Organization is not set")
1✔
34
        }
1✔
35
        if ctx.Tenant == "" {
2✔
36
                return errors.New("Tenant is not set")
1✔
37
        }
1✔
38
        documentId, err := c.startDigitization(ctx, logger)
1✔
39
        if err != nil {
2✔
40
                return err
1✔
41
        }
1✔
42

43
        for i := 1; i <= 60; i++ {
2✔
44
                finished, err := c.waitForDigitization(documentId, ctx, writer, logger)
1✔
45
                if err != nil {
2✔
46
                        return err
1✔
47
                }
1✔
48
                if finished {
2✔
49
                        return nil
1✔
50
                }
1✔
51
                time.Sleep(1 * time.Second)
×
52
        }
53
        return fmt.Errorf("Digitization with documentId '%s' did not finish in time", documentId)
×
54
}
55

56
func (c DigitizeCommand) startDigitization(ctx plugin.ExecutionContext, logger log.Logger) (string, error) {
1✔
57
        uploadBar := visualization.NewProgressBar(logger)
1✔
58
        defer uploadBar.Remove()
1✔
59

1✔
60
        projectId := c.getProjectId(ctx.Parameters)
1✔
61
        file := ctx.Input
1✔
62
        if file == nil {
2✔
63
                file = c.getFileParameter(ctx.Parameters)
1✔
64
        }
1✔
65
        contentType := c.getParameter("content-type", ctx.Parameters)
1✔
66
        if contentType == "" {
2✔
67
                contentType = "application/octet-stream"
1✔
68
        }
1✔
69

70
        baseUri := c.formatUri(ctx.BaseUri, ctx.Organization, ctx.Tenant)
1✔
71
        client := api.NewDuClient(baseUri, ctx.Auth.Token, ctx.Debug, ctx.Settings, logger)
1✔
72
        return client.StartDigitization(projectId, file, contentType, uploadBar)
1✔
73
}
74

75
func (c DigitizeCommand) waitForDigitization(documentId string, ctx plugin.ExecutionContext, writer output.OutputWriter, logger log.Logger) (bool, error) {
1✔
76
        projectId := c.getProjectId(ctx.Parameters)
1✔
77
        baseUri := c.formatUri(ctx.BaseUri, ctx.Organization, ctx.Tenant)
1✔
78
        client := api.NewDuClient(baseUri, ctx.Auth.Token, ctx.Debug, ctx.Settings, logger)
1✔
79
        result, err := client.GetDigitizationResult(projectId, documentId)
1✔
80
        if err != nil {
2✔
81
                return true, err
1✔
82
        }
1✔
83
        if result == "" {
1✔
NEW
84
                return false, nil
×
UNCOV
85
        }
×
86

87
        err = writer.WriteResponse(*output.NewResponseInfo(200, "200 OK", "HTTP/1.1", map[string][]string{}, strings.NewReader(result)))
1✔
88
        return true, err
1✔
89
}
90

91
func (c DigitizeCommand) formatUri(baseUri url.URL, org string, tenant string) string {
1✔
92
        path := baseUri.Path
1✔
93
        if baseUri.Path == "" {
2✔
94
                path = "/{organization}/{tenant}/du_"
1✔
95
        }
1✔
96
        path = strings.ReplaceAll(path, "{organization}", org)
1✔
97
        path = strings.ReplaceAll(path, "{tenant}", tenant)
1✔
98
        path = strings.TrimSuffix(path, "/")
1✔
99
        return fmt.Sprintf("%s://%s%s", baseUri.Scheme, baseUri.Host, path)
1✔
100
}
101

102
func (c DigitizeCommand) getProjectId(parameters []plugin.ExecutionParameter) string {
1✔
103
        projectId := c.getParameter("project-id", parameters)
1✔
104
        if projectId == "" {
2✔
105
                projectId = "00000000-0000-0000-0000-000000000000"
1✔
106
        }
1✔
107
        return projectId
1✔
108
}
109

110
func (c DigitizeCommand) getParameter(name string, parameters []plugin.ExecutionParameter) string {
1✔
111
        result := ""
1✔
112
        for _, p := range parameters {
2✔
113
                if p.Name == name {
2✔
114
                        if data, ok := p.Value.(string); ok {
2✔
115
                                result = data
1✔
116
                                break
1✔
117
                        }
118
                }
119
        }
120
        return result
1✔
121
}
122

123
func (c DigitizeCommand) getFileParameter(parameters []plugin.ExecutionParameter) stream.Stream {
1✔
124
        var result stream.Stream
1✔
125
        for _, p := range parameters {
2✔
126
                if p.Name == "file" {
2✔
127
                        if stream, ok := p.Value.(stream.Stream); ok {
2✔
128
                                result = stream
1✔
129
                                break
1✔
130
                        }
131
                }
132
        }
133
        return result
1✔
134
}
135

136
func NewDigitizeCommand() *DigitizeCommand {
1✔
137
        return &DigitizeCommand{}
1✔
138
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc