msrest
msrest is the Python client runtime library for AutoRest-generated Python SDKs, primarily used within the Azure SDK ecosystem. It provides functionalities for serialization, deserialization, and HTTP communication, enabling Python clients to interact with REST APIs defined by Swagger/OpenAPI specifications. The current version is 0.7.1, with the latest release being June 2022. The library is now deprecated and will not receive further updates, with its functionalities moved to `azure-identity` and `azure-core`, or vendored directly into SDKs.
Warnings
- deprecated The `msrest` library is officially deprecated and no longer receives updates. Its functionalities have been moved to `azure-identity` for authentication and `azure-core` or are vendored directly into newer Azure SDKs. It will soon be archived.
- breaking In `msrest` version 0.7.0, `SerializationError` and `DeserializationError` classes were moved from `msrest.exceptions` to `azure.core.exceptions`.
- gotcha DateTime objects, especially timezone-naive ones, can cause serialization or deserialization issues. `msrest` expects RFC1123 or ISO 8601 formatted strings and prefers timezone-aware datetime objects for serialization.
- gotcha Mixing different versions of Azure SDKs that have varying `msrest` dependencies can lead to `pip` dependency resolution conflicts. Some newer SDKs have removed `msrest` as a direct dependency or require specific versions.
Install
-
pip install msrest
Imports
- ServiceClient
from msrest import ServiceClient
- Serializer
from msrest.serialization import Serializer
- Deserializer
from msrest.serialization import Deserializer
- HttpOperationError
from msrest.exceptions import HttpOperationError
- SerializationError
from azure.core.exceptions import SerializationError
- DeserializationError
from azure.core.exceptions import DeserializationError
Quickstart
import json
import datetime
import isodate # Needed for deserialized datetime object type checking
from msrest.serialization import Serializer, Deserializer
# Define a simple model schema for demonstration
models = {
'MyModel': {
'type': 'object',
'properties': {
'id': {'type': 'integer', 'format': 'int32'},
'name': {'type': 'string'},
'created_at': {'type': 'string', 'format': 'date-time'}
},
'required': ['id', 'name']
}
}
# Initialize Serializer and Deserializer with defined models
serializer = Serializer(models)
deserializer = Deserializer(models)
# Create a Python object to be serialized
class MyModel:
def __init__(self, id, name, created_at=None):
self.id = id
self.name = name
self.created_at = created_at
instance_to_serialize = MyModel(
id=1,
name='Example Item',
created_at=datetime.datetime.now(datetime.timezone.utc)
)
# Serialize the object to a format suitable for a REST API call
# The 'MyModel' string refers to the schema key defined in 'models'
serialized_data = serializer.body(instance_to_serialize, 'MyModel')
print("Serialized data:", json.dumps(serialized_data, indent=2))
# Simulate a raw JSON response from a REST API
raw_json_response = {
"id": 2,
"name": "Another Example",
"created_at": "2023-10-27T10:30:00Z"
}
# Deserialize the raw JSON response into a Python object
deserialized_instance = deserializer(MyModel, raw_json_response)
print(f"\nDeserialized name: {deserialized_instance.name}")
print(f"Deserialized created_at: {deserialized_instance.created_at}")
print(f"Type of deserialized created_at: {type(deserialized_instance.created_at)}")