...

Source file src/github.com/Azure/azure-sdk-for-go/services/preview/appinsights/v1/insights/query.go

Documentation: github.com/Azure/azure-sdk-for-go/services/preview/appinsights/v1/insights

     1  package insights
     2  
     3  // Copyright (c) Microsoft Corporation. All rights reserved.
     4  // Licensed under the MIT License. See License.txt in the project root for license information.
     5  //
     6  // Code generated by Microsoft (R) AutoRest Code Generator.
     7  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
     8  
     9  import (
    10  	"context"
    11  	"github.com/Azure/go-autorest/autorest"
    12  	"github.com/Azure/go-autorest/autorest/azure"
    13  	"github.com/Azure/go-autorest/autorest/validation"
    14  	"github.com/Azure/go-autorest/tracing"
    15  	"net/http"
    16  )
    17  
    18  // QueryClient is the composite Swagger for Application Insights Data Client
    19  type QueryClient struct {
    20  	BaseClient
    21  }
    22  
    23  // NewQueryClient creates an instance of the QueryClient client.
    24  func NewQueryClient() QueryClient {
    25  	return NewQueryClientWithBaseURI(DefaultBaseURI)
    26  }
    27  
    28  // NewQueryClientWithBaseURI creates an instance of the QueryClient client using a custom endpoint.  Use this when
    29  // interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
    30  func NewQueryClientWithBaseURI(baseURI string) QueryClient {
    31  	return QueryClient{NewWithBaseURI(baseURI)}
    32  }
    33  
    34  // Execute executes an Analytics query for data.
    35  // [Here](https://dev.applicationinsights.io/documentation/Using-the-API/Query) is an example for using POST with an
    36  // Analytics query.
    37  // Parameters:
    38  // appID - ID of the application. This is Application ID from the API Access settings blade in the Azure
    39  // portal.
    40  // body - the Analytics query. Learn more about the [Analytics query
    41  // syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/)
    42  func (client QueryClient) Execute(ctx context.Context, appID string, body QueryBody) (result QueryResults, err error) {
    43  	if tracing.IsEnabled() {
    44  		ctx = tracing.StartSpan(ctx, fqdn+"/QueryClient.Execute")
    45  		defer func() {
    46  			sc := -1
    47  			if result.Response.Response != nil {
    48  				sc = result.Response.Response.StatusCode
    49  			}
    50  			tracing.EndSpan(ctx, sc, err)
    51  		}()
    52  	}
    53  	if err := validation.Validate([]validation.Validation{
    54  		{TargetValue: body,
    55  			Constraints: []validation.Constraint{{Target: "body.Query", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
    56  		return result, validation.NewError("insights.QueryClient", "Execute", err.Error())
    57  	}
    58  
    59  	req, err := client.ExecutePreparer(ctx, appID, body)
    60  	if err != nil {
    61  		err = autorest.NewErrorWithError(err, "insights.QueryClient", "Execute", nil, "Failure preparing request")
    62  		return
    63  	}
    64  
    65  	resp, err := client.ExecuteSender(req)
    66  	if err != nil {
    67  		result.Response = autorest.Response{Response: resp}
    68  		err = autorest.NewErrorWithError(err, "insights.QueryClient", "Execute", resp, "Failure sending request")
    69  		return
    70  	}
    71  
    72  	result, err = client.ExecuteResponder(resp)
    73  	if err != nil {
    74  		err = autorest.NewErrorWithError(err, "insights.QueryClient", "Execute", resp, "Failure responding to request")
    75  		return
    76  	}
    77  
    78  	return
    79  }
    80  
    81  // ExecutePreparer prepares the Execute request.
    82  func (client QueryClient) ExecutePreparer(ctx context.Context, appID string, body QueryBody) (*http.Request, error) {
    83  	pathParameters := map[string]interface{}{
    84  		"appId": autorest.Encode("path", appID),
    85  	}
    86  
    87  	preparer := autorest.CreatePreparer(
    88  		autorest.AsContentType("application/json; charset=utf-8"),
    89  		autorest.AsPost(),
    90  		autorest.WithBaseURL(client.BaseURI),
    91  		autorest.WithPathParameters("/apps/{appId}/query", pathParameters),
    92  		autorest.WithJSON(body))
    93  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
    94  }
    95  
    96  // ExecuteSender sends the Execute request. The method will close the
    97  // http.Response Body if it receives an error.
    98  func (client QueryClient) ExecuteSender(req *http.Request) (*http.Response, error) {
    99  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   100  }
   101  
   102  // ExecuteResponder handles the response to the Execute request. The method always
   103  // closes the http.Response Body.
   104  func (client QueryClient) ExecuteResponder(resp *http.Response) (result QueryResults, err error) {
   105  	err = autorest.Respond(
   106  		resp,
   107  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   108  		autorest.ByUnmarshallingJSON(&result),
   109  		autorest.ByClosing())
   110  	result.Response = autorest.Response{Response: resp}
   111  	return
   112  }
   113  

View as plain text