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
2 changes: 1 addition & 1 deletion src/citrine/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.24.1"
__version__ = "3.25.0"
55 changes: 53 additions & 2 deletions src/citrine/resources/project.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Resources that represent both individual and collections of projects."""
from deprecation import deprecated
from functools import partial
from typing import Optional, Dict, List, Union, Iterable, Tuple, Iterator
from uuid import UUID
from warnings import warn
Expand Down Expand Up @@ -77,6 +78,8 @@ class Project(Resource['Project']):
"""str: Status of the project."""
created_at = properties.Optional(properties.Datetime(), 'created_at')
"""int: Time the project was created, in seconds since epoch."""
archived = properties.Optional(properties.Boolean, 'archived')
"""bool: Whether the project is archived."""
_team_id = properties.Optional(properties.UUID, "team.id", serializable=False)

def __init__(self,
Expand Down Expand Up @@ -599,9 +602,57 @@ def register(self, name: str, *, description: Optional[str] = None) -> Project:
project = Project(name, description=description)
return super().register(project)

def _list_base(self, *, per_page: int = 1000, archived: Optional[bool] = None):
filters = {}
if archived is not None:
filters["archived"] = str(archived).lower()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both requests and our testing library just call str on a bool, leaving it capitalized. And it seems the accounts service doesn't recognize that as a valid boolean.

For the record, I confirmed that at least Orion paths can handle either. So I'm slightly inclined to add a snippet in Session that converts any bool query parameters to lower-cased strings automatically. But I've left that for the future, when we can more thoroughly ensure there are no ill effects elsewhere in the system.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll approve, but I'd rather see json used to encode this, since the problem (I believe) is that the JSON standard uses lower case false and stringified Python uses title case. This works, but is less intuitive about why.


fetcher = partial(self._fetch_page, additional_params=filters, version=self._api_version)
return self._paginator.paginate(page_fetcher=fetcher,
collection_builder=self._build_collection_elements,
per_page=per_page)

def list(self, *, per_page: int = 1000) -> Iterator[Project]:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that ProjectCollection.list defaults to listing every project. This differs from many other collections, such as predictors an design spaces, where list only lists the active assets. This will be addressed in 4.0 by renaming PredictorCollection.list to list_active and PredictorCollection.list_all to list.

"""
List projects using pagination.
List all projects using pagination.

Parameters
---------
per_page: int, optional
Max number of results to return per page. Default is 1000. This parameter
is used when making requests to the backend service. If the page parameter
is specified it limits the maximum number of elements in the response.

Returns
-------
Iterator[Project]
Projects in this collection.

"""
return self._list_base(per_page=per_page)

def list_active(self, *, per_page: int = 1000) -> Iterator[Project]:
"""
List non-archived projects using pagination.

Parameters
---------
per_page: int, optional
Max number of results to return per page. Default is 1000. This parameter
is used when making requests to the backend service. If the page parameter
is specified it limits the maximum number of elements in the response.

Returns
-------
Iterator[Project]
Projects in this collection.

"""
return self._list_base(per_page=per_page, archived=False)

def list_archived(self, *, per_page: int = 1000) -> Iterable[Project]:
"""
List archived projects using pagination.

Parameters
---------
Expand All @@ -616,7 +667,7 @@ def list(self, *, per_page: int = 1000) -> Iterator[Project]:
Projects in this collection.

"""
return super().list(per_page=per_page)
return self._list_base(per_page=per_page, archived=True)

def search_all(self, search_params: Optional[Dict]) -> Iterable[Dict]:
"""
Expand Down
41 changes: 40 additions & 1 deletion tests/resources/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,46 @@ def test_list_projects(collection, session):

# Then
assert 1 == session.num_calls
expected_call = FakeCall(method='GET', path=f'/teams/{collection.team_id}/projects', params={'per_page': 1000, 'page': 1})
expected_call = FakeCall(method='GET',
path=f'/teams/{collection.team_id}/projects',
params={'per_page': 1000, 'page': 1},
version="v3")
assert expected_call == session.last_call
assert 5 == len(projects)


def test_list_archived_projects(collection, session):
# Given
projects_data = ProjectDataFactory.create_batch(5)
session.set_response({'projects': projects_data})

# When
projects = list(collection.list_archived())

# Then
assert 1 == session.num_calls
expected_call = FakeCall(method='GET',
path=f'/teams/{collection.team_id}/projects',
params={'per_page': 1000, 'page': 1, 'archived': "true"},
version="v3")
assert expected_call == session.last_call
assert 5 == len(projects)


def test_list_active_projects(collection, session):
# Given
projects_data = ProjectDataFactory.create_batch(5)
session.set_response({'projects': projects_data})

# When
projects = list(collection.list_active())

# Then
assert 1 == session.num_calls
expected_call = FakeCall(method='GET',
path=f'/teams/{collection.team_id}/projects',
params={'per_page': 1000, 'page': 1, 'archived': "false"},
version="v3")
assert expected_call == session.last_call
assert 5 == len(projects)

Expand Down
Loading