|
1 |
| -import { _OAuthHttpClient } from '@dynatrace-sdk/http-client'; |
| 1 | +import { _OAuthHttpClient, HttpClientRequestOptions, HttpClientResponse, RequestBodyTypes } from '@dynatrace-sdk/http-client'; |
2 | 2 | import { getSSOUrl } from 'dt-app';
|
| 3 | +import { version as VERSION } from '../package.json'; |
3 | 4 |
|
4 |
| - |
5 |
| -// {"errorCode":400,"message":"Bad Request","issueId":"<some-id>","error":"invalid_request","error_description":""} |
6 |
| - |
| 5 | +// Define the OAuthTokenResponse interface to match the expected structure of the response |
7 | 6 | export interface OAuthTokenResponse {
|
8 | 7 | scope?: string;
|
9 | 8 | token_type?: string;
|
@@ -46,6 +45,32 @@ const requestToken = async (clientId: string, clientSecret: string, authUrl: str
|
46 | 45 | return await res.json();
|
47 | 46 | }
|
48 | 47 |
|
| 48 | +/** |
| 49 | + * ExtendedOAuthClient that takes parameters for clientId, secret, scopes, environmentUrl, authUrl, and the version of the dynatrace-mcp-server |
| 50 | + */ |
| 51 | +export class ExtendedOauthClient extends _OAuthHttpClient { |
| 52 | + constructor(config: { |
| 53 | + clientId: string; |
| 54 | + secret: string; |
| 55 | + scopes: string[]; |
| 56 | + environmentUrl: string; |
| 57 | + authUrl: string; |
| 58 | + }, protected userAgent: string) { |
| 59 | + super(config); |
| 60 | + } |
| 61 | + |
| 62 | + send<T extends keyof RequestBodyTypes = 'json'>(options: HttpClientRequestOptions<T>): Promise<HttpClientResponse> { |
| 63 | + // add the user-agent header to the request |
| 64 | + options.headers = { |
| 65 | + ...options.headers, |
| 66 | + 'User-Agent': this.userAgent, |
| 67 | + }; |
| 68 | + // call the parent send method |
| 69 | + return super.send(options); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | + |
49 | 74 | /** Create an Oauth Client based on clientId, clientSecret, environmentUrl and scopes */
|
50 | 75 | export const createOAuthClient = async (clientId: string, clientSecret: string, environmentUrl: string, scopes: string[]): Promise<_OAuthHttpClient> => {
|
51 | 76 | if (!clientId) {
|
@@ -73,13 +98,15 @@ export const createOAuthClient = async (clientId: string, clientSecret: string,
|
73 | 98 | }
|
74 | 99 | console.error(`Successfully retrieved token from SSO!`);
|
75 | 100 |
|
76 |
| - return new _OAuthHttpClient({ |
| 101 | + const userAgent = `dynatrace-mcp-server/v${VERSION} (${process.platform}-${process.arch})` |
| 102 | + |
| 103 | + return new ExtendedOauthClient({ |
77 | 104 | scopes,
|
78 | 105 | clientId,
|
79 | 106 | secret: clientSecret,
|
80 | 107 | environmentUrl,
|
81 | 108 | authUrl: ssoAuthUrl,
|
82 |
| - }); |
| 109 | + }, userAgent); |
83 | 110 | };
|
84 | 111 |
|
85 | 112 | /** Helper function to call an app-function via platform-api */
|
|
0 commit comments