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

UiPath / uipathcli / 13944783591

19 Mar 2025 10:53AM UTC coverage: 90.36% (+0.2%) from 90.112%
13944783591

push

github

thschmitt
Add new api package with orchestrator and du client

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

9 existing lines in 1 file now uncovered.

5896 of 6525 relevant lines covered (90.36%)

1.01 hits per line

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

88.0
/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/network"
15
        "github.com/UiPath/uipathcli/utils/stream"
16
        "github.com/UiPath/uipathcli/utils/visualization"
17
)
18

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
137
func (c DigitizeCommand) httpClientSettings(ctx plugin.ExecutionContext) network.HttpClientSettings {
×
UNCOV
138
        return *network.NewHttpClientSettings(
×
UNCOV
139
                ctx.Debug,
×
UNCOV
140
                ctx.Settings.OperationId,
×
UNCOV
141
                ctx.Settings.Timeout,
×
UNCOV
142
                ctx.Settings.MaxAttempts,
×
UNCOV
143
                ctx.Settings.Insecure)
×
UNCOV
144
}
×
145

146
func NewDigitizeCommand() *DigitizeCommand {
1✔
147
        return &DigitizeCommand{}
1✔
148
}
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

© 2026 Coveralls, Inc