Skip to content

Commit

Permalink
[core] Core updates for Storage migration (#24356)
Browse files Browse the repository at this point in the history
Fix issues in core that were blocking Storage SDKs from migrating.
  • Loading branch information
xirzec committed Jan 3, 2023
1 parent a43aaf3 commit 637732a
Show file tree
Hide file tree
Showing 22 changed files with 307 additions and 60 deletions.
24 changes: 15 additions & 9 deletions common/config/rush/pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions sdk/core/core-client/CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
### Bugs Fixed

- Fix a serializer issue where resettable streams were not being accepted.
- Fix an issue where XML options are not propagated during deserialization.

### Other Changes

Expand Down
12 changes: 8 additions & 4 deletions sdk/core/core-client/src/deserializationPolicy.ts
Expand Up @@ -159,7 +159,8 @@ async function deserializeResponseBody(
const { error, shouldReturnResponse } = handleErrorResponse(
parsedResponse,
operationSpec,
responseSpec
responseSpec,
options
);
if (error) {
throw error;
Expand All @@ -182,7 +183,8 @@ async function deserializeResponseBody(
parsedResponse.parsedBody = operationSpec.serializer.deserialize(
responseSpec.bodyMapper,
valueToDeserialize,
"operationRes.parsedBody"
"operationRes.parsedBody",
options
);
} catch (deserializeError: any) {
const restError = new RestError(
Expand Down Expand Up @@ -223,7 +225,8 @@ function isOperationSpecEmpty(operationSpec: OperationSpec): boolean {
function handleErrorResponse(
parsedResponse: FullOperationResponse,
operationSpec: OperationSpec,
responseSpec: OperationResponseMap | undefined
responseSpec: OperationResponseMap | undefined,
options: RequiredSerializerOptions
): { error: RestError | null; shouldReturnResponse: boolean } {
const isSuccessByStatus = 200 <= parsedResponse.status && parsedResponse.status < 300;
const isExpectedStatusCode: boolean = isOperationSpecEmpty(operationSpec)
Expand Down Expand Up @@ -282,7 +285,8 @@ function handleErrorResponse(
deserializedError = operationSpec.serializer.deserialize(
defaultBodyMapper,
valueToDeserialize,
"error.response.parsedBody"
"error.response.parsedBody",
options
);
}

Expand Down
10 changes: 10 additions & 0 deletions sdk/core/core-client/src/operationHelpers.ts
Expand Up @@ -107,8 +107,18 @@ function getPropertyFromParameterPath(
}

const operationRequestMap = new WeakMap<OperationRequest, OperationRequestInfo>();
const originalRequestSymbol = Symbol.for("@azure/core-client original request");

function hasOriginalRequest(
request: OperationRequest
): request is OperationRequest & { [originalRequestSymbol]: OperationRequest } {
return originalRequestSymbol in request;
}

export function getOperationRequestInfo(request: OperationRequest): OperationRequestInfo {
if (hasOriginalRequest(request)) {
return getOperationRequestInfo(request[originalRequestSymbol]);
}
let info = operationRequestMap.get(request);

if (!info) {
Expand Down
8 changes: 7 additions & 1 deletion sdk/core/core-http-compat/CHANGELOG.md
@@ -1,11 +1,17 @@
# Release History

## 1.3.1 (Unreleased)
## 2.0.0 (Unreleased)

### Features Added

- Added support for `clone` on `WebResourceLike`s.
- Added support for using a `core-http` style HttpClient as a `core-rest-pipeline` HttpClient called `convertHttpClient`.
- Expose helper for converting `HttpHeaders` from `core-rest-pipeline` into the same shape as `core-http`.

### Breaking Changes

- renamed property `disbaleKeepAlivePolicyName` to `disableKeepAlivePolicyName`

### Bugs Fixed

### Other Changes
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-http-compat/package.json
@@ -1,6 +1,6 @@
{
"name": "@azure/core-http-compat",
"version": "1.3.1",
"version": "2.0.0",
"description": "Core HTTP Compatibility Library to bridge the gap between Core V1 & V2 packages.",
"sdk-type": "client",
"main": "dist/index.js",
Expand Down
13 changes: 12 additions & 1 deletion sdk/core/core-http-compat/review/core-http-compat.api.md
Expand Up @@ -7,6 +7,8 @@
import { AbortSignalLike } from '@azure/abort-controller';
import { CommonClientOptions } from '@azure/core-client';
import { FullOperationResponse } from '@azure/core-client';
import { HttpClient } from '@azure/core-rest-pipeline';
import { HttpHeaders } from '@azure/core-rest-pipeline';
import { HttpMethods } from '@azure/core-rest-pipeline';
import { OperationArguments } from '@azure/core-client';
import { OperationSpec } from '@azure/core-client';
Expand All @@ -21,11 +23,14 @@ export interface CompatResponse extends Omit<FullOperationResponse, "request" |
request: WebResourceLike;
}

// @public
export function convertHttpClient(requestPolicyClient: RequestPolicy): HttpClient;

// @public
export function createRequestPolicyFactoryPolicy(factories: RequestPolicyFactory[]): PipelinePolicy;

// @public (undocumented)
export const disbaleKeepAlivePolicyName = "DisableKeepAlivePolicy";
export const disableKeepAlivePolicyName = "DisableKeepAlivePolicy";

// @public
export interface ExtendedClientOptions {
Expand Down Expand Up @@ -118,6 +123,9 @@ export interface RequestPolicyOptionsLike {
shouldLog(logLevel: HttpPipelineLogLevel): boolean;
}

// @public
export function toHttpHeadersLike(headers: HttpHeaders): HttpHeadersLike;

// @public
export type TransferProgressEvent = {
loadedBytes: number;
Expand All @@ -127,13 +135,15 @@ export type TransferProgressEvent = {
export interface WebResourceLike {
abortSignal?: AbortSignalLike;
body?: any;
clone(): WebResourceLike;
decompressResponse?: boolean;
formData?: any;
headers: HttpHeadersLike;
keepAlive?: boolean;
method: HttpMethods;
onDownloadProgress?: (progress: TransferProgressEvent) => void;
onUploadProgress?: (progress: TransferProgressEvent) => void;
prepare(options: unknown): WebResourceLike;
proxySettings?: ProxySettings;
query?: {
[key: string]: any;
Expand All @@ -144,6 +154,7 @@ export interface WebResourceLike {
streamResponseStatusCodes?: Set<number>;
timeout: number;
url: string;
validateRequestProperties(): void;
withCredentials: boolean;
}

Expand Down
23 changes: 23 additions & 0 deletions sdk/core/core-http-compat/src/httpClientAdapter.ts
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HttpClient, PipelineRequest, PipelineResponse } from "@azure/core-rest-pipeline";
import { RequestPolicy } from "./policies/requestPolicyFactoryPolicy";
import { toPipelineResponse } from "./response";
import { toWebResourceLike } from "./util";

/**
* Converts a RequestPolicy based HttpClient to a PipelineRequest based HttpClient.
* @param requestPolicyClient - A HttpClient compatible with core-http
* @returns A HttpClient compatible with core-rest-pipeline
*/
export function convertHttpClient(requestPolicyClient: RequestPolicy): HttpClient {
return {
sendRequest: async (request: PipelineRequest): Promise<PipelineResponse> => {
const response = await requestPolicyClient.sendRequest(
toWebResourceLike(request, { createProxy: true })
);
return toPipelineResponse(response);
},
};
}
4 changes: 3 additions & 1 deletion sdk/core/core-http-compat/src/index.ts
Expand Up @@ -23,11 +23,13 @@ export {
} from "./policies/requestPolicyFactoryPolicy";
export { KeepAliveOptions } from "./policies/keepAliveOptions";
export { RedirectOptions } from "./policies/redirectOptions";
export { disbaleKeepAlivePolicyName } from "./policies/disableKeepAlivePolicy";
export { disableKeepAlivePolicyName } from "./policies/disableKeepAlivePolicy";
export { convertHttpClient } from "./httpClientAdapter";
export {
WebResourceLike,
HttpHeadersLike,
RawHttpHeaders,
HttpHeader,
TransferProgressEvent,
toHttpHeadersLike,
} from "./util";
Expand Up @@ -8,11 +8,11 @@ import {
SendRequest,
} from "@azure/core-rest-pipeline";

export const disbaleKeepAlivePolicyName = "DisableKeepAlivePolicy";
export const disableKeepAlivePolicyName = "DisableKeepAlivePolicy";

export function createDisableKeepAlivePolicy(): PipelinePolicy {
return {
name: disbaleKeepAlivePolicyName,
name: disableKeepAlivePolicyName,
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
request.disableKeepAlive = true;
return next(request);
Expand Down
6 changes: 4 additions & 2 deletions sdk/core/core-http-compat/src/response.ts
Expand Up @@ -6,7 +6,7 @@ import { PipelineResponse, createHttpHeaders } from "@azure/core-rest-pipeline";
import {
HttpHeadersLike,
WebResourceLike,
toHttpHeaderLike,
toHttpHeadersLike,
toPipelineRequest,
toWebResourceLike,
} from "./util";
Expand Down Expand Up @@ -37,14 +37,16 @@ export function toCompatResponse(
options?: { createProxy?: boolean }
): CompatResponse {
let request = toWebResourceLike(response.request);
let headers = toHttpHeaderLike(response.headers);
let headers = toHttpHeadersLike(response.headers);
if (options?.createProxy) {
return new Proxy(response, {
get(target, prop, receiver) {
if (prop === "headers") {
return headers;
} else if (prop === "request") {
return request;
} else if (prop === originalResponse) {
return response;
}
return Reflect.get(target, prop, receiver);
},
Expand Down

0 comments on commit 637732a

Please sign in to comment.