Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
b4031ec
Updated dependencies
annatisch Aug 27, 2019
9f30dd6
Added core pipeline
annatisch Aug 27, 2019
d2ec4b3
Ignore test config
annatisch Aug 27, 2019
81d0a62
Merge remote-tracking branch 'upstream/master' into cosmos-pipeline
annatisch Aug 27, 2019
d7394d7
Fixed indexes test
annatisch Aug 27, 2019
d628d91
Refactored request creation
annatisch Aug 28, 2019
8c091d8
Fixed index test
annatisch Aug 28, 2019
abc9030
Added trace decorators
annatisch Aug 28, 2019
f02ca0f
Bumped version
annatisch Aug 28, 2019
c9ae867
Updated policies
annatisch Aug 28, 2019
095f6f8
Renamed request_options -> request_params
annatisch Aug 28, 2019
6b88446
Merge remote-tracking branch 'upstream/master' into cosmos-pipeline
annatisch Aug 28, 2019
872cb26
Renamed clients
annatisch Aug 29, 2019
85ee07d
Updated with azure-core errors
annatisch Aug 30, 2019
0aacc0b
Fixed test warnings
annatisch Aug 30, 2019
943595c
Updated config
annatisch Aug 30, 2019
7e54f58
Merge remote-tracking branch 'upstream/feature/cosmos-preview2' into …
annatisch Aug 30, 2019
ff3628a
PR fixes
annatisch Aug 30, 2019
1986464
Fixed init user import
annatisch Aug 30, 2019
6366f02
Fixed init clients
annatisch Aug 30, 2019
01bb2b4
Started revising constructors
annatisch Sep 3, 2019
50f9b12
Test conn str constructor
annatisch Sep 3, 2019
8441a6c
Update iterables with core paging
annatisch Sep 3, 2019
ce6c622
Added context managers
annatisch Sep 3, 2019
398d9dd
Reverted storage changes
annatisch Sep 3, 2019
dab0703
Updated constructor
annatisch Sep 4, 2019
be457d6
Mypy and Pylint
annatisch Sep 4, 2019
44f88af
Renamed all listing operations
annatisch Sep 4, 2019
c1053ee
Some mypy fixes
annatisch Sep 4, 2019
44fb172
Cleaned up method signatures
annatisch Sep 4, 2019
5d1ad18
Fix pylint
annatisch Sep 4, 2019
70b5ee6
Propagate kwargs
annatisch Sep 5, 2019
ba45e57
Fix pylint
annatisch Sep 5, 2019
338209c
Some mypy fixes
annatisch Sep 5, 2019
e6a427b
Updated readme and release notes
annatisch Sep 5, 2019
404dae0
Fix for passing in extra headers
annatisch Sep 5, 2019
11509db
Reverted credentials
annatisch Sep 5, 2019
3f7a7d0
Review feedback
annatisch Sep 5, 2019
191eb6f
Fix pylint
annatisch Sep 5, 2019
90b0064
Fixed samples
annatisch Sep 6, 2019
cfa1b8c
Updated docstrings
annatisch Sep 6, 2019
9f3ea2f
Fixed whitespace and imports
annatisch Sep 6, 2019
4fad699
Some mypy fixes
annatisch Sep 6, 2019
34246ba
Mypy fixes
annatisch Sep 6, 2019
56c341e
Removed continuation token support
annatisch Sep 6, 2019
d36e04b
Pylint fix
annatisch Sep 6, 2019
f74ec89
Docs tweaks
annatisch Sep 9, 2019
0ba76ab
Updated continuation token
annatisch Sep 9, 2019
da5f0f9
Updated response header
annatisch Sep 9, 2019
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
42 changes: 42 additions & 0 deletions sdk/cosmos/azure-cosmos/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
# Change Log azure-cosmos

## Version 4.0.0b2:

Version 4.0.0b2 is the second iteration in our efforts to build a more Pythonic client library.

**Breaking changes**

- The client connection has been adapted to consume the HTTP pipeline defined in `azure.core.pipeline`.
- Interactive objects have now been renamed as proxies. This includes:
- `Database` -> `DatabaseProxy`
- `User` -> `UserProxy`
- `Container` -> `ContainerProxy`
- `Scripts` -> `ScriptsProxy`
- The constructor of `CosmosClient` has been updated:
- The `auth` parameter has been renamed to `credential` and will now take an authentication type directly. This means the master key value, a dictionary of resource tokens, or a list of permissions can be passed in. However the old dictionary format is still supported.
- The `connection_policy` parameter has been made a keyword only parameter, and while it is still supported, each of the individual attributes of the policy can now be passed in as explicit keyword arguments:
- `request_timeout`
- `media_request_timeout`
- `connection_mode`
- `media_read_mode`
- `proxy_config`
- `enable_endpoint_discovery`
- `preferred_locations`
- `multiple_write_locations`
- A new classmethod constructor has been added to `CosmosClient` to enable creation via a connection string retrieved from the Azure portal.
- Some `read_all` operations have been renamed to `list` operations:
- `CosmosClient.read_all_databases` -> `CosmosClient.list_databases`
- `Container.read_all_conflicts` -> `ContainerProxy.list_conflicts`
- `Database.read_all_containers` -> `DatabaseProxy.list_containers`
- `Database.read_all_users` -> `DatabaseProxy.list_users`
- `User.read_all_permissions` -> `UserProxy.list_permissions`
- All operations that take `request_options` or `feed_options` parameters, these have been moved to keyword only parameters. In addition, while these options dictionaries are still supported, each of the individual options within the dictionary are now supported as explicit keyword arguments.
- The error heirarchy is now inherited from `azure.core.AzureError` instead of `CosmosError` which has been removed.
- `HTTPFailure` has been renamed to `CosmosHttpResponseError`
- `JSONParseFailure` has been removed and replaced by `azure.core.DecodeError`
- Added additional errors for specific response codes:
- `CosmosResourceNotFoundError` for status 404
- `CosmosResourceExistsError` for status 409
- `CosmosAccessConditionFailedError` for status 412
- `CosmosClient` can now be run in a context manager to handle closing the client connection.
- Iterable responses (e.g. query responses and list responses) are now of type `azure.core.paging.ItemPaged`. The method `fetch_next_block` has been replaced by a secondary iterator, accessed by the `by_page` method.

## Version 4.0.0b1:

Version 4.0.0b1 is the first preview of our efforts to create a user-friendly and Pythonic client library for Azure Cosmos. For more information about this, and preview releases of other Azure SDK libraries, please visit https://aka.ms/azure-sdk-preview1-python.
Expand Down
40 changes: 17 additions & 23 deletions sdk/cosmos/azure-cosmos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,21 @@ export ACCOUNT_KEY=$(az cosmosdb list-keys --resource-group $RES_GROUP --name $A
Once you've populated the `ACCOUNT_URI` and `ACCOUNT_KEY` environment variables, you can create the [CosmosClient][ref_cosmosclient].

```Python
from azure.cosmos import HTTPFailure, CosmosClient, Container, Database, PartitionKey
from azure.cosmos import CosmosClient, Container, Database, PartitionKey, errors

import os
url = os.environ['ACCOUNT_URI']
key = os.environ['ACCOUNT_KEY']
client = CosmosClient(url, auth = {
'masterKey': key
})
client = CosmosClient(url, credential=key)
```

## Usage

Once you've initialized a [CosmosClient][ref_cosmosclient], you can interact with the primary resource types in Cosmos DB:

* [Database][ref_database]: A Cosmos DB account can contain multiple databases. When you create a database, you specify the API you'd like to use when interacting with its documents: SQL, MongoDB, Gremlin, Cassandra, or Azure Table. Use the [Database][ref_database] object to manage its containers.
* [Database][ref_database]: A Cosmos DB account can contain multiple databases. When you create a database, you specify the API you'd like to use when interacting with its documents: SQL, MongoDB, Gremlin, Cassandra, or Azure Table. Use the [DatabaseProxy][ref_database] object to manage its containers.

* [Container][ref_container]: A container is a collection of JSON documents. You create (insert), read, update, and delete items in a container by using methods on the [Container][ref_container] object.
* [Container][ref_container]: A container is a collection of JSON documents. You create (insert), read, update, and delete items in a container by using methods on the [ContainerProxy][ref_container] object.

* [Item][ref_item]: An Item is the dictionary-like representation of a JSON document stored in a container. Each Item you add to a container must include an `id` key with a value that uniquely identifies the item within the container.

Expand All @@ -106,9 +104,7 @@ After authenticating your [CosmosClient][ref_cosmosclient], you can work with an
database_name = 'testDatabase'
try:
database = client.create_database(database_name)
except HTTPFailure as e:
if e.status_code != 409:
raise
except errors.CosmosResourceExistsError:
database = client.get_database_client(database_name)
```

Expand All @@ -120,13 +116,13 @@ This example creates a container with default settings. If a container with the
container_name = 'products'
try:
container = database.create_container(id=container_name, partition_key=PartitionKey(path="/productName"))
except HTTPFailure as e:
if e.status_code != 409:
raise
except errors.CosmosResourceExistsError:
container = database.get_container_client(container_name)
except errors.CosmosHttpResponseError:
raise
```

The preceding snippet also handles the [HTTPFailure][ref_httpfailure] exception if the container creation failed. For more information on error handling and troubleshooting, see the [Troubleshooting](#troubleshooting) section.
The preceding snippet also handles the [CosmosHttpResponseError][ref_httpfailure] exception if the container creation failed. For more information on error handling and troubleshooting, see the [Troubleshooting](#troubleshooting) section.

### Get an existing container

Expand All @@ -139,7 +135,7 @@ container = database.get_container_client(container_name)

### Insert data

To insert items into a container, pass a dictionary containing your data to [Container.upsert_item][ref_container_upsert_item]. Each item you add to a container must include an `id` key with a value that uniquely identifies the item within the container.
To insert items into a container, pass a dictionary containing your data to [ContainerProxy.upsert_item][ref_container_upsert_item]. Each item you add to a container must include an `id` key with a value that uniquely identifies the item within the container.

This example inserts several items into the container, each with a unique `id`:

Expand All @@ -158,7 +154,7 @@ for i in range(1, 10):

### Delete data

To delete items from a container, use [Container.delete_item][ref_container_delete_item]. The SQL API in Cosmos DB does not support the SQL `DELETE` statement.
To delete items from a container, use [ContainerProxy.delete_item][ref_container_delete_item]. The SQL API in Cosmos DB does not support the SQL `DELETE` statement.

```Python
for item in container.query_items(query='SELECT * FROM products p WHERE p.productModel = "DISCONTINUED"',
Expand All @@ -168,7 +164,7 @@ for item in container.query_items(query='SELECT * FROM products p WHERE p.produc

### Query the database

A Cosmos DB SQL API database supports querying the items in a container with [Container.query_items][ref_container_query_items] using SQL-like syntax.
A Cosmos DB SQL API database supports querying the items in a container with [ContainerProxy.query_items][ref_container_query_items] using SQL-like syntax.

This example queries a container for items with a specific `id`:

Expand All @@ -186,7 +182,7 @@ for item in container.query_items(

> NOTE: Although you can specify any value for the container name in the `FROM` clause, we recommend you use the container name for consistency.

Perform parameterized queries by passing a dictionary containing the parameters and their values to [Container.query_items][ref_container_query_items]:
Perform parameterized queries by passing a dictionary containing the parameters and their values to [ContainerProxy.query_items][ref_container_query_items]:

```Python
discontinued_items = container.query_items(
Expand Down Expand Up @@ -243,13 +239,11 @@ For example, if you try to create a container using an ID (name) that's already
```Python
try:
database.create_container(id=container_name, partition_key=PartitionKey(path="/productName")
except HTTPFailure as e:
if e.status_code == 409:
print("""Error creating container.
except errors.CosmosResourceExistsError:
print("""Error creating container
HTTP status code 409: The ID (name) provided for the container is already in use.
The container name must be unique within the database.""")
else:
raise

```

## More sample code
Expand Down Expand Up @@ -285,7 +279,7 @@ For more extensive documentation on the Cosmos DB service, see the [Azure Cosmos
[ref_cosmosclient_create_database]: http://cosmosproto.westus.azurecontainer.io/#azure.cosmos.CosmosClient.create_database
[ref_cosmosclient]: http://cosmosproto.westus.azurecontainer.io/#azure.cosmos.CosmosClient
[ref_database]: http://cosmosproto.westus.azurecontainer.io/#azure.cosmos.Database
[ref_httpfailure]: https://docs.microsoft.com/python/api/azure-cosmos/azure.cosmos.errors.httpfailure
[ref_httpfailure]: https://docs.microsoft.com/python/api/azure-cosmos/azure.cosmos.errors.CosmosHttpResponseError
[ref_item]: http://cosmosproto.westus.azurecontainer.io/#azure.cosmos.Item
[sample_database_mgmt]: https://github.com/binderjoe/cosmos-python-prototype/blob/master/examples/databasemanagementsample.py
[sample_document_mgmt]: https://github.com/binderjoe/cosmos-python-prototype/blob/master/examples/documentmanagementsample.py
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmos/azure-cosmos/azure/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
18 changes: 9 additions & 9 deletions sdk/cosmos/azure-cosmos/azure/cosmos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from .container import Container
from .container import ContainerProxy
from .cosmos_client import CosmosClient
from .database import Database
from .database import DatabaseProxy
from .user import UserProxy
from .scripts import ScriptsProxy
from .documents import (
ConsistencyLevel,
DataType,
Expand All @@ -35,18 +37,16 @@
)
from .partition_key import PartitionKey
from .permission import Permission
from .scripts import Scripts
from .user import User
from .version import VERSION

__all__ = (
"Container",
"CosmosClient",
"Database",
"DatabaseProxy",
"ContainerProxy",
"PartitionKey",
"Permission",
"Scripts",
"User",
"ScriptsProxy",
"UserProxy",
"ConsistencyLevel",
"DataType",
"IndexKind",
Expand All @@ -57,4 +57,4 @@
"TriggerOperation",
"TriggerType",
)
__version__ = VERSION
__version__ = VERSION
40 changes: 40 additions & 0 deletions sdk/cosmos/azure-cosmos/azure/cosmos/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import json
import uuid
import binascii
from typing import Dict, Any

import six
from six.moves.urllib.parse import quote as urllib_quote
Expand All @@ -39,6 +40,45 @@

# pylint: disable=protected-access

_COMMON_OPTIONS = {
'initial_headers': 'initialHeaders',
'pre_trigger_include': 'preTriggerInclude',
'post_trigger_include': 'postTriggerInclude',
'max_item_count': 'maxItemCount',
'access_condition': 'accessCondition',
'indexing_directive': 'indexingDirective',
'consistency_level': 'consistencyLevel',
'session_token': 'sessionToken',
'enable_scan_in_query': 'enableScanInQuery',
'resource_token_expiry_seconds': 'resourceTokenExpirySeconds',
'offer_type': 'offerType',
'offer_throughput': 'offerThroughput',
'partition_key': 'partitionKey',
'enable_cross_partition_query': 'enableCrossPartitionQuery',
'populate_query_metrics': 'populateQueryMetrics',
'enable_script_logging': 'enableScriptLogging',
'offer_enable_ru_per_minute_throughput': 'offerEnableRUPerMinuteThroughput',
'disable_ru_per_minute_usage': 'disableRUPerMinuteUsage',
'change_feed': 'changeFeed',
'continuation': 'continuation',
'is_start_from_beginning': 'isStartFromBeginning',
'populate_partition_key_range_statistics': 'populatePartitionKeyRangeStatistics',
'populate_quota_info': 'populateQuotaInfo'
}

def build_options(kwargs):
# type: (Dict[str, Any]) -> Dict[str, Any]
options = kwargs.pop('request_options', kwargs.pop('feed_options', {}))
for key, value in _COMMON_OPTIONS.items():
if key in kwargs:
options[value] = kwargs.pop(key)

if 'if_match' in kwargs:
options['accessCondition'] = {'type': 'IfMatch', 'condition': kwargs.pop('if_match')}
if 'if_none_match' in kwargs:
options['accessCondition'] = {'type': 'IfNoneMatch', 'condition': kwargs.pop('if_none_match')}
return options


def GetHeaders( # pylint: disable=too-many-statements,too-many-branches
cosmos_client_connection,
Expand Down
Loading