Skip to content

Commit

Permalink
Add version param to Rest.request
Browse files Browse the repository at this point in the history
Outstanding questions [1], [2] re spec

[1] https://ably-real-time.slack.com/archives/C030C5YLY/p1682618758910979
[2] https://ably-real-time.slack.com/archives/C030C5YLY/p1682621842854199

TODO test

The test changes are minimal but Prettier has decided to do some
re-indentation that messes things up
  • Loading branch information
lawrence-forooghian committed May 1, 2023
1 parent d001eb1 commit 5e6accb
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 72 deletions.
8 changes: 8 additions & 0 deletions ably.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,7 @@ declare namespace Types {
*
* @param method - The request method to use, such as `GET`, `POST`.
* @param path - The request path.
* @param version - TODO need canonical docs
* @param params - The parameters to include in the URL query of the request. The parameters depend on the endpoint being queried. See the [REST API reference](https://ably.com/docs/api/rest-api) for the available parameters of each endpoint.
* @param body - The JSON body of the request.
* @param headers - Additional HTTP headers to include in the request.
Expand All @@ -1649,6 +1650,7 @@ declare namespace Types {
request<T = any>(
method: string,
path: string,
version: number,
params?: any,
body?: any[] | any,
headers?: any,
Expand Down Expand Up @@ -1704,6 +1706,7 @@ declare namespace Types {
*
* @param method - The request method to use, such as `GET`, `POST`.
* @param path - The request path.
* @param version - TODO need canonical docs
* @param params - The parameters to include in the URL query of the request. The parameters depend on the endpoint being queried. See the [REST API reference](https://ably.com/docs/api/rest-api) for the available parameters of each endpoint.
* @param body - The JSON body of the request.
* @param headers - Additional HTTP headers to include in the request.
Expand All @@ -1712,6 +1715,7 @@ declare namespace Types {
request<T = any>(
method: string,
path: string,
version: number,
params?: any,
body?: any[] | any,
headers?: any
Expand Down Expand Up @@ -1782,6 +1786,7 @@ declare namespace Types {
*
* @param method - The request method to use, such as `GET`, `POST`.
* @param path - The request path.
* @param version - TODO need canonical docs
* @param params - The parameters to include in the URL query of the request. The parameters depend on the endpoint being queried. See the [REST API reference](https://ably.com/docs/api/rest-api) for the available parameters of each endpoint.
* @param body - The JSON body of the request.
* @param headers - Additional HTTP headers to include in the request.
Expand All @@ -1790,6 +1795,7 @@ declare namespace Types {
request<T = any>(
method: string,
path: string,
version: number,
params?: any,
body?: any[] | any,
headers?: any,
Expand Down Expand Up @@ -1841,6 +1847,7 @@ declare namespace Types {
*
* @param method - The request method to use, such as `GET`, `POST`.
* @param path - The request path.
* @param version - TODO need canonical docs
* @param params - The parameters to include in the URL query of the request. The parameters depend on the endpoint being queried. See the [REST API reference](https://ably.com/docs/api/rest-api) for the available parameters of each endpoint.
* @param body - The JSON body of the request.
* @param headers - Additional HTTP headers to include in the request.
Expand All @@ -1849,6 +1856,7 @@ declare namespace Types {
request<T = any>(
method: string,
path: string,
version: number,
params?: any,
body?: any[] | any,
headers?: any
Expand Down
7 changes: 4 additions & 3 deletions src/common/lib/client/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class Rest {
request(
method: string,
path: string,
version: number,
params: RequestParams,
body: unknown,
customHeaders: Record<string, string>,
Expand All @@ -182,12 +183,12 @@ class Rest {
const _method = method.toLowerCase() as HttpMethods;
const headers =
_method == 'get'
? Utils.defaultGetHeaders(this.options, { format })
: Utils.defaultPostHeaders(this.options, { format });
? Utils.defaultGetHeaders(this.options, { format, protocolVersion: version })
: Utils.defaultPostHeaders(this.options, { format, protocolVersion: version });

if (callback === undefined) {
if (this.options.promises) {
return Utils.promisify(this, 'request', [method, path, params, body, customHeaders]) as Promise<
return Utils.promisify(this, 'request', [method, path, version, params, body, customHeaders]) as Promise<
HttpPaginatedResponse<unknown>
>;
}
Expand Down
16 changes: 12 additions & 4 deletions src/common/lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,35 +353,43 @@ export enum Format {

export interface HeadersOptions {
format?: Format;
protocolVersion?: number;
}

const defaultHeadersOptions: Required<HeadersOptions> = {
format: Format.json,
protocolVersion: Defaults.protocolVersion,
};

export function defaultGetHeaders(
options: NormalisedClientOptions,
{ format = defaultHeadersOptions.format }: HeadersOptions = {}
{
format = defaultHeadersOptions.format,
protocolVersion = defaultHeadersOptions.protocolVersion,
}: HeadersOptions = {}
): Record<string, string> {
const accept = contentTypes[format];
return {
accept: accept,
'X-Ably-Version': Defaults.protocolVersion.toString(),
'X-Ably-Version': protocolVersion.toString(),
'Ably-Agent': getAgentString(options),
};
}

export function defaultPostHeaders(
options: NormalisedClientOptions,
{ format = defaultHeadersOptions.format }: HeadersOptions = {}
{
format = defaultHeadersOptions.format,
protocolVersion = defaultHeadersOptions.protocolVersion,
}: HeadersOptions = {}
): Record<string, string> {
let contentType;
const accept = (contentType = contentTypes[format]);

return {
accept: accept,
'content-type': contentType,
'X-Ably-Version': Defaults.protocolVersion.toString(),
'X-Ably-Version': protocolVersion.toString(),
'Ably-Agent': getAgentString(options),
};
}
Expand Down
70 changes: 40 additions & 30 deletions test/realtime/encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
var displayError = helper.displayError;
var encodingFixturesPath = helper.testResourcesPath + 'messages-encoding.json';
var closeAndFinish = helper.closeAndFinish;
var Defaults = Ably.Rest.Platform.Defaults;

describe('realtime/encoding', function () {
this.timeout(60 * 1000);
Expand Down Expand Up @@ -98,6 +99,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
realtime.request(
'post',
channelPath,
Defaults.protocolVersion,
null,
{ name: name, data: encodingSpec.data, encoding: encodingSpec.encoding },
null,
Expand Down Expand Up @@ -174,39 +176,47 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
eachOfCb(err);
return;
}
realtime.request('get', channelPath, null, null, null, function (err, resultPage) {
if (err) {
eachOfCb(err);
return;
}
try {
var msgs = helper.arrFilter(resultPage.items, function (m) {
return m.name === name;
});
expect(msgs.length).to.equal(
2,
'Check expected number of results (one from json rt, one from binary rt)'
);
expect(msgs[0].encoding == encodingSpec.encoding, 'Check encodings match').to.be.ok;
expect(msgs[1].encoding == encodingSpec.encoding, 'Check encodings match').to.be.ok;
if (msgs[0].encoding === 'json') {
expect(JSON.parse(encodingSpec.data)).to.deep.equal(
JSON.parse(msgs[0].data),
'Check data matches'
);
expect(JSON.parse(encodingSpec.data)).to.deep.equal(
JSON.parse(msgs[1].data),
'Check data matches'
realtime.request(
'get',
channelPath,
Defaults.protocolVersion,
null,
null,
null,
function (err, resultPage) {
if (err) {
eachOfCb(err);
return;
}
try {
var msgs = helper.arrFilter(resultPage.items, function (m) {
return m.name === name;
});
expect(msgs.length).to.equal(
2,
'Check expected number of results (one from json rt, one from binary rt)'
);
} else {
expect(encodingSpec.data).to.equal(msgs[0].data, 'Check data matches');
expect(encodingSpec.data).to.equal(msgs[1].data, 'Check data matches');
expect(msgs[0].encoding == encodingSpec.encoding, 'Check encodings match').to.be.ok;
expect(msgs[1].encoding == encodingSpec.encoding, 'Check encodings match').to.be.ok;
if (msgs[0].encoding === 'json') {
expect(JSON.parse(encodingSpec.data)).to.deep.equal(
JSON.parse(msgs[0].data),
'Check data matches'
);
expect(JSON.parse(encodingSpec.data)).to.deep.equal(
JSON.parse(msgs[1].data),
'Check data matches'
);
} else {
expect(encodingSpec.data).to.equal(msgs[0].data, 'Check data matches');
expect(encodingSpec.data).to.equal(msgs[1].data, 'Check data matches');
}
eachOfCb();
} catch (err) {
eachOfCb(err);
}
eachOfCb();
} catch (err) {
eachOfCb(err);
}
});
);
}
);
},
Expand Down
Loading

0 comments on commit 5e6accb

Please sign in to comment.