...

Source file src/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.1/customvision/prediction/client.go

Documentation: github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.1/customvision/prediction

     1  // Package prediction implements the Azure ARM Prediction service API version 2.0.
     2  //
     3  //
     4  package prediction
     5  
     6  // Copyright (c) Microsoft Corporation. All rights reserved.
     7  // Licensed under the MIT License. See License.txt in the project root for license information.
     8  //
     9  // Code generated by Microsoft (R) AutoRest Code Generator.
    10  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
    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  // BaseClient is the base client for Prediction.
    23  type BaseClient struct {
    24  	autorest.Client
    25  	APIKey   string
    26  	Endpoint string
    27  }
    28  
    29  // New creates an instance of the BaseClient client.
    30  func New(aPIKey string, endpoint string) BaseClient {
    31  	return NewWithoutDefaults(aPIKey, endpoint)
    32  }
    33  
    34  // NewWithoutDefaults creates an instance of the BaseClient client.
    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  // PredictImage sends the predict image request.
    44  // Parameters:
    45  // projectID - the project id
    46  // iterationID - optional. Specifies the id of a particular iteration to evaluate against.
    47  // The default iteration for the project will be used when not specified
    48  // application - optional. Specifies the name of application using the endpoint
    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  // PredictImagePreparer prepares the PredictImage request.
    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  // PredictImageSender sends the PredictImage request. The method will close the
   115  // http.Response Body if it receives an error.
   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  // PredictImageResponder handles the response to the PredictImage request. The method always
   121  // closes the http.Response Body.
   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  // PredictImageURL sends the predict image url request.
   133  // Parameters:
   134  // projectID - the project id
   135  // imageURL - an {Iris.Web.Api.Models.ImageUrl} that contains the url of the image to be evaluated
   136  // iterationID - optional. Specifies the id of a particular iteration to evaluate against.
   137  // The default iteration for the project will be used when not specified
   138  // application - optional. Specifies the name of application using the endpoint
   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  // PredictImageURLPreparer prepares the PredictImageURL request.
   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  // PredictImageURLSender sends the PredictImageURL request. The method will close the
   202  // http.Response Body if it receives an error.
   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  // PredictImageURLResponder handles the response to the PredictImageURL request. The method always
   208  // closes the http.Response Body.
   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  // PredictImageURLWithNoStore sends the predict image url with no store request.
   220  // Parameters:
   221  // projectID - the project id
   222  // imageURL - an {Iris.Web.Api.Models.ImageUrl} that contains the url of the image to be evaluated
   223  // iterationID - optional. Specifies the id of a particular iteration to evaluate against.
   224  // The default iteration for the project will be used when not specified
   225  // application - optional. Specifies the name of application using the endpoint
   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  // PredictImageURLWithNoStorePreparer prepares the PredictImageURLWithNoStore request.
   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  // PredictImageURLWithNoStoreSender sends the PredictImageURLWithNoStore request. The method will close the
   289  // http.Response Body if it receives an error.
   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  // PredictImageURLWithNoStoreResponder handles the response to the PredictImageURLWithNoStore request. The method always
   295  // closes the http.Response Body.
   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  // PredictImageWithNoStore sends the predict image with no store request.
   307  // Parameters:
   308  // projectID - the project id
   309  // iterationID - optional. Specifies the id of a particular iteration to evaluate against.
   310  // The default iteration for the project will be used when not specified
   311  // application - optional. Specifies the name of application using the endpoint
   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  // PredictImageWithNoStorePreparer prepares the PredictImageWithNoStore request.
   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  // PredictImageWithNoStoreSender sends the PredictImageWithNoStore request. The method will close the
   378  // http.Response Body if it receives an error.
   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  // PredictImageWithNoStoreResponder handles the response to the PredictImageWithNoStore request. The method always
   384  // closes the http.Response Body.
   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