Export necessary interface and class for customize http client implemendation#86
Open
lkebin wants to merge 1 commit into
Open
Export necessary interface and class for customize http client implemendation#86lkebin wants to merge 1 commit into
lkebin wants to merge 1 commit into
Conversation
…entation Some application doesn't support standard `fetch` api. such as Wechat Miniprogram.
Contributor
|
Did you see: https://github.com/Dynatrace/openkit-js/blob/main/src/OpenKitBuilder.ts#L170 Which allows you to fully customize the whole communication channel? |
Author
|
Yes, but I need export more info for custom HttpClient implementation. It's easier than implements whole customize communication channel, right ? There's my implements. import { HttpClient, HttpResponse, Logger, LoggerFactory } from "@dynatrace/openkit-js";
import 'miniprogram-api-typings';
export class WxHttpClient implements HttpClient {
private readonly logger: Logger;
constructor(loggerFactory: LoggerFactory) {
this.logger = loggerFactory.createLogger('WxHttpClient');
}
public async get(url: string): Promise<HttpResponse> {
this.logger.debug('GET', url);
const response = await new Promise<WechatMiniprogram.RequestSuccessCallbackResult<string>>((resolve, reject) => {
wx.request({
url: url,
method: 'GET',
// Wechat disallow customize user-agent header of request
//
// header: {
// 'User-Agent': 'OpenKit/' + openKitVersion,
// },
dataType: '其他',
success: (res: WechatMiniprogram.RequestSuccessCallbackResult<string>) => {
resolve(res)
},
fail: (res) => {
reject(res)
},
})
})
return this.parseFetchResponse(response);
}
public async post(url: string, payload: string): Promise<HttpResponse> {
this.logger.debug('POST', url, payload);
const response = await new Promise<WechatMiniprogram.RequestSuccessCallbackResult<string>>((resolve, reject) => {
wx.request({
url: url,
method: 'POST',
data: payload,
// Wechat disallow customize user-agent header of request
//
// header: {
// 'User-Agent': 'OpenKit/' + openKitVersion,
// },
dataType: '其他',
success: (res: WechatMiniprogram.RequestSuccessCallbackResult<string>) => {
resolve(res)
},
fail: (res) => {
reject(res)
},
})
})
return this.parseFetchResponse(response);
}
private async parseFetchResponse(
response: WechatMiniprogram.RequestSuccessCallbackResult<string>,
): Promise<HttpResponse> {
const responseString = response.data;
this.logger.debug('RESPONSE', {
status: response.statusCode,
payload: responseString,
});
const headers: Record<string, string> = {};
Object.entries(response.header).forEach(([key, value]) => {
if (value != null) {
headers[key] = value.toString();
}
});
return {
status: response.statusCode,
payload: responseString,
headers,
};
}
} |
Contributor
|
I can follow your idea. Although this is not an issue, more like a feature request. So there needs to be an official feature request via the Dynatrace platform, in the meantime feel free to modify the library as it is open source anyways. |
Author
|
OK, will use forked version for customize platform until official supported. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some applications doesn't support standard
fetchapi. such as Wechat Miniprogram.