|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | + |
| 5 | +package datadogV2 |
| 6 | + |
| 7 | +import ( |
| 8 | + _context "context" |
| 9 | + _fmt "fmt" |
| 10 | + _log "log" |
| 11 | + _nethttp "net/http" |
| 12 | + _neturl "net/url" |
| 13 | + |
| 14 | + "github.com/DataDog/datadog-api-client-go/v2/api/datadog" |
| 15 | +) |
| 16 | + |
| 17 | +// TestOptimizationApi service type |
| 18 | +type TestOptimizationApi datadog.Service |
| 19 | + |
| 20 | +// SearchFlakyTestsOptionalParameters holds optional parameters for SearchFlakyTests. |
| 21 | +type SearchFlakyTestsOptionalParameters struct { |
| 22 | + Body *FlakyTestsSearchRequest |
| 23 | +} |
| 24 | + |
| 25 | +// NewSearchFlakyTestsOptionalParameters creates an empty struct for parameters. |
| 26 | +func NewSearchFlakyTestsOptionalParameters() *SearchFlakyTestsOptionalParameters { |
| 27 | + this := SearchFlakyTestsOptionalParameters{} |
| 28 | + return &this |
| 29 | +} |
| 30 | + |
| 31 | +// WithBody sets the corresponding parameter name and returns the struct. |
| 32 | +func (r *SearchFlakyTestsOptionalParameters) WithBody(body FlakyTestsSearchRequest) *SearchFlakyTestsOptionalParameters { |
| 33 | + r.Body = &body |
| 34 | + return r |
| 35 | +} |
| 36 | + |
| 37 | +// SearchFlakyTests Search flaky tests. |
| 38 | +// List endpoint returning flaky tests from Flaky Test Management. Results are paginated. |
| 39 | +func (a *TestOptimizationApi) SearchFlakyTests(ctx _context.Context, o ...SearchFlakyTestsOptionalParameters) (FlakyTestsSearchResponse, *_nethttp.Response, error) { |
| 40 | + var ( |
| 41 | + localVarHTTPMethod = _nethttp.MethodPost |
| 42 | + localVarPostBody interface{} |
| 43 | + localVarReturnValue FlakyTestsSearchResponse |
| 44 | + optionalParams SearchFlakyTestsOptionalParameters |
| 45 | + ) |
| 46 | + |
| 47 | + if len(o) > 1 { |
| 48 | + return localVarReturnValue, nil, datadog.ReportError("only one argument of type SearchFlakyTestsOptionalParameters is allowed") |
| 49 | + } |
| 50 | + if len(o) == 1 { |
| 51 | + optionalParams = o[0] |
| 52 | + } |
| 53 | + |
| 54 | + operationId := "v2.SearchFlakyTests" |
| 55 | + isOperationEnabled := a.Client.Cfg.IsUnstableOperationEnabled(operationId) |
| 56 | + if !isOperationEnabled { |
| 57 | + return localVarReturnValue, nil, datadog.GenericOpenAPIError{ErrorMessage: _fmt.Sprintf("Unstable operation '%s' is disabled", operationId)} |
| 58 | + } |
| 59 | + if isOperationEnabled && a.Client.Cfg.Debug { |
| 60 | + _log.Printf("WARNING: Using unstable operation '%s'", operationId) |
| 61 | + } |
| 62 | + |
| 63 | + localBasePath, err := a.Client.Cfg.ServerURLWithContext(ctx, "v2.TestOptimizationApi.SearchFlakyTests") |
| 64 | + if err != nil { |
| 65 | + return localVarReturnValue, nil, datadog.GenericOpenAPIError{ErrorMessage: err.Error()} |
| 66 | + } |
| 67 | + |
| 68 | + localVarPath := localBasePath + "/api/v2/test/flaky-test-management/tests" |
| 69 | + |
| 70 | + localVarHeaderParams := make(map[string]string) |
| 71 | + localVarQueryParams := _neturl.Values{} |
| 72 | + localVarFormParams := _neturl.Values{} |
| 73 | + localVarHeaderParams["Content-Type"] = "application/json" |
| 74 | + localVarHeaderParams["Accept"] = "application/json" |
| 75 | + |
| 76 | + // body params |
| 77 | + if optionalParams.Body != nil { |
| 78 | + localVarPostBody = &optionalParams.Body |
| 79 | + } |
| 80 | + if a.Client.Cfg.DelegatedTokenConfig != nil { |
| 81 | + err = datadog.UseDelegatedTokenAuth(ctx, &localVarHeaderParams, a.Client.Cfg.DelegatedTokenConfig) |
| 82 | + if err != nil { |
| 83 | + return localVarReturnValue, nil, err |
| 84 | + } |
| 85 | + } else { |
| 86 | + datadog.SetAuthKeys( |
| 87 | + ctx, |
| 88 | + &localVarHeaderParams, |
| 89 | + [2]string{"apiKeyAuth", "DD-API-KEY"}, |
| 90 | + [2]string{"appKeyAuth", "DD-APPLICATION-KEY"}, |
| 91 | + ) |
| 92 | + } |
| 93 | + req, err := a.Client.PrepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, nil) |
| 94 | + if err != nil { |
| 95 | + return localVarReturnValue, nil, err |
| 96 | + } |
| 97 | + |
| 98 | + localVarHTTPResponse, err := a.Client.CallAPI(req) |
| 99 | + if err != nil || localVarHTTPResponse == nil { |
| 100 | + return localVarReturnValue, localVarHTTPResponse, err |
| 101 | + } |
| 102 | + |
| 103 | + localVarBody, err := datadog.ReadBody(localVarHTTPResponse) |
| 104 | + if err != nil { |
| 105 | + return localVarReturnValue, localVarHTTPResponse, err |
| 106 | + } |
| 107 | + |
| 108 | + if localVarHTTPResponse.StatusCode >= 300 { |
| 109 | + newErr := datadog.GenericOpenAPIError{ |
| 110 | + ErrorBody: localVarBody, |
| 111 | + ErrorMessage: localVarHTTPResponse.Status, |
| 112 | + } |
| 113 | + if localVarHTTPResponse.StatusCode == 400 || localVarHTTPResponse.StatusCode == 403 || localVarHTTPResponse.StatusCode == 429 { |
| 114 | + var v APIErrorResponse |
| 115 | + err = a.Client.Decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) |
| 116 | + if err != nil { |
| 117 | + return localVarReturnValue, localVarHTTPResponse, newErr |
| 118 | + } |
| 119 | + newErr.ErrorModel = v |
| 120 | + } |
| 121 | + return localVarReturnValue, localVarHTTPResponse, newErr |
| 122 | + } |
| 123 | + |
| 124 | + err = a.Client.Decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) |
| 125 | + if err != nil { |
| 126 | + newErr := datadog.GenericOpenAPIError{ |
| 127 | + ErrorBody: localVarBody, |
| 128 | + ErrorMessage: err.Error(), |
| 129 | + } |
| 130 | + return localVarReturnValue, localVarHTTPResponse, newErr |
| 131 | + } |
| 132 | + |
| 133 | + return localVarReturnValue, localVarHTTPResponse, nil |
| 134 | +} |
| 135 | + |
| 136 | +// SearchFlakyTestsWithPagination provides a paginated version of SearchFlakyTests returning a channel with all items. |
| 137 | +func (a *TestOptimizationApi) SearchFlakyTestsWithPagination(ctx _context.Context, o ...SearchFlakyTestsOptionalParameters) (<-chan datadog.PaginationResult[FlakyTest], func()) { |
| 138 | + ctx, cancel := _context.WithCancel(ctx) |
| 139 | + pageSize_ := int64(10) |
| 140 | + if len(o) == 0 { |
| 141 | + o = append(o, SearchFlakyTestsOptionalParameters{}) |
| 142 | + } |
| 143 | + if o[0].Body == nil { |
| 144 | + o[0].Body = NewFlakyTestsSearchRequest() |
| 145 | + } |
| 146 | + if o[0].Body.Data == nil { |
| 147 | + o[0].Body.Data = NewFlakyTestsSearchRequestData() |
| 148 | + } |
| 149 | + if o[0].Body.Data.Attributes == nil { |
| 150 | + o[0].Body.Data.Attributes = NewFlakyTestsSearchRequestAttributes() |
| 151 | + } |
| 152 | + if o[0].Body.Data.Attributes.Page == nil { |
| 153 | + o[0].Body.Data.Attributes.Page = NewFlakyTestsSearchPageOptions() |
| 154 | + } |
| 155 | + if o[0].Body.Data.Attributes.Page.Limit != nil { |
| 156 | + pageSize_ = *o[0].Body.Data.Attributes.Page.Limit |
| 157 | + } |
| 158 | + o[0].Body.Data.Attributes.Page.Limit = &pageSize_ |
| 159 | + |
| 160 | + items := make(chan datadog.PaginationResult[FlakyTest], pageSize_) |
| 161 | + go func() { |
| 162 | + for { |
| 163 | + resp, _, err := a.SearchFlakyTests(ctx, o...) |
| 164 | + if err != nil { |
| 165 | + var returnItem FlakyTest |
| 166 | + items <- datadog.PaginationResult[FlakyTest]{Item: returnItem, Error: err} |
| 167 | + break |
| 168 | + } |
| 169 | + respData, ok := resp.GetDataOk() |
| 170 | + if !ok { |
| 171 | + break |
| 172 | + } |
| 173 | + results := *respData |
| 174 | + |
| 175 | + for _, item := range results { |
| 176 | + select { |
| 177 | + case items <- datadog.PaginationResult[FlakyTest]{Item: item, Error: nil}: |
| 178 | + case <-ctx.Done(): |
| 179 | + close(items) |
| 180 | + return |
| 181 | + } |
| 182 | + } |
| 183 | + if len(results) < int(pageSize_) { |
| 184 | + break |
| 185 | + } |
| 186 | + cursorMeta, ok := resp.GetMetaOk() |
| 187 | + if !ok { |
| 188 | + break |
| 189 | + } |
| 190 | + cursorMetaPagination, ok := cursorMeta.GetPaginationOk() |
| 191 | + if !ok { |
| 192 | + break |
| 193 | + } |
| 194 | + cursorMetaPaginationNextPage, ok := cursorMetaPagination.GetNextPageOk() |
| 195 | + if !ok { |
| 196 | + break |
| 197 | + } |
| 198 | + |
| 199 | + o[0].Body.Data.Attributes.Page.Cursor = cursorMetaPaginationNextPage |
| 200 | + } |
| 201 | + close(items) |
| 202 | + }() |
| 203 | + return items, cancel |
| 204 | +} |
| 205 | + |
| 206 | +// NewTestOptimizationApi Returns NewTestOptimizationApi. |
| 207 | +func NewTestOptimizationApi(client *datadog.APIClient) *TestOptimizationApi { |
| 208 | + return &TestOptimizationApi{ |
| 209 | + Client: client, |
| 210 | + } |
| 211 | +} |
0 commit comments