Skip to content
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

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-network-providers",
"version": "2.6.0",
"version": "2.7.0",
"lockfileVersion": 2,
"requires": true,
"author": "MultiversX",
Expand Down
5 changes: 5 additions & 0 deletions src/NetworkProviderConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AxiosRequestConfig } from 'axios';
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


export interface ExtendedAxiosRequestConfig extends AxiosRequestConfig {
clientName?: string;
}
10 changes: 7 additions & 3 deletions src/apiNetworkProvider.ts
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";
Expand All @@ -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'

Copy link
Contributor

Choose a reason for hiding this comment

The 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 multiversx-sdk/api (or something similar - "multiversx" is necessary, to stand out from other possible libraries that talk to MultiversX APIs).

Constants can also be moved to constants.ts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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> {
Expand Down
10 changes: 7 additions & 3 deletions src/proxyNetworkProvider.ts
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";
Expand All @@ -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'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, multiversx-sdk/proxy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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> {
Expand Down
22 changes: 22 additions & 0 deletions src/userAgent.ts
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extendUserAgent or something similar (we are not clearing what has been previously set)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a small unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, tests added

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current calls of this function, config is never undefined. Thus, we can simplify the function signature (and the first lines of the implementation / dropping the first check).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check can be above const headers = ... (so that we have the checks nicely grouped above).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?")
Copy link
Contributor

Choose a reason for hiding this comment

The 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."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
const resolvedClientName = config.clientName || 'unknown';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placeholder can be moved to constants.ts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A space is missing between userAgentPrefix and resolvedClientName.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


headers.setUserAgent(newUserAgent, true);
}
Loading