-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core-client-rest] Handle Binary and FormData in Rest Clients (#18753)
This PR adds better handling for Binary and FormData for RestClients the main changes are: - Move injecting api-version qsp to a pipeline policy to be more robust - Logic to identify binary response content based on well known content-types - Add a request option to explicitly tell core-client-rest to handle response a binary regardless of the content-type. This is useful for non-common content-types. - Pack binary responses into Uint8Array - Allow users providing Binary content for request body as Uint8Array or string - Support form-data using the same body property. Internally we detect `content-type: multipart/form-data` and pass it to core-rest-pipeline as `request.formData`
- Loading branch information
Showing
20 changed files
with
803 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { PipelinePolicy } from "@azure/core-rest-pipeline"; | ||
import { ClientOptions } from "./common"; | ||
import { URL } from "./url"; | ||
|
||
export const apiVersionPolicyName = "ApiVersionPolicy"; | ||
|
||
/** | ||
* Creates a policy that sets the apiVersion as a query parameter on every request | ||
* @param options - Client options | ||
* @returns Pipeline policy that sets the apiVersion as a query parameter on every request | ||
*/ | ||
export function apiVersionPolicy(options: ClientOptions): PipelinePolicy { | ||
return { | ||
name: apiVersionPolicyName, | ||
sendRequest: (req, next) => { | ||
if (options.apiVersion) { | ||
const url = new URL(req.url); | ||
url.searchParams.append("api-version", options.apiVersion); | ||
req.url = url.toString(); | ||
} | ||
|
||
return next(req); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
/** | ||
* Converts a string representing binary content into a Uint8Array | ||
*/ | ||
export function stringToBinaryArray(content: string): Uint8Array { | ||
const arr = new Uint8Array(content.length); | ||
for (let i = 0; i < content.length; i++) { | ||
arr[i] = content.charCodeAt(i); | ||
} | ||
|
||
return arr; | ||
} | ||
|
||
/** | ||
* Converts binary content to its string representation | ||
*/ | ||
export function binaryArrayToString(content: Uint8Array): string { | ||
let decodedBody = ""; | ||
for (const element of content) { | ||
decodedBody += String.fromCharCode(element); | ||
} | ||
|
||
return decodedBody; | ||
} |
Oops, something went wrong.