Skip to content

Commit

Permalink
Make calls to API endpoints in beta thread safe (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonvt authored Sep 7, 2023
1 parent cc64e59 commit 4973470
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 184 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [6.21.1] - 2023-09-07
### Fixed
- Concurrent usage of the `CogniteClient` could result in API calls being made with the wrong value for `api_subversion`.

## [6.21.0] - 2023-09-06
### Added
- Supporting pattern mode and extra configuration for diagram detect in beta.
Expand Down
145 changes: 56 additions & 89 deletions cognite/client/_api/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,12 @@ def aggregate_count(
"""
self._validate_filter(advanced_filter)

api_version = self._api_subversion
try:
self._api_subversion = "beta"
return self._advanced_aggregate(
"count",
filter=filter,
advanced_filter=advanced_filter,
)
finally:
self._api_subversion = api_version
return self._advanced_aggregate(
"count",
filter=filter,
advanced_filter=advanced_filter,
api_subversion="beta",
)

def aggregate_cardinality_values(
self,
Expand Down Expand Up @@ -303,20 +298,14 @@ def aggregate_cardinality_values(
... aggregate_filter=not_america)
"""
self._validate_filter(advanced_filter)

api_version = self._api_subversion

try:
self._api_subversion = "beta"
return self._advanced_aggregate(
"cardinalityValues",
properties=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
)
finally:
self._api_subversion = api_version
return self._advanced_aggregate(
"cardinalityValues",
properties=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
api_subversion="beta",
)

def aggregate_cardinality_properties(
self,
Expand Down Expand Up @@ -346,19 +335,14 @@ def aggregate_cardinality_properties(
>>> count = c.sequences.aggregate_cardinality_values(SequenceProperty.metadata)
"""
self._validate_filter(advanced_filter)
api_version = self._api_subversion

try:
self._api_subversion = "beta"
return self._advanced_aggregate(
"cardinalityProperties",
path=path,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
)
finally:
self._api_subversion = api_version
return self._advanced_aggregate(
"cardinalityProperties",
path=path,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
api_subversion="beta",
)

def aggregate_unique_values(
self,
Expand Down Expand Up @@ -413,29 +397,23 @@ def aggregate_unique_values(
>>> print(result.unique)
"""
self._validate_filter(advanced_filter)

api_version = self._api_subversion

try:
self._api_subversion = "beta"
if property == ["metadata"] or property is SequenceProperty.metadata:
return self._advanced_aggregate(
aggregate="uniqueProperties",
path=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
)
else:
return self._advanced_aggregate(
aggregate="uniqueValues",
properties=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
)
finally:
self._api_subversion = api_version
if property == ["metadata"] or property is SequenceProperty.metadata:
return self._advanced_aggregate(
aggregate="uniqueProperties",
path=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
api_subversion="beta",
)
return self._advanced_aggregate(
aggregate="uniqueValues",
properties=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
api_subversion="beta",
)

def aggregate_unique_properties(
self,
Expand Down Expand Up @@ -465,20 +443,14 @@ def aggregate_unique_properties(
>>> result = c.sequences.aggregate_unique_properties(SequenceProperty.metadata)
"""
self._validate_filter(advanced_filter)

api_version = self._api_subversion

try:
self._api_subversion = "beta"
return self._advanced_aggregate(
aggregate="uniqueProperties",
path=path,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
)
finally:
self._api_subversion = api_version
return self._advanced_aggregate(
aggregate="uniqueProperties",
path=path,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
api_subversion="beta",
)

@overload
def create(self, sequence: Sequence) -> Sequence:
Expand Down Expand Up @@ -800,20 +772,15 @@ def filter(
elif not isinstance(sort, list):
sort = [sort]

api_version = self._api_subversion

try:
self._api_subversion = "beta"
return self._list(
list_cls=SequenceList,
resource_cls=Sequence,
method="POST",
limit=limit,
advanced_filter=filter.dump(camel_case=True) if isinstance(filter, Filter) else filter,
sort=[SequenceSort.load(item).dump(camel_case=True) for item in sort],
)
finally:
self._api_subversion = api_version
return self._list(
list_cls=SequenceList,
resource_cls=Sequence,
method="POST",
limit=limit,
advanced_filter=filter.dump(camel_case=True) if isinstance(filter, Filter) else filter,
sort=[SequenceSort.load(item).dump(camel_case=True) for item in sort],
api_subversion="beta",
)

def _validate_filter(self, filter: Filter | dict | None) -> None:
_validate_filter(filter, _FILTERS_SUPPORTED, type(self).__name__)
Expand Down
128 changes: 47 additions & 81 deletions cognite/client/_api/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,12 @@ def aggregate_count(
"""
self._validate_filter(advanced_filter)

api_version = self._api_subversion
try:
self._api_subversion = "beta"
return self._advanced_aggregate(
"count",
filter=filter,
advanced_filter=advanced_filter,
)
finally:
self._api_subversion = api_version
return self._advanced_aggregate(
"count",
filter=filter,
advanced_filter=advanced_filter,
api_subversion="beta",
)

def aggregate_cardinality_values(
self,
Expand Down Expand Up @@ -315,20 +310,14 @@ def aggregate_cardinality_values(
"""
self._validate_filter(advanced_filter)

api_version = self._api_subversion

try:
self._api_subversion = "beta"
return self._advanced_aggregate(
"cardinalityValues",
properties=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
)
finally:
self._api_subversion = api_version
return self._advanced_aggregate(
"cardinalityValues",
properties=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
api_subversion="beta",
)

def aggregate_cardinality_properties(
self,
Expand Down Expand Up @@ -357,20 +346,14 @@ def aggregate_cardinality_properties(
>>> key_count = c.time_series.aggregate_cardinality_properties(TimeSeriesProperty.metadata)
"""
self._validate_filter(advanced_filter)

api_version = self._api_subversion

try:
self._api_subversion = "beta"
return self._advanced_aggregate(
"cardinalityProperties",
path=path,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
)
finally:
self._api_subversion = api_version
return self._advanced_aggregate(
"cardinalityProperties",
path=path,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
api_subversion="beta",
)

def aggregate_unique_values(
self,
Expand Down Expand Up @@ -425,20 +408,14 @@ def aggregate_unique_values(
>>> print(result.unique)
"""
self._validate_filter(advanced_filter)

api_version = self._api_subversion

try:
self._api_subversion = "beta"
return self._advanced_aggregate(
aggregate="uniqueValues",
properties=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
)
finally:
self._api_subversion = api_version
return self._advanced_aggregate(
aggregate="uniqueValues",
properties=property,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
api_subversion="beta",
)

def aggregate_unique_properties(
self,
Expand Down Expand Up @@ -468,20 +445,14 @@ def aggregate_unique_properties(
>>> result = c.time_series.aggregate_unique_values(TimeSeriesProperty.metadata)
"""
self._validate_filter(advanced_filter)

api_version = self._api_subversion

try:
self._api_subversion = "beta"
return self._advanced_aggregate(
aggregate="uniqueProperties",
path=path,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
)
finally:
self._api_subversion = api_version
return self._advanced_aggregate(
aggregate="uniqueProperties",
path=path,
filter=filter,
advanced_filter=advanced_filter,
aggregate_filter=aggregate_filter,
api_subversion="beta",
)

@overload
def create(self, time_series: Sequence[TimeSeries]) -> TimeSeriesList:
Expand Down Expand Up @@ -717,20 +688,15 @@ def filter(
elif not isinstance(sort, list):
sort = [sort]

api_version = self._api_subversion

try:
self._api_subversion = "beta"
return self._list(
list_cls=TimeSeriesList,
resource_cls=TimeSeries,
method="POST",
limit=limit,
advanced_filter=filter.dump(camel_case=True) if isinstance(filter, Filter) else filter,
sort=[TimeSeriesSort.load(item).dump(camel_case=True) for item in sort],
)
finally:
self._api_subversion = api_version
return self._list(
list_cls=TimeSeriesList,
resource_cls=TimeSeries,
method="POST",
limit=limit,
advanced_filter=filter.dump(camel_case=True) if isinstance(filter, Filter) else filter,
sort=[TimeSeriesSort.load(item).dump(camel_case=True) for item in sort],
api_subversion="beta",
)

def _validate_filter(self, filter: Filter | dict | None) -> None:
_validate_filter(filter, _FILTERS_SUPPORTED, type(self).__name__)
Expand Down
Loading

0 comments on commit 4973470

Please sign in to comment.