1
2
3
4 package prediction
5
6
7
8
9
10
11
12 import (
13 "context"
14 "github.com/Azure/go-autorest/autorest"
15 "github.com/Azure/go-autorest/autorest/azure"
16 "github.com/Azure/go-autorest/tracing"
17 "github.com/gofrs/uuid"
18 "io"
19 "net/http"
20 )
21
22
23 type BaseClient struct {
24 autorest.Client
25 APIKey string
26 Endpoint string
27 }
28
29
30 func New(aPIKey string, endpoint string) BaseClient {
31 return NewWithoutDefaults(aPIKey, endpoint)
32 }
33
34
35 func NewWithoutDefaults(aPIKey string, endpoint string) BaseClient {
36 return BaseClient{
37 Client: autorest.NewClientWithUserAgent(UserAgent()),
38 APIKey: aPIKey,
39 Endpoint: endpoint,
40 }
41 }
42
43
44
45
46
47
48
49 func (client BaseClient) PredictImage(ctx context.Context, projectID uuid.UUID, imageData io.ReadCloser, iterationID *uuid.UUID, application string) (result ImagePrediction, err error) {
50 if tracing.IsEnabled() {
51 ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.PredictImage")
52 defer func() {
53 sc := -1
54 if result.Response.Response != nil {
55 sc = result.Response.Response.StatusCode
56 }
57 tracing.EndSpan(ctx, sc, err)
58 }()
59 }
60 req, err := client.PredictImagePreparer(ctx, projectID, imageData, iterationID, application)
61 if err != nil {
62 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImage", nil, "Failure preparing request")
63 return
64 }
65
66 resp, err := client.PredictImageSender(req)
67 if err != nil {
68 result.Response = autorest.Response{Response: resp}
69 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImage", resp, "Failure sending request")
70 return
71 }
72
73 result, err = client.PredictImageResponder(resp)
74 if err != nil {
75 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImage", resp, "Failure responding to request")
76 return
77 }
78
79 return
80 }
81
82
83 func (client BaseClient) PredictImagePreparer(ctx context.Context, projectID uuid.UUID, imageData io.ReadCloser, iterationID *uuid.UUID, application string) (*http.Request, error) {
84 urlParameters := map[string]interface{}{
85 "Endpoint": client.Endpoint,
86 }
87
88 pathParameters := map[string]interface{}{
89 "projectId": autorest.Encode("path", projectID),
90 }
91
92 queryParameters := map[string]interface{}{}
93 if iterationID != nil {
94 queryParameters["iterationId"] = autorest.Encode("query", *iterationID)
95 }
96 if len(application) > 0 {
97 queryParameters["application"] = autorest.Encode("query", application)
98 }
99
100 formDataParameters := map[string]interface{}{
101 "imageData": imageData,
102 }
103
104 preparer := autorest.CreatePreparer(
105 autorest.AsPost(),
106 autorest.WithCustomBaseURL("{Endpoint}/customvision/v2.0/Prediction", urlParameters),
107 autorest.WithPathParameters("/{projectId}/image", pathParameters),
108 autorest.WithQueryParameters(queryParameters),
109 autorest.WithMultiPartFormData(formDataParameters),
110 autorest.WithHeader("Prediction-Key", client.APIKey))
111 return preparer.Prepare((&http.Request{}).WithContext(ctx))
112 }
113
114
115
116 func (client BaseClient) PredictImageSender(req *http.Request) (*http.Response, error) {
117 return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
118 }
119
120
121
122 func (client BaseClient) PredictImageResponder(resp *http.Response) (result ImagePrediction, err error) {
123 err = autorest.Respond(
124 resp,
125 azure.WithErrorUnlessStatusCode(http.StatusOK),
126 autorest.ByUnmarshallingJSON(&result),
127 autorest.ByClosing())
128 result.Response = autorest.Response{Response: resp}
129 return
130 }
131
132
133
134
135
136
137
138
139 func (client BaseClient) PredictImageURL(ctx context.Context, projectID uuid.UUID, imageURL ImageURL, iterationID *uuid.UUID, application string) (result ImagePrediction, err error) {
140 if tracing.IsEnabled() {
141 ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.PredictImageURL")
142 defer func() {
143 sc := -1
144 if result.Response.Response != nil {
145 sc = result.Response.Response.StatusCode
146 }
147 tracing.EndSpan(ctx, sc, err)
148 }()
149 }
150 req, err := client.PredictImageURLPreparer(ctx, projectID, imageURL, iterationID, application)
151 if err != nil {
152 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImageURL", nil, "Failure preparing request")
153 return
154 }
155
156 resp, err := client.PredictImageURLSender(req)
157 if err != nil {
158 result.Response = autorest.Response{Response: resp}
159 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImageURL", resp, "Failure sending request")
160 return
161 }
162
163 result, err = client.PredictImageURLResponder(resp)
164 if err != nil {
165 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImageURL", resp, "Failure responding to request")
166 return
167 }
168
169 return
170 }
171
172
173 func (client BaseClient) PredictImageURLPreparer(ctx context.Context, projectID uuid.UUID, imageURL ImageURL, iterationID *uuid.UUID, application string) (*http.Request, error) {
174 urlParameters := map[string]interface{}{
175 "Endpoint": client.Endpoint,
176 }
177
178 pathParameters := map[string]interface{}{
179 "projectId": autorest.Encode("path", projectID),
180 }
181
182 queryParameters := map[string]interface{}{}
183 if iterationID != nil {
184 queryParameters["iterationId"] = autorest.Encode("query", *iterationID)
185 }
186 if len(application) > 0 {
187 queryParameters["application"] = autorest.Encode("query", application)
188 }
189
190 preparer := autorest.CreatePreparer(
191 autorest.AsContentType("application/json; charset=utf-8"),
192 autorest.AsPost(),
193 autorest.WithCustomBaseURL("{Endpoint}/customvision/v2.0/Prediction", urlParameters),
194 autorest.WithPathParameters("/{projectId}/url", pathParameters),
195 autorest.WithJSON(imageURL),
196 autorest.WithQueryParameters(queryParameters),
197 autorest.WithHeader("Prediction-Key", client.APIKey))
198 return preparer.Prepare((&http.Request{}).WithContext(ctx))
199 }
200
201
202
203 func (client BaseClient) PredictImageURLSender(req *http.Request) (*http.Response, error) {
204 return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
205 }
206
207
208
209 func (client BaseClient) PredictImageURLResponder(resp *http.Response) (result ImagePrediction, err error) {
210 err = autorest.Respond(
211 resp,
212 azure.WithErrorUnlessStatusCode(http.StatusOK),
213 autorest.ByUnmarshallingJSON(&result),
214 autorest.ByClosing())
215 result.Response = autorest.Response{Response: resp}
216 return
217 }
218
219
220
221
222
223
224
225
226 func (client BaseClient) PredictImageURLWithNoStore(ctx context.Context, projectID uuid.UUID, imageURL ImageURL, iterationID *uuid.UUID, application string) (result ImagePrediction, err error) {
227 if tracing.IsEnabled() {
228 ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.PredictImageURLWithNoStore")
229 defer func() {
230 sc := -1
231 if result.Response.Response != nil {
232 sc = result.Response.Response.StatusCode
233 }
234 tracing.EndSpan(ctx, sc, err)
235 }()
236 }
237 req, err := client.PredictImageURLWithNoStorePreparer(ctx, projectID, imageURL, iterationID, application)
238 if err != nil {
239 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImageURLWithNoStore", nil, "Failure preparing request")
240 return
241 }
242
243 resp, err := client.PredictImageURLWithNoStoreSender(req)
244 if err != nil {
245 result.Response = autorest.Response{Response: resp}
246 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImageURLWithNoStore", resp, "Failure sending request")
247 return
248 }
249
250 result, err = client.PredictImageURLWithNoStoreResponder(resp)
251 if err != nil {
252 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImageURLWithNoStore", resp, "Failure responding to request")
253 return
254 }
255
256 return
257 }
258
259
260 func (client BaseClient) PredictImageURLWithNoStorePreparer(ctx context.Context, projectID uuid.UUID, imageURL ImageURL, iterationID *uuid.UUID, application string) (*http.Request, error) {
261 urlParameters := map[string]interface{}{
262 "Endpoint": client.Endpoint,
263 }
264
265 pathParameters := map[string]interface{}{
266 "projectId": autorest.Encode("path", projectID),
267 }
268
269 queryParameters := map[string]interface{}{}
270 if iterationID != nil {
271 queryParameters["iterationId"] = autorest.Encode("query", *iterationID)
272 }
273 if len(application) > 0 {
274 queryParameters["application"] = autorest.Encode("query", application)
275 }
276
277 preparer := autorest.CreatePreparer(
278 autorest.AsContentType("application/json; charset=utf-8"),
279 autorest.AsPost(),
280 autorest.WithCustomBaseURL("{Endpoint}/customvision/v2.0/Prediction", urlParameters),
281 autorest.WithPathParameters("/{projectId}/url/nostore", pathParameters),
282 autorest.WithJSON(imageURL),
283 autorest.WithQueryParameters(queryParameters),
284 autorest.WithHeader("Prediction-Key", client.APIKey))
285 return preparer.Prepare((&http.Request{}).WithContext(ctx))
286 }
287
288
289
290 func (client BaseClient) PredictImageURLWithNoStoreSender(req *http.Request) (*http.Response, error) {
291 return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
292 }
293
294
295
296 func (client BaseClient) PredictImageURLWithNoStoreResponder(resp *http.Response) (result ImagePrediction, err error) {
297 err = autorest.Respond(
298 resp,
299 azure.WithErrorUnlessStatusCode(http.StatusOK),
300 autorest.ByUnmarshallingJSON(&result),
301 autorest.ByClosing())
302 result.Response = autorest.Response{Response: resp}
303 return
304 }
305
306
307
308
309
310
311
312 func (client BaseClient) PredictImageWithNoStore(ctx context.Context, projectID uuid.UUID, imageData io.ReadCloser, iterationID *uuid.UUID, application string) (result ImagePrediction, err error) {
313 if tracing.IsEnabled() {
314 ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.PredictImageWithNoStore")
315 defer func() {
316 sc := -1
317 if result.Response.Response != nil {
318 sc = result.Response.Response.StatusCode
319 }
320 tracing.EndSpan(ctx, sc, err)
321 }()
322 }
323 req, err := client.PredictImageWithNoStorePreparer(ctx, projectID, imageData, iterationID, application)
324 if err != nil {
325 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImageWithNoStore", nil, "Failure preparing request")
326 return
327 }
328
329 resp, err := client.PredictImageWithNoStoreSender(req)
330 if err != nil {
331 result.Response = autorest.Response{Response: resp}
332 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImageWithNoStore", resp, "Failure sending request")
333 return
334 }
335
336 result, err = client.PredictImageWithNoStoreResponder(resp)
337 if err != nil {
338 err = autorest.NewErrorWithError(err, "prediction.BaseClient", "PredictImageWithNoStore", resp, "Failure responding to request")
339 return
340 }
341
342 return
343 }
344
345
346 func (client BaseClient) PredictImageWithNoStorePreparer(ctx context.Context, projectID uuid.UUID, imageData io.ReadCloser, iterationID *uuid.UUID, application string) (*http.Request, error) {
347 urlParameters := map[string]interface{}{
348 "Endpoint": client.Endpoint,
349 }
350
351 pathParameters := map[string]interface{}{
352 "projectId": autorest.Encode("path", projectID),
353 }
354
355 queryParameters := map[string]interface{}{}
356 if iterationID != nil {
357 queryParameters["iterationId"] = autorest.Encode("query", *iterationID)
358 }
359 if len(application) > 0 {
360 queryParameters["application"] = autorest.Encode("query", application)
361 }
362
363 formDataParameters := map[string]interface{}{
364 "imageData": imageData,
365 }
366
367 preparer := autorest.CreatePreparer(
368 autorest.AsPost(),
369 autorest.WithCustomBaseURL("{Endpoint}/customvision/v2.0/Prediction", urlParameters),
370 autorest.WithPathParameters("/{projectId}/image/nostore", pathParameters),
371 autorest.WithQueryParameters(queryParameters),
372 autorest.WithMultiPartFormData(formDataParameters),
373 autorest.WithHeader("Prediction-Key", client.APIKey))
374 return preparer.Prepare((&http.Request{}).WithContext(ctx))
375 }
376
377
378
379 func (client BaseClient) PredictImageWithNoStoreSender(req *http.Request) (*http.Response, error) {
380 return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
381 }
382
383
384
385 func (client BaseClient) PredictImageWithNoStoreResponder(resp *http.Response) (result ImagePrediction, err error) {
386 err = autorest.Respond(
387 resp,
388 azure.WithErrorUnlessStatusCode(http.StatusOK),
389 autorest.ByUnmarshallingJSON(&result),
390 autorest.ByClosing())
391 result.Response = autorest.Response{Response: resp}
392 return
393 }
394
View as plain text