Skip to content

Instantly share code, notes, and snippets.

@lmazuel
Last active September 1, 2023 22:54
Show Gist options
  • Save lmazuel/fd08dbb5fc5aa469225494e507dea095 to your computer and use it in GitHub Desktop.
Save lmazuel/fd08dbb5fc5aa469225494e507dea095 to your computer and use it in GitHub Desktop.
LRO fun in TypeSpec
import "@typespec/http";
import "@typespec/rest";
import "@typespec/versioning";
import "@azure-tools/typespec-azure-core";
using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.Versioning;
using Azure.Core;
using Azure.Core.Traits;
@useAuth(
ApiKeyAuth<ApiKeyLocation.header, "api-key"> | OAuth2Auth<[
{
type: OAuth2FlowType.implicit,
authorizationUrl: "https://login.contoso.com/common/oauth2/v2.0/authorize",
scopes: ["https://widget.contoso.com/.default"],
}
]>
)
@service({
title: "Contoso Widget Manager",
})
@server(
"{endpoint}/widget",
"Contoso Widget APIs",
{
@doc("""
Supported Widget Services endpoints (protocol and hostname, for example:
https://westus.api.widget.contoso.com).
""")
endpoint: string,
}
)
@versioned(Contoso.WidgetManager.Versions)
namespace Contoso.WidgetManager;
@doc("The Contoso Widget Manager service version.")
enum Versions {
@doc("Version 2022-08-31")
@useDependency(Azure.Core.Versions.v1_0_Preview_2)
`2022-08-30`,
}
@doc("A sample widget")
model SimpleWidget {
@doc("The widget identity")
@key
@segment("simpleWidgets")
@visibility("read")
@path
id: string;
operationId: string;
@doc("A value")
value: string;
}
@doc("simple polling status")
model PollingStatus {
@doc("PollingLocation")
@header location?: ResourceLocation<PollingStatus>;
@doc("The status of the operation")
@Azure.Core.lroStatus
statusValue: "Succeeded" | "Canceled" | "Failed" | "Running";
}
model WidgetStatusMonitorResponse {
@pollingLocation
@header("Operation-Location")
operationLocation: ResourceLocation<ResourceOperationStatus<SimpleWidget>>;
// following the url in this header will provide a response of type ResourceOperationStatus<Widget>
}
model MyStatus is ResourceOperationStatus<SimpleWidget>;
@doc("Get a widget")
@route("/simpleWidgets2/{id}")
@get op getWidget(@doc("The id") @path id: string): SimpleWidget;
@doc("get lro status")
@route("/simpleWidgets2/{id}/operations/{operationId}")
@get op getStatus(
@doc("The id") @path id: string,
@doc("The operation") @path operationId: string
): PollingStatus;
@doc("Create a widget")
@finalOperation(getWidget)
@pollingOperation(getStatus)
@route("/simpleWidgets2/{id}")
@put op createWidget(
@doc("The id") @path id: string,
@doc("The request body")body: SimpleWidget
) : {
@statusCode _: 202;
@pollingOperationParameter(getStatus::parameters.id) @header id: string,
@pollingOperationParameter(getStatus::parameters.operationId) @header("operation-id") operation: string
};
op getWidgetOperationStatus is GetResourceOperationStatus<SimpleWidget>;
@pollingOperation(getWidgetOperationStatus,
{
@doc("The id")id: ResponseProperty<"id">;
@doc("The operation")operationId: ResponseProperty<"operationId">
})
op createWidgetAsync is LongRunningResourceCreateOrReplace<SimpleWidget>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment