-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tool 233 extend user agent on js network providers #67
Changes from 3 commits
9a91315
b014180
691a1a9
e6a6609
8089c7e
fc2503a
9f9e675
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { AxiosRequestConfig } from 'axios'; | ||
|
||
export interface ExtendedAxiosRequestConfig extends AxiosRequestConfig { | ||
clientName?: string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import axios, { AxiosRequestConfig } from "axios"; | ||
import axios from "axios"; | ||
import { AccountOnNetwork, GuardianData } from "./accounts"; | ||
import { defaultAxiosConfig, defaultPagination } from "./config"; | ||
import { ContractQueryRequest } from "./contractQueryRequest"; | ||
|
@@ -16,17 +16,21 @@ import { DefinitionOfFungibleTokenOnNetwork, DefinitionOfTokenCollectionOnNetwor | |
import { FungibleTokenOfAccountOnNetwork, NonFungibleTokenOfAccountOnNetwork } from "./tokens"; | ||
import { TransactionOnNetwork, prepareTransactionForBroadcasting } from "./transactions"; | ||
import { TransactionStatus } from "./transactionStatus"; | ||
import { setUserAgent } from "./userAgent"; | ||
import { ExtendedAxiosRequestConfig } from "./NetworkProviderConfig"; | ||
|
||
// TODO: Find & remove duplicate code between "ProxyNetworkProvider" and "ApiNetworkProvider". | ||
export class ApiNetworkProvider implements INetworkProvider { | ||
private url: string; | ||
private config: AxiosRequestConfig; | ||
private config: ExtendedAxiosRequestConfig; | ||
private backingProxyNetworkProvider; | ||
private userAgentPrefix = 'sdk-network-providers/api' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the near future, we will unify the JS libraries. Thus, let's use Constants can also be moved to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
constructor(url: string, config?: AxiosRequestConfig) { | ||
constructor(url: string, config?: ExtendedAxiosRequestConfig) { | ||
this.url = url; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
this.config = { ...defaultAxiosConfig, ...config }; | ||
this.backingProxyNetworkProvider = new ProxyNetworkProvider(url, config); | ||
setUserAgent(this.userAgentPrefix, this.config); | ||
} | ||
|
||
async getNetworkConfig(): Promise<NetworkConfig> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import axios, { AxiosRequestConfig } from "axios"; | ||
import axios from "axios"; | ||
import { AccountOnNetwork, GuardianData } from "./accounts"; | ||
import { defaultAxiosConfig } from "./config"; | ||
import { EsdtContractAddress } from "./constants"; | ||
|
@@ -14,15 +14,19 @@ import { DefinitionOfFungibleTokenOnNetwork, DefinitionOfTokenCollectionOnNetwor | |
import { FungibleTokenOfAccountOnNetwork, NonFungibleTokenOfAccountOnNetwork } from "./tokens"; | ||
import { TransactionOnNetwork, prepareTransactionForBroadcasting } from "./transactions"; | ||
import { TransactionStatus } from "./transactionStatus"; | ||
import { setUserAgent } from "./userAgent"; | ||
import { ExtendedAxiosRequestConfig } from "./NetworkProviderConfig"; | ||
|
||
// TODO: Find & remove duplicate code between "ProxyNetworkProvider" and "ApiNetworkProvider". | ||
export class ProxyNetworkProvider implements INetworkProvider { | ||
private url: string; | ||
private config: AxiosRequestConfig; | ||
private config: ExtendedAxiosRequestConfig; | ||
private userAgentPrefix = 'sdk-network-providers/proxy' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
constructor(url: string, config?: AxiosRequestConfig) { | ||
constructor(url: string, config?: ExtendedAxiosRequestConfig) { | ||
this.url = url; | ||
this.config = { ...defaultAxiosConfig, ...config }; | ||
setUserAgent(this.userAgentPrefix, this.config); | ||
} | ||
|
||
async getNetworkConfig(): Promise<NetworkConfig> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AxiosHeaders } from "axios"; | ||
import { ExtendedAxiosRequestConfig } from "./NetworkProviderConfig"; | ||
|
||
export function setUserAgent(userAgentPrefix: string, config: ExtendedAxiosRequestConfig | undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a small unit test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, tests added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the current calls of this function, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
if (!config) { | ||
config = { headers: new AxiosHeaders({}) } | ||
} | ||
if (!config.headers) { | ||
config.headers = new AxiosHeaders({}) | ||
}; | ||
|
||
const headers = AxiosHeaders.from(config.headers as AxiosHeaders).normalize(true); | ||
if (!config.clientName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check can be above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
console.log("Can you please provide the client name of the aplication that uses the sdk?") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo. Can also be as advice instead of question e.g. "please provide ..., to be used for (shallow) analytics." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
const resolvedClientName = config.clientName || 'unknown'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Placeholder can be moved to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
const currentUserAgent = headers.hasUserAgent() ? headers.getUserAgent() : ''; | ||
const newUserAgent = `${currentUserAgent} ${userAgentPrefix}${resolvedClientName}`.trim(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A space is missing between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
headers.setUserAgent(newUserAgent, true); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filename should be with lower case, for consistency. Name of interface can also match the file name,
interface NetworkProviderConfig
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done