Skip to content

Commit

Permalink
Merge pull request #53 from pieces-app/feat-3.1.0
Browse files Browse the repository at this point in the history
Feat 3.1.0
  • Loading branch information
bishoy-at-pieces authored Sep 3, 2024
2 parents fa8a28c + 3af5c13 commit 5ee1588
Show file tree
Hide file tree
Showing 20 changed files with 720 additions and 237 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
pip install poetry -U
poetry install
- name: Run the tests
run: pytest --doctest-modules --junitxml=junit/test-results.xml --cov=com --cov-report=xml --cov-report=html

- name: Build and publish
if: startsWith(github.ref, 'refs/tags/v')
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pieces_os_client"
version = "3.0.1"
version = "3.1.0"
description = "A powerful code engine package for writing applications on top of Pieces OS"
authors = ["Pieces <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/pieces_os_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
""" # noqa: E501


__version__ = "3.0.1"
__version__ = "3.1.0"

# import apis into sdk package
from pieces_os_client.api.activities_api import ActivitiesApi
Expand Down
2 changes: 1 addition & 1 deletion src/pieces_os_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
self.default_headers[header_name] = header_value
self.cookie = cookie
# Set default User-Agent.
self.user_agent = 'OpenAPI-Generator/3.0.1/python'
self.user_agent = 'OpenAPI-Generator/3.1.0/python'
self.client_side_validation = configuration.client_side_validation

def __enter__(self):
Expand Down
2 changes: 1 addition & 1 deletion src/pieces_os_client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def to_debug_report(self):
"OS: {env}\n"\
"Python Version: {pyversion}\n"\
"Version of the API: 1.0\n"\
"SDK Package Version: 3.0.1".\
"SDK Package Version: 3.1.0".\
format(env=sys.platform, pyversion=sys.version)

def get_host_settings(self):
Expand Down
3 changes: 2 additions & 1 deletion src/pieces_os_client/wrapper/basic_identifier/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .asset import BasicAsset
from .chat import BasicChat
from .message import BasicMessage
from .user import BasicUser

__all__ = ["BasicAsset","BasicChat","BasicMessage"]
__all__ = ["BasicAsset","BasicChat","BasicMessage","BasicUser"]
59 changes: 55 additions & 4 deletions src/pieces_os_client/wrapper/basic_identifier/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
SeededFragment,
TransferableString,
FragmentMetadata,
AssetReclassification)
AssetReclassification,
Linkify,
Shares
)

from typing import Optional
from .basic import Basic
from .user import BasicUser

# Friendly wrapper (to avoid interacting with the pieces_os_client sdks models)

Expand Down Expand Up @@ -202,22 +206,46 @@ def delete(self):
AssetSnapshot.pieces_client.assets_api.assets_delete_asset(self.id)

@classmethod
def create(cls,raw: str, metadata: Optional[FragmentMetadata] = None) -> str:
def create(cls,raw_content: str, metadata: Optional[FragmentMetadata] = None) -> str:
"""
Create a new asset.
Args:
raw (str): The raw content of the asset.
raw_content (str): The raw content of the asset.
metadata (Optional[FragmentMetadata]): The metadata of the asset.
Returns:
str: The ID of the created asset.
"""
seed = cls._get_seed(raw,metadata)
seed = cls._get_seed(raw_content,metadata)

created_asset_id = AssetSnapshot.pieces_client.assets_api.assets_create_new_asset(transferables=False, seed=seed).id
return created_asset_id

def share(self) -> Shares:
"""
Generates a shareable link for the given asset.
Raises:
PermissionError: If the user is not logged in or is not connected to the cloud.
"""
return self._share(self.asset)


@classmethod
def share_raw_content(cls,raw_content:str) -> Shares:
"""
Generates a shareable link for the given user raw content.
Note: this will create an asset
Args:
raw_content (str): The raw content of the asset that will be shared.
Raises:
PermissionError: If the user is not logged in or is not connected to the cloud.
"""
return cls._share(seed = cls._get_seed(raw_content))

@staticmethod
def _get_seed(raw: str, metadata: Optional[FragmentMetadata] = None) -> Seed:
return Seed(
Expand Down Expand Up @@ -283,4 +311,27 @@ def _ocr_from_format(src: Optional[Format]) -> Optional[str]:
def _edit_asset(asset):
AssetSnapshot.pieces_client.asset_api.asset_update(False,asset)

@staticmethod
def _share(asset=None,seed=None):
"""
You need to either give the seed or the asset_id
"""
if asset:
kwargs = {"asset" : asset}
else:
kwargs = {"seed" : seed}

user = BasicUser.user_profile

if not user:
raise PermissionError("You need to be logged in to generate a shareable link")

if not user.allocation:
raise PermissionError("You need to connect to the cloud to generate a shareable link")

return AssetSnapshot.pieces_client.linkfy_api.linkify(
linkify=Linkify(
access="PUBLIC",
**kwargs
)
)
144 changes: 144 additions & 0 deletions src/pieces_os_client/wrapper/basic_identifier/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import threading
from pieces_os_client import UserProfile,AllocationStatusEnum
from typing import Optional

## TODO: Modify the Basic class to be able to fit in the BasicUser
class BasicUser:
"""
A class to represent a basic user and manage their connection to the cloud.
Attributes:
user_profile: The profile of the user.
pieces_client: The client used to interact with the pieces OS API.
"""

user_profile: Optional[UserProfile] = None

def __init__(self, pieces_client) -> None:
"""
Initializes the BasicUser with a pieces client.
Args:
pieces_client: The client used to interact with the pieces OS API.
"""
self.pieces_client = pieces_client

def on_user_callback(self, user: Optional[UserProfile], connecting=False):
"""
Callback function to set the user profile.
Args:
user: The profile of the user.
connecting: A flag indicating if the user is connecting to the cloud (default is False).
"""
self.user_profile = user

def _on_login_connect(self, thread, timeout):
"""
Waits for the user to login and then connects to the cloud.
Args:
thread: The thread handling the login process.
timeout: The maximum time to wait for the login process.
"""
thread.get(timeout) # Wait for the user to login
self.connect()

def login(self, connect_after_login=True, timeout=120):
"""
Logs the user into the OS and optionally connects to the cloud.
Args:
connect_after_login: A flag indicating if the user should connect to the cloud after login (default is True).
timeout: The maximum time to wait for the login process (default is 120 seconds).
"""
thread = self.pieces_client.os_api.sign_into_os(async_req=True)
if connect_after_login:
threading.Thread(target=lambda: self._on_login_connect(thread, timeout))

def logout(self):
"""
Logs the user out of the OS.
"""
self.pieces_client.api_client.os_api.sign_out_of_os()

def connect(self):
"""
Connects the user to the cloud.
Raises:
PermissionError: If the user is not logged in.
"""
if not self.user_profile:
raise PermissionError("You must be logged in to use this feature")
self.on_user_callback(self.user_profile, True) # Set the connecting to cloud bool to true
self.pieces_client.allocations_api.allocations_connect_new_cloud(self.user_profile)

def disconnect(self):
"""
Disconnects the user from the cloud.
Raises:
PermissionError: If the user is not logged in.
"""
if not self.user_profile:
raise PermissionError("You must be logged in to use this feature")
if self.user_profile.allocation: # Check if there is an allocation iterable
self.pieces_client.api_client.allocations_api.allocations_disconnect_cloud(self.user_profile.allocation)

@property
def picture(self) -> Optional[str]:
"""
Returns the user's profile picture URL.
Returns:
The URL of the user's profile picture, or None if not available.
"""
if self.user_profile:
return self.user_profile.picture


@property
def name(self) -> Optional[str]:
"""
Returns the name of the user.
Returns:
Optional[str]: The name of the user if the user logged in, otherwise None.
"""
if self.user_profile:
return self.user_profile.name

@property
def email(self) -> Optional[str]:
"""
Returns the email of the user.
Returns:
Optional[str]: The email of the user if the user logged in, otherwise None.
"""
if self.user_profile:
return self.user_profile.email

@property
def vanity_name(self) -> Optional[str]: # TODO: implements the setter object
"""
Returns the vanity name of the user which is the base name of the cloud url.
For example, if the cloud URL is 'bishoyatpieces.pieces.cloud', this method returns 'bishoyatpieces'.
Returns:
Optional[str]: The vanity name of the user if the user user logged in and connected to the cloud, otherwise None.
"""
if self.user_profile:
return self.user_profile.vanityname

@property
def cloud_status(self) -> Optional[AllocationStatusEnum]:
"""
Returns the cloud status of the user's cloud.
Returns:
Optional[AllocationStatusEnum]: The cloud status of the user's cloud.
"""
if self.user_profile and self.user_profile.allocation:
return self.user_profile.allocation.status.cloud
Loading

0 comments on commit 5ee1588

Please sign in to comment.