Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
"myfile",
"nazsdk",
"nbytes",
"nbsp",
Comment thread
annatisch marked this conversation as resolved.
"noarch",
"NOPERM",
"northcentralus",
Expand Down Expand Up @@ -262,8 +263,10 @@
"prevsnapshot",
"pschema",
"PSECRET",
"pyfetch",
"pyfuncitem",
"pygobject",
"pyodide",
"parameterizing",
"pytyped",
"pytz",
Expand Down
1 change: 1 addition & 0 deletions eng/.docsettings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ known_content_issues:
- ['sdk/textanalytics/azure-ai-textanalytics/swagger/README.md', '#4554']
- ['sdk/appconfiguration/azure-appconfiguration/swagger/README.md', '#4554']
- ['sdk/core/azure-core/tests/testserver_tests/coretestserver/README.md', '#4554']
- ['sdk/core/azure-core-experimental/README.md', '#4554']
package_indexing_exclusion_list:
- 'azure-sdk-tools'
- 'azure-template'
1 change: 1 addition & 0 deletions eng/ignore-links.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ https://docs.microsoft.com/samples/azure-samples/azure-samples-python-management
https://pypi.org/project/azure-messaging-webpubsubservice/
https://github.com/Azure/azure-rest-api-specs-pr
https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/communication/azure-communication-rooms/samples
http://localhost:8000/samples/pyodide_integration
5 changes: 4 additions & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ method-name-hint=lower_case_with_underscores
function-name-hint=lower_case_with_underscores
argument-name-hint=lower_case_with_underscores
variable-name-hint=lower_case_with_underscores
inlinevar-name-hint=lower_case_with_underscores (short is OK)
inlinevar-name-hint=lower_case_with_underscores (short is OK)

[TYPECHECK]
generated-members=js.*
9 changes: 9 additions & 0 deletions sdk/core/azure-core-experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Release History

## 1.0.0b1 (2022-10-06)

Initial release

### Features Added

- pyodide implementation of azure-core transport protocol
Comment thread
xiangyan99 marked this conversation as resolved.
21 changes: 21 additions & 0 deletions sdk/core/azure-core-experimental/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (c) Microsoft Corporation.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions sdk/core/azure-core-experimental/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
recursive-include tests *.py
include *.md
include LICENSE
include azure/__init__.py
include azure/core/__init__.py
include azure/core/experimental/__init__.py
include azure/core/experimental/transport/*.py
recursive-include samples *.py
include azure/core/experimental/py.typed
24 changes: 24 additions & 0 deletions sdk/core/azure-core-experimental/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# Azure Core Experimental client library for Python

Azure Core Experimental client library provides experimental features that are not ready for stable release. The main reason might be because the feature depends on some other packages that there is no stable version available yet. If its dependant package introduces breaking changes, the features maybe break. It is not recommended to use azure core experimental library in production environment.

## Getting started

Install the azure core experimental package with [pip](https://pypi.org/project/pip/):

```bash
pip install azure-core-experimental
```

## Features list

- pyodide transport

## Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
1 change: 1 addition & 0 deletions sdk/core/azure-core-experimental/azure/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore
1 change: 1 addition & 0 deletions sdk/core/azure-core-experimental/azure/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------

from ._version import VERSION
Comment thread
annatisch marked this conversation as resolved.
__version__ = VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

VERSION = "1.0.0b1"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
import sys

# pylint: disable=undefined-all-variable

if sys.version_info >= (3, 7):
__all__ = [
'PyodideTransport',
]

def __dir__():
return __all__

def __getattr__(name):
if name == 'PyodideTransport':
try:
from ._pyodide import PyodideTransport
return PyodideTransport
except ImportError:
raise ImportError("pyodide package is not installed")
raise AttributeError(f"module 'azure.core.experimental.transport' has no attribute {name}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------

from collections.abc import AsyncIterator
from io import BytesIO

import js # pylint: disable=import-error
from pyodide import JsException # pylint: disable=import-error
from pyodide.http import pyfetch # pylint: disable=import-error

from azure.core.exceptions import HttpResponseError
from azure.core.pipeline import Pipeline
from azure.core.utils import CaseInsensitiveDict

from azure.core.rest._http_response_impl_async import AsyncHttpResponseImpl
from azure.core.pipeline.transport import HttpRequest, AsyncioRequestsTransport


class PyodideTransportResponse(AsyncHttpResponseImpl):
"""Async response object for the `PyodideTransport`."""


def _js_stream(self):
"""So we get a fresh stream every time."""
return self._internal_response.clone().js_response.body

async def close(self) -> None:
"""We don't actually have control over closing connections in the browser, so we just pretend
to close.
"""
self._is_closed = True

def body(self) -> bytes:
"""The body is just the content."""
return self.content

async def load_body(self) -> None:
"""Backcompat"""
if self._content is None:
self._content = await self._internal_response.clone().bytes()

class PyodideStreamDownloadGenerator(AsyncIterator):
"""Simple stream download generator that returns the contents of
a request.
"""

# pylint: disable=unused-argument
def __init__(self, pipeline: Pipeline, response: PyodideTransportResponse, *_, **kwargs):
self._block_size = response._block_size
self.response = response
# use this to efficiently store bytes.
if kwargs.pop("decompress", False) and self.response.headers.get("enc", None) in ("gzip", "deflate"):
self._js_reader = response._js_stream().pipeThrough(js.DecompressionStream.new("gzip")).getReader()
else:
self._js_reader = response._js_stream().getReader()
self._stream = BytesIO()

self._closed = False
# We cannot control how many bytes we get from `response.reader`. `self._buffer_left`
# indicates how many unread bytes there are in `self.stream`
self._buffer_left = 0
self.done = False

async def __anext__(self) -> bytes:
"""Get the next block of bytes."""
if self._closed:
raise StopAsyncIteration()

# remember the initial stream position
start_pos = self._stream.tell()
# move stream position to the end
self._stream.read()
# read from reader until there is no more data or we have `self._block_size` unread bytes.
while self._buffer_left < self._block_size:
read = await self._js_reader.read()
if read.done:
self._closed = True
break
self._buffer_left += self._stream.write(bytes(read.value))

# move the stream position back to where we started
self._stream.seek(start_pos)
self._buffer_left -= self._block_size
return self._stream.read(self._block_size)

class PyodideTransport(AsyncioRequestsTransport):
"""**This object is experimental**, meaning it may be changed in a future release
or might break with a future Pyodide release. This transport was built with Pyodide
version 0.20.0.

Implements a basic HTTP sender using the Pyodide Javascript Fetch API.
"""

async def send(self, request: HttpRequest, **kwargs) -> PyodideTransportResponse:
"""Send request object according to configuration.

:param request: The request object to be sent.
:type request: ~azure.core.pipeline.transport.HttpRequest
:return: An HTTPResponse object.
:rtype: PyodideResponseTransport
"""
stream_response = kwargs.pop("stream_response", False)
endpoint = request.url
request_headers = dict(request.headers)
init = {
"method": request.method,
"headers": request_headers,
"body": request.data,
"files": request.files,
"verify": kwargs.pop("connection_verify", self.connection_config.verify),
"cert": kwargs.pop("connection_cert", self.connection_config.cert),
"allow_redirects": False,
**kwargs,
}

try:
response = await pyfetch(endpoint, **init)
except JsException as error:
raise HttpResponseError(error, error=error)

headers = CaseInsensitiveDict(response.js_response.headers)
transport_response = PyodideTransportResponse(
request=request,
internal_response=response,
block_size=self.connection_config.data_block_size,
status_code=response.status,
reason=response.status_text,
content_type=headers.get("content-type"),
headers=headers,
stream_download_generator=PyodideStreamDownloadGenerator,
)
if not stream_response:
await transport_response.load_body()

return transport_response
5 changes: 5 additions & 0 deletions sdk/core/azure-core-experimental/dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-e ../../../tools/azure-sdk-tools
../azure-core
-e ../../../tools/azure-devtools
js
pyodide==0.19.0a1; python_version >= '3.8'
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import traceback
import sys


class AsyncTestSuite:
"""Async test cases
Test must be asynchronous and follow the pattern `test*`.
"""

async def run(self):
"""Run the tests an print the results."""
print("".join(("-" * 8, type(self).__name__, "-" * 8)))
for method_name in dir(self):
if not method_name.startswith("test"):
continue
print(method_name, end="... ")
try:
await getattr(self, method_name)()
except AssertionError:
print("FAIL")
traceback.print_exception(*sys.exc_info())
except Exception: # pylint: disable=broad-except
print("ERROR")
traceback.print_exception(*sys.exc_info())
else:
print("PASS")
print()
Loading