Skip to content

Commit da5e55f

Browse files
authored
Enhance API and remove the code from DefaultApi (which is auto-generated code) (#1833)
1 parent 632dae7 commit da5e55f

6 files changed

Lines changed: 209 additions & 214 deletions

File tree

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-common/report-DefaultApiClient.api.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,6 @@ export class DefaultApiClient {
1313
fetch: typeof fetch;
1414
};
1515
});
16-
// Warning: (ae-forgotten-export) The symbol "CostManagementReport" needs to be exported by the entry point index.d.ts
17-
getCostManagementReport(
18-
request: {
19-
query: {
20-
currency?: CurrencyCode;
21-
delta?: string;
22-
'filter[limit]'?: number;
23-
'filter[offset]'?: number;
24-
'filter[resolution]'?: 'daily' | 'monthly';
25-
'filter[time_scope_units]'?: 'day' | 'month';
26-
'filter[time_scope_value]'?: number;
27-
'group_by[project]'?: '*' | string;
28-
'group_by[cluster]'?: '*' | string;
29-
'group_by[node]'?: '*' | string;
30-
'group_by[tag]'?: '*' | string;
31-
'order_by[cost]'?: 'asc' | 'desc';
32-
'order_by[distributed_cost]'?: 'asc' | 'desc';
33-
'order_by[markup_cost]'?: 'asc' | 'desc';
34-
'order_by[raw_cost]'?: 'asc' | 'desc';
35-
[key: string]: string | number | undefined;
36-
};
37-
},
38-
options?: RequestOptions,
39-
): Promise<TypedResponse<CostManagementReport>>;
4016
// Warning: (ae-forgotten-export) The symbol "RecommendationBoxPlots" needs to be exported by the entry point index.d.ts
4117
getRecommendationById(
4218
request: {
@@ -88,9 +64,5 @@ export type TypedResponse<T> = Omit<Response, 'json'> & {
8864
json: () => Promise<T>;
8965
};
9066

91-
// Warnings were encountered during analysis:
92-
//
93-
// src/generated/apis/DefaultApi.client.d.ts:91:13 - (ae-forgotten-export) The symbol "CurrencyCode" needs to be exported by the entry point index.d.ts
94-
9567
// (No @packageDocumentation comment for this package)
9668
```

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-common/report-clients.api.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,27 @@ export interface GetAccessResponse {
156156
}
157157

158158
// @public (undocumented)
159-
export type GetCostManagementRequest = Parameters<
160-
OptimizationsApi['getCostManagementReport']
161-
>[0];
159+
export interface GetCostManagementRequest {
160+
// (undocumented)
161+
query: {
162+
currency?: CurrencyCode;
163+
delta?: string;
164+
'filter[limit]'?: number;
165+
'filter[offset]'?: number;
166+
'filter[resolution]'?: 'daily' | 'monthly';
167+
'filter[time_scope_units]'?: 'day' | 'month';
168+
'filter[time_scope_value]'?: number;
169+
'group_by[project]'?: '*' | string;
170+
'group_by[cluster]'?: '*' | string;
171+
'group_by[node]'?: '*' | string;
172+
'group_by[tag]'?: '*' | string;
173+
'order_by[cost]'?: 'asc' | 'desc';
174+
'order_by[distributed_cost]'?: 'asc' | 'desc';
175+
'order_by[markup_cost]'?: 'asc' | 'desc';
176+
'order_by[raw_cost]'?: 'asc' | 'desc';
177+
[key: string]: string | number | undefined;
178+
};
179+
}
162180

163181
// @public (undocumented)
164182
export type GetRecommendationByIdRequest = Parameters<
@@ -193,6 +211,9 @@ export type OptimizationsApi = Omit<
193211
InstanceType<typeof DefaultApiClient>,
194212
'fetchApi' | 'discoveryApi'
195213
> & {
214+
getCostManagementReport(
215+
request: GetCostManagementRequest,
216+
): Promise<TypedResponse<CostManagementReport>>;
196217
searchOpenShiftProjects(search?: string): Promise<
197218
TypedResponse<{
198219
data: Array<{
@@ -351,7 +372,7 @@ export interface Tag {
351372

352373
// Warnings were encountered during analysis:
353374
//
354-
// src/clients/optimizations/types.d.ts:5:5 - (ae-forgotten-export) The symbol "TypedResponse" needs to be exported by the entry point index.d.ts
375+
// src/clients/optimizations/types.d.ts:27:5 - (ae-forgotten-export) The symbol "TypedResponse" needs to be exported by the entry point index.d.ts
355376

356377
// (No @packageDocumentation comment for this package)
357378
```

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-common/report.api.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -325,29 +325,6 @@ export class DefaultApiClient {
325325
fetch: typeof fetch;
326326
};
327327
});
328-
getCostManagementReport(
329-
request: {
330-
query: {
331-
currency?: CurrencyCode;
332-
delta?: string;
333-
'filter[limit]'?: number;
334-
'filter[offset]'?: number;
335-
'filter[resolution]'?: 'daily' | 'monthly';
336-
'filter[time_scope_units]'?: 'day' | 'month';
337-
'filter[time_scope_value]'?: number;
338-
'group_by[project]'?: '*' | string;
339-
'group_by[cluster]'?: '*' | string;
340-
'group_by[node]'?: '*' | string;
341-
'group_by[tag]'?: '*' | string;
342-
'order_by[cost]'?: 'asc' | 'desc';
343-
'order_by[distributed_cost]'?: 'asc' | 'desc';
344-
'order_by[markup_cost]'?: 'asc' | 'desc';
345-
'order_by[raw_cost]'?: 'asc' | 'desc';
346-
[key: string]: string | number | undefined;
347-
};
348-
},
349-
options?: RequestOptions,
350-
): Promise<TypedResponse<CostManagementReport>>;
351328
getRecommendationById(
352329
request: {
353330
path: {
@@ -411,9 +388,27 @@ export interface GetAccessResponse {
411388
}
412389

413390
// @public (undocumented)
414-
export type GetCostManagementRequest = Parameters<
415-
OptimizationsApi['getCostManagementReport']
416-
>[0];
391+
export interface GetCostManagementRequest {
392+
// (undocumented)
393+
query: {
394+
currency?: CurrencyCode;
395+
delta?: string;
396+
'filter[limit]'?: number;
397+
'filter[offset]'?: number;
398+
'filter[resolution]'?: 'daily' | 'monthly';
399+
'filter[time_scope_units]'?: 'day' | 'month';
400+
'filter[time_scope_value]'?: number;
401+
'group_by[project]'?: '*' | string;
402+
'group_by[cluster]'?: '*' | string;
403+
'group_by[node]'?: '*' | string;
404+
'group_by[tag]'?: '*' | string;
405+
'order_by[cost]'?: 'asc' | 'desc';
406+
'order_by[distributed_cost]'?: 'asc' | 'desc';
407+
'order_by[markup_cost]'?: 'asc' | 'desc';
408+
'order_by[raw_cost]'?: 'asc' | 'desc';
409+
[key: string]: string | number | undefined;
410+
};
411+
}
417412

418413
// @public (undocumented)
419414
export type GetRecommendationByIdRequest = Parameters<
@@ -546,6 +541,9 @@ export type OptimizationsApi = Omit<
546541
InstanceType<typeof DefaultApiClient>,
547542
'fetchApi' | 'discoveryApi'
548543
> & {
544+
getCostManagementReport(
545+
request: GetCostManagementRequest,
546+
): Promise<TypedResponse<CostManagementReport>>;
549547
searchOpenShiftProjects(search?: string): Promise<
550548
TypedResponse<{
551549
data: Array<{

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-common/src/clients/optimizations/OptimizationsClient.ts

Lines changed: 131 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -166,26 +166,126 @@ export class OptimizationsClient implements OptimizationsApi {
166166
this.token = accessToken;
167167
}
168168

169-
// Call the cost-management API via backend proxy
170-
let response = await this.defaultClient.getCostManagementReport(request, {
171-
token: this.token,
172-
});
169+
// Get the proxy base URL for cost-management API
170+
const baseUrl = await this.discoveryApi.getBaseUrl('proxy');
171+
const uri = '/cost-management/v1/reports/openshift/costs/';
173172

174-
// Handle 401 errors by refreshing token and retrying
175-
if (!response.ok && response.status === 401) {
176-
const { accessToken } = await this.getNewToken();
177-
this.token = accessToken;
173+
const queryParams = this.buildCostManagementQueryParams(request);
174+
const queryString = queryParams.toString();
175+
const queryPart = queryString ? `?${queryString}` : '';
176+
const url = `${baseUrl}${uri}${queryPart}`;
178177

179-
response = await this.defaultClient.getCostManagementReport(request, {
180-
token: this.token,
181-
});
178+
return await this.fetchWithTokenAndRetry<CostManagementReport>(url);
179+
}
180+
181+
/**
182+
* Builds query parameters for cost management report request
183+
* @param request - The cost management request
184+
* @returns URLSearchParams with all query parameters
185+
*/
186+
private buildCostManagementQueryParams(
187+
request: GetCostManagementRequest,
188+
): URLSearchParams {
189+
const queryParams = new URLSearchParams();
190+
this.appendBasicQueryParams(queryParams, request);
191+
this.appendDynamicParams(queryParams, request, 'group_by[');
192+
this.appendDynamicParams(queryParams, request, 'order_by[');
193+
this.appendDynamicFilterParams(queryParams, request);
194+
return queryParams;
195+
}
196+
197+
/**
198+
* Appends basic query parameters (currency, delta, and standard filters)
199+
*/
200+
private appendBasicQueryParams(
201+
queryParams: URLSearchParams,
202+
request: GetCostManagementRequest,
203+
): void {
204+
const basicParams: Array<{
205+
key: keyof typeof request.query;
206+
paramName: string;
207+
needsStringConversion?: boolean;
208+
}> = [
209+
{ key: 'currency', paramName: 'currency' },
210+
{ key: 'delta', paramName: 'delta' },
211+
{
212+
key: 'filter[limit]',
213+
paramName: 'filter[limit]',
214+
needsStringConversion: true,
215+
},
216+
{
217+
key: 'filter[offset]',
218+
paramName: 'filter[offset]',
219+
needsStringConversion: true,
220+
},
221+
{ key: 'filter[resolution]', paramName: 'filter[resolution]' },
222+
{
223+
key: 'filter[time_scope_units]',
224+
paramName: 'filter[time_scope_units]',
225+
},
226+
{
227+
key: 'filter[time_scope_value]',
228+
paramName: 'filter[time_scope_value]',
229+
needsStringConversion: true,
230+
},
231+
];
232+
233+
for (const { key, paramName, needsStringConversion } of basicParams) {
234+
const value = request.query[key];
235+
if (value) {
236+
queryParams.append(
237+
paramName,
238+
needsStringConversion ? String(value) : (value as string),
239+
);
240+
}
182241
}
242+
}
183243

184-
if (!response.ok) {
185-
throw new Error(response.statusText);
244+
/**
245+
* Appends dynamic parameters that match a specific prefix (e.g., 'group_by[', 'order_by[')
246+
*/
247+
private appendDynamicParams(
248+
queryParams: URLSearchParams,
249+
request: GetCostManagementRequest,
250+
prefix: string,
251+
): void {
252+
for (const key of Object.keys(request.query)) {
253+
if (key.startsWith(prefix) && key.endsWith(']')) {
254+
const value = request.query[key as keyof typeof request.query];
255+
if (value) {
256+
queryParams.append(key, String(value));
257+
}
258+
}
186259
}
260+
}
187261

188-
return response;
262+
/**
263+
* Appends dynamic filter parameters, excluding those already handled explicitly
264+
*/
265+
private appendDynamicFilterParams(
266+
queryParams: URLSearchParams,
267+
request: GetCostManagementRequest,
268+
): void {
269+
const handledFilterKeys = new Set([
270+
'filter[limit]',
271+
'filter[offset]',
272+
'filter[resolution]',
273+
'filter[time_scope_units]',
274+
'filter[time_scope_value]',
275+
]);
276+
277+
for (const key of Object.keys(request.query)) {
278+
if (
279+
key.startsWith('filter[') &&
280+
key.endsWith(']') &&
281+
!handledFilterKeys.has(key)
282+
) {
283+
const value = request.query[key as keyof typeof request.query];
284+
if (value) {
285+
queryParams.append(key, String(value));
286+
}
287+
}
288+
}
189289
}
190290

191291
/**
@@ -254,6 +354,21 @@ export class OptimizationsClient implements OptimizationsApi {
254354
this.token = accessToken;
255355
}
256356

357+
return await this.fetchWithTokenAndRetry<{
358+
data: Array<{ value: string }>;
359+
meta?: any;
360+
links?: any;
361+
}>(url);
362+
}
363+
364+
/**
365+
* Fetches a URL with token authentication, handles 401 errors by refreshing token and retrying
366+
* @param url - The URL to fetch
367+
* @returns TypedResponse with the response data
368+
*/
369+
private async fetchWithTokenAndRetry<T>(
370+
url: string,
371+
): Promise<TypedResponse<T>> {
257372
// Call the API via backend proxy
258373
let response = await this.fetchApi.fetch(url, {
259374
headers: {
@@ -284,12 +399,8 @@ export class OptimizationsClient implements OptimizationsApi {
284399
return {
285400
...response,
286401
json: async () => {
287-
const data = await response.json();
288-
return data as {
289-
data: Array<{ value: string }>;
290-
meta?: any;
291-
links?: any;
292-
};
402+
const data = (await response.json()) as T;
403+
return data;
293404
},
294405
};
295406
}

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-common/src/clients/optimizations/types.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,41 @@
1616

1717
import { DefaultApiClient } from '../../generated/apis/DefaultApi.client';
1818
import type { TypedResponse } from '../../generated/apis/DefaultApi.client';
19+
import type {
20+
CostManagementReport,
21+
CurrencyCode,
22+
} from '../types/cost-management';
23+
24+
/** @public */
25+
export interface GetCostManagementRequest {
26+
query: {
27+
currency?: CurrencyCode;
28+
delta?: string;
29+
'filter[limit]'?: number;
30+
'filter[offset]'?: number;
31+
'filter[resolution]'?: 'daily' | 'monthly';
32+
'filter[time_scope_units]'?: 'day' | 'month';
33+
'filter[time_scope_value]'?: number;
34+
'group_by[project]'?: '*' | string;
35+
'group_by[cluster]'?: '*' | string;
36+
'group_by[node]'?: '*' | string;
37+
'group_by[tag]'?: '*' | string;
38+
'order_by[cost]'?: 'asc' | 'desc';
39+
'order_by[distributed_cost]'?: 'asc' | 'desc';
40+
'order_by[markup_cost]'?: 'asc' | 'desc';
41+
'order_by[raw_cost]'?: 'asc' | 'desc';
42+
[key: string]: string | number | undefined;
43+
};
44+
}
1945

2046
/** @public */
2147
export type OptimizationsApi = Omit<
2248
InstanceType<typeof DefaultApiClient>,
2349
'fetchApi' | 'discoveryApi'
2450
> & {
51+
getCostManagementReport(
52+
request: GetCostManagementRequest,
53+
): Promise<TypedResponse<CostManagementReport>>;
2554
searchOpenShiftProjects(
2655
search?: string,
2756
): Promise<
@@ -39,11 +68,6 @@ export type OptimizationsApi = Omit<
3968
>;
4069
};
4170

42-
/** @public */
43-
export type GetCostManagementRequest = Parameters<
44-
OptimizationsApi['getCostManagementReport']
45-
>[0];
46-
4771
/**
4872
* This is a copy of GetTokenResponse, to avoid importing redhat-resource-optimization-backend.
4973
*

0 commit comments

Comments
 (0)