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

Get remote config template by version number #874

Merged
merged 5 commits into from
May 7, 2020

Conversation

lahirumaramba
Copy link
Member

  • Add support to get remote config template by version number

NOTE: API proposal is still under review. Getting a head start

public getTemplate(): Promise<RemoteConfigTemplate> {
public getTemplate(versionNumber?: number | string): Promise<RemoteConfigTemplate> {
let path = 'remoteConfig';
if (versionNumber) {
Copy link
Member Author

Choose a reason for hiding this comment

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

If the versionNumber is an invalid string (NaN) the backend API will return an error similar to the following:

errorInfo:
   { code: 'remote-config/invalid-argument',
     message: 'Invalid value at \'version_number\' (TYPE_INT64), "abc"' },
  codePrefix: 'remote-config' }

We could add a check here before making the request, but converting a string to a number to check isNaN will be tricky as the string is in int64 format. Appreciate any thoughts on this! :)

Copy link
Member Author

Choose a reason for hiding this comment

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

We could also run a RegEx to check if the string only contains numbers @hiranya911

Copy link
Contributor

Choose a reason for hiding this comment

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

How about:

const version = validators.isNumber(versionNumber) ? versionNumber : parseFloat(versionNumber);
if (!Number.isInteger(version)) {
  throw
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! I am fine with this. If the string prefixes with numbers and followed by characters, parseFloat ignores the characters and parses the number in string.

ex: 70abc becomes 70

I believe this is okay, unless we would like further validate with a regex

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

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

Looks mostly good. Few things to improve.

src/remote-config/remote-config-api-client.ts Outdated Show resolved Hide resolved
src/remote-config/remote-config-api-client.ts Outdated Show resolved Hide resolved
public getTemplate(): Promise<RemoteConfigTemplate> {
public getTemplate(versionNumber?: number | string): Promise<RemoteConfigTemplate> {
let path = 'remoteConfig';
if (versionNumber) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about:

const version = validators.isNumber(versionNumber) ? versionNumber : parseFloat(versionNumber);
if (!Number.isInteger(version)) {
  throw
}

return this.getUrl()
.then((url) => {
const request: HttpRequestConfig = {
method: 'GET',
url: `${url}/remoteConfig`,
url: `${url}/${path}`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of appending to URL, try passing:

data: {versionNumber: `${version}`},

src/remote-config/remote-config-api-client.ts Outdated Show resolved Hide resolved
@lahirumaramba
Copy link
Member Author

Updated and added a new test case for version number validation

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

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

Almost there. Few suggestions.

let path = 'remoteConfig';
if (versionNumber) {
path += '?versionNumber=' + versionNumber;
let requestData: any;
Copy link
Contributor

Choose a reason for hiding this comment

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

const requestData = {};

headers: FIREBASE_REMOTE_CONFIG_HEADERS
url: `${url}/remoteConfig`,
headers: FIREBASE_REMOTE_CONFIG_HEADERS,
...requestData
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not very readable. Change to data: requestData once the above changes are made.

Copy link
Member Author

Choose a reason for hiding this comment

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

I used the spread syntax here to avoid sending an empty object for data when no version number is provided. Will update the code to be more readable :)

let requestData: any;
if (typeof versionNumber !== 'undefined') {
this.validateVersionNumber(versionNumber);
requestData = { versionNumber: `${versionNumber}` };
Copy link
Contributor

Choose a reason for hiding this comment

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

requestData['versionNumber'] = `${versionNumber}`

'versionNumber must be a non-empty string in int64 format or a number');
}
const version = validator.isNumber(versionNumber) ?
versionNumber : parseFloat(versionNumber as string);
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like Number(str) provides stricter parsing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Number is 64 bit double (2^53 - 1) so I was concerned at first as we cannot represent int 64 (2^63 - 1). But it looks like Number.isInteger(Number(version)) should work here.

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM 👍

@lahirumaramba
Copy link
Member Author

Hi @egilmorez! Please review the docs for the new changes. Thanks!

Copy link
Contributor

@egilmorez egilmorez left a comment

Choose a reason for hiding this comment

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

One nit, otherwise LG, thanks!

src/index.d.ts Outdated
*
*
* @param versionNumber Version number of the Remote Config template to look up.
* If not specified, the latest Remote Config template will be returned. Optional.
Copy link
Contributor

Choose a reason for hiding this comment

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

Always prefer "is" to "will be" if it works. It does here! :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! Thank you. Updated!

@lahirumaramba lahirumaramba merged commit e7af8bb into remote-config-vc May 7, 2020
@lahirumaramba lahirumaramba deleted the lm-rc-getversion branch May 7, 2020 18:20
lahirumaramba added a commit that referenced this pull request Jun 25, 2020
* Get remote config template by version number (#874)

* Get remote config template by version number

* Refactor unit tests for Remote Config (#884)

* Refactor unit tests

* Add Remote Config Rollback operation (#885)

* Add Remote Config Rollback operation

* Update docs and move etag validation to a helper function

* Update docs

* Introduce a util to create a template from API response

* PR fixes

* Remote Config Add list versions operation (#896)

* Remote Config: Add list versions operation

* Add version Impl and other PR fixes

* PR fixes

* Imrpoved unit tests and some code clean up

* Fix code formatting

* Add a separate function to get a Template with version (#902)

* Add version meta data to RC templates (#906)

* Add version meta data to RC templates

* PR fixes

* Use assertion to unwrap template.version

* Update Remote Config Docstrings in index.d.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants