Skip to content

Commit

Permalink
add coverage for repeatability (#2112)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhxiaofeng authored Nov 14, 2023
1 parent 8fdc016 commit cafd43f
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/typespec-ts/test/commands/cadl-ranch-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ export const modularTsps: TypeSpecRanchConfig[] = [
{
outputPath: "authentication/union",
inputPath: "authentication/union"
},
{
outputPath: "headers/repeatability",
inputPath: "special-headers/repeatability"
}
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { Pipeline } from "@azure/core-rest-pipeline";
import {
immediateSuccess,
createRepeatability,
RepeatabilityClientOptions,
RepeatabilityContext,
} from "./api/index.js";
import { ImmediateSuccessOptions } from "./models/options.js";

export { RepeatabilityClientOptions } from "./api/RepeatabilityContext.js";

export class RepeatabilityClient {
private _client: RepeatabilityContext;
/** The pipeline used by this client to make requests */
public readonly pipeline: Pipeline;

/** Illustrates OASIS repeatability headers */
constructor(options: RepeatabilityClientOptions = {}) {
this._client = createRepeatability(options);
this.pipeline = this._client.pipeline;
}

/** Check we recognize Repeatability-Request-ID and Repeatability-First-Sent. */
immediateSuccess(
repeatabilityRequestID: string,
repeatabilityFirstSent: Date,
options: ImmediateSuccessOptions = { requestOptions: {} }
): Promise<void> {
return immediateSuccess(
this._client,
repeatabilityRequestID,
repeatabilityFirstSent,
options
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { ClientOptions } from "@azure-rest/core-client";
import { RepeatabilityContext } from "../rest/index.js";
import getClient from "../rest/index.js";

export interface RepeatabilityClientOptions extends ClientOptions {}

export { RepeatabilityContext } from "../rest/index.js";

/** Illustrates OASIS repeatability headers */
export function createRepeatability(
options: RepeatabilityClientOptions = {}
): RepeatabilityContext {
const clientContext = getClient(options);
return clientContext;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export { immediateSuccess } from "./operations.js";
export {
createRepeatability,
RepeatabilityClientOptions,
RepeatabilityContext,
} from "./RepeatabilityContext.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import {
ImmediateSuccess204Response,
RepeatabilityContext as Client,
} from "../rest/index.js";
import {
StreamableMethod,
operationOptionsToRequestParameters,
} from "@azure-rest/core-client";
import { ImmediateSuccessOptions } from "../models/options.js";

export function _immediateSuccessSend(
context: Client,
repeatabilityRequestID: string,
repeatabilityFirstSent: Date,
options: ImmediateSuccessOptions = { requestOptions: {} }
): StreamableMethod<ImmediateSuccess204Response> {
return context
.path("/special-headers/repeatability/immediateSuccess")
.post({
...operationOptionsToRequestParameters(options),
headers: {
"Repeatability-Request-ID": repeatabilityRequestID,
"Repeatability-First-Sent": repeatabilityFirstSent.toUTCString(),
},
});
}

export async function _immediateSuccessDeserialize(
result: ImmediateSuccess204Response
): Promise<void> {
if (result.status !== "204") {
throw result.body;
}

return;
}

/** Check we recognize Repeatability-Request-ID and Repeatability-First-Sent. */
export async function immediateSuccess(
context: Client,
repeatabilityRequestID: string,
repeatabilityFirstSent: Date,
options: ImmediateSuccessOptions = { requestOptions: {} }
): Promise<void> {
const result = await _immediateSuccessSend(
context,
repeatabilityRequestID,
repeatabilityFirstSent,
options
);
return _immediateSuccessDeserialize(result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export {
RepeatabilityClient,
RepeatabilityClientOptions,
} from "./RepeatabilityClient.js";
export { ImmediateSuccessOptions } from "./models/index.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { createClientLogger } from "@azure/logger";
export const logger = createClientLogger("headers-repeatability");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export { ImmediateSuccessOptions } from "./options.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { OperationOptions } from "@azure-rest/core-client";

export interface ImmediateSuccessOptions extends OperationOptions {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { ImmediateSuccessParameters } from "./parameters.js";
import { ImmediateSuccess204Response } from "./responses.js";
import { Client, StreamableMethod } from "@azure-rest/core-client";

export interface ImmediateSuccess {
/** Check we recognize Repeatability-Request-ID and Repeatability-First-Sent. */
post(
options: ImmediateSuccessParameters
): StreamableMethod<ImmediateSuccess204Response>;
}

export interface Routes {
/** Resource for '/special-headers/repeatability/immediateSuccess' has methods for the following verbs: post */
(path: "/special-headers/repeatability/immediateSuccess"): ImmediateSuccess;
}

export type RepeatabilityContext = Client & {
path: Routes;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import RepeatabilityClient from "./repeatabilityClient.js";

export * from "./repeatabilityClient.js";
export * from "./parameters.js";
export * from "./responses.js";
export * from "./clientDefinitions.js";

export default RepeatabilityClient;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { RawHttpHeadersInput } from "@azure/core-rest-pipeline";
import { RequestParameters } from "@azure-rest/core-client";

export interface ImmediateSuccessHeaders {
"Repeatability-Request-ID": string;
"Repeatability-First-Sent": string;
}

export interface ImmediateSuccessHeaderParam {
headers: RawHttpHeadersInput & ImmediateSuccessHeaders;
}

export type ImmediateSuccessParameters = ImmediateSuccessHeaderParam &
RequestParameters;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "../logger.js";
import { RepeatabilityContext } from "./clientDefinitions.js";

/**
* Initialize a new instance of `RepeatabilityContext`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {}
): RepeatabilityContext {
const baseUrl = options.baseUrl ?? `http://localhost:3000`;
options.apiVersion = options.apiVersion ?? "1.0.0";
const userAgentInfo = `azsdk-js-headers-repeatability-rest/1.0.0-beta.1`;
const userAgentPrefix =
options.userAgentOptions && options.userAgentOptions.userAgentPrefix
? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}`
: `${userAgentInfo}`;
options = {
...options,
userAgentOptions: {
userAgentPrefix,
},
loggingOptions: {
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(baseUrl, options) as RepeatabilityContext;

return client;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { RawHttpHeaders } from "@azure/core-rest-pipeline";
import { HttpResponse } from "@azure-rest/core-client";

export interface ImmediateSuccess204Headers {
/** Indicates whether the repeatable request was accepted or rejected. */
"repeatability-result"?: "accepted" | "rejected";
}

/** There is no content to send for this request, but the headers may be useful. */
export interface ImmediateSuccess204Response extends HttpResponse {
status: "204";
headers: RawHttpHeaders & ImmediateSuccess204Headers;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
emit:
- "@azure-tools/typespec-ts"
options:
"@azure-tools/typespec-ts":
generateMetadata: false
generateTest: false
azureSdkForJs: false
isModularLibrary: true
hierarchyClient: false
"emitter-output-dir": "{project-root}/generated"
packageDetails:
name: "@msinternal/headers-repeatability"
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { RepeatabilityClient } from "./generated/headers/repeatability/generated/src/index.js";
import { assert } from "chai";

describe("Repeatability Client", () => {
let client: RepeatabilityClient;

beforeEach(() => {
client = new RepeatabilityClient({
allowInsecureConnection: true
});
});

it("should add client-request-id and date in header", async () => {
try {
const requestID = "86aede1f-96fa-4e7f-b1e1-bf8a947cb804";
const result = await client.immediateSuccess(
requestID,
new Date("Mon, 13 Nov 2023 14:38:00 GMT")
);
assert.isUndefined(result);
} catch (err) {
assert.fail(err as string);
}
});
});

0 comments on commit cafd43f

Please sign in to comment.