Compare commits
88 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cca7b18119 | |||
| 8a34b95128 | |||
| 46d7abbaee | |||
| 67e95caf5f | |||
| 04532ed645 | |||
| 7df175d9bb | |||
| 3912fa8860 | |||
| 8a91260887 | |||
| 88eea00787 | |||
| c57d57c009 | |||
| 708e3329e3 | |||
| f0e68845c0 | |||
| 434a4376b3 | |||
| d701bedcc7 | |||
| 6238150bd5 | |||
| 3e41e8cd8e | |||
| 3962126b54 | |||
| c99c25e848 | |||
| 1ef9b91e82 | |||
| c0cfe1471a | |||
| da838280c3 | |||
| 681872e5ff | |||
| e11c41e0f8 | |||
| ece957fb0f | |||
| 5338d8ac0f | |||
| e36ea70e8a | |||
| edf2afaa89 | |||
| e0b1b272c0 | |||
| 682e82057e | |||
| 473e5cfddb | |||
| 03cd989165 | |||
| 284d841a1e | |||
| 668fc5131f | |||
| 64926bd41d | |||
| 1c9b186ea5 | |||
| ed9f1ad818 | |||
| b83c30a1c9 | |||
| c079342a55 | |||
| 6aa29d9b30 | |||
| f456e4ddbb | |||
| dcd13224d0 | |||
| 06952a5991 | |||
| 6049049813 | |||
| 75b7d30bd6 | |||
| 19e26318fc | |||
| 798dc7ff6a | |||
| 0e4cff5904 | |||
| d1502c9072 | |||
| 869629e2a3 | |||
| 48b98294fb | |||
| 8205180e5d | |||
| a2fd21f541 | |||
| 08ac76cf09 | |||
| fbf19420fa | |||
| 44336addaf | |||
| 43c9a9cace | |||
| 99e9f773b8 | |||
| 189a5847cf | |||
| eb86b4881a | |||
| 64fca5f280 | |||
| 784e9c1326 | |||
| c7f5e0718b | |||
| d2685c5cf5 | |||
| 1b83e5a84b | |||
| 77e09b9780 | |||
| 402f750200 | |||
| a43e7471a4 | |||
| a4ed7ebb08 | |||
| e7eb7c67a9 | |||
| d547cdaac0 | |||
| 6f35c1bd20 | |||
| 420c73f484 | |||
| c2859475cc | |||
| 56e8d65e2b | |||
| 7885a6be8d | |||
| b19b85c9d1 | |||
| db4b2b7f87 | |||
| 77916995bc | |||
| 3dd56dc38e | |||
| ae42bec1c3 | |||
| ea7baf8eb5 | |||
| 8352bb5c9a | |||
| fc34b876fd | |||
| 183993cfc5 | |||
| 9be3b4b93d | |||
| 0b14660115 | |||
| 68c4c682a0 | |||
| 4f93ddcaf3 |
+23
-33
@@ -6,48 +6,38 @@ orbs:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
docker:
|
||||
- image: "cimg/python:<<parameters.tag>>"
|
||||
- image: "cimg/node:16.15"
|
||||
- image: "cimg/redis:6.2"
|
||||
- image: "cimg/postgres:14.2"
|
||||
environment:
|
||||
POSTGRES_DB: speckle2_test
|
||||
POSTGRES_PASSWORD: speckle
|
||||
POSTGRES_USER: speckle
|
||||
- image: "speckle/speckle-server"
|
||||
command: ["bash", "-c", "/wait && node bin/www"]
|
||||
environment:
|
||||
POSTGRES_URL: "127.0.0.1"
|
||||
POSTGRES_USER: "speckle"
|
||||
POSTGRES_PASSWORD: "speckle"
|
||||
POSTGRES_DB: "speckle2_test"
|
||||
REDIS_URL: "redis://127.0.0.1"
|
||||
SESSION_SECRET: "keyboard cat"
|
||||
STRATEGY_LOCAL: "true"
|
||||
CANONICAL_URL: "http://localhost:3000"
|
||||
WAIT_HOSTS: 127.0.0.1:5432, 127.0.0.1:6379
|
||||
DISABLE_FILE_UPLOADS: "true"
|
||||
machine:
|
||||
image: ubuntu-2204:2023.02.1
|
||||
docker_layer_caching: true
|
||||
resource_class: medium
|
||||
parameters:
|
||||
tag:
|
||||
default: "3.8"
|
||||
default: "3.11"
|
||||
type: string
|
||||
steps:
|
||||
- checkout
|
||||
- run: python --version
|
||||
- run:
|
||||
command: python -m pip install --upgrade pip
|
||||
name: upgrade pip
|
||||
- python/install-packages:
|
||||
pkg-manager: poetry
|
||||
- run: poetry run pytest --cov --cov-report xml:reports/coverage.xml --junitxml=reports/test-results.xml
|
||||
|
||||
name: Install python
|
||||
command: |
|
||||
pyenv install -s << parameters.tag >>
|
||||
pyenv global << parameters.tag >>
|
||||
- run:
|
||||
name: Startup the Speckle Server
|
||||
command: docker compose -f docker-compose.yml up -d
|
||||
- run:
|
||||
name: Install Poetry
|
||||
command: |
|
||||
pip install poetry
|
||||
- run:
|
||||
name: Install packages
|
||||
command: poetry install
|
||||
- run:
|
||||
name: Run tests
|
||||
command: poetry run pytest --cov --cov-report xml:reports/coverage.xml --junitxml=reports/test-results.xml
|
||||
- store_test_results:
|
||||
path: reports
|
||||
|
||||
- store_artifacts:
|
||||
path: reports
|
||||
|
||||
- codecov/upload
|
||||
|
||||
deploy:
|
||||
@@ -65,7 +55,7 @@ workflows:
|
||||
- test:
|
||||
matrix:
|
||||
parameters:
|
||||
tag: ["3.7", "3.8", "3.9", "3.10", "3.11"]
|
||||
tag: ["3.11"]
|
||||
filters:
|
||||
tags:
|
||||
only: /.*/
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
name: Update issue Status
|
||||
|
||||
on:
|
||||
issues:
|
||||
types: [closed]
|
||||
|
||||
jobs:
|
||||
update_issue:
|
||||
uses: specklesystems/github-actions/.github/workflows/project-add-issue.yml@main
|
||||
secrets: inherit
|
||||
with:
|
||||
issue-id: ${{ github.event.issue.node_id }}
|
||||
@@ -1,12 +0,0 @@
|
||||
name: Move new issues into Project
|
||||
|
||||
on:
|
||||
issues:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
track_issue:
|
||||
uses: specklesystems/github-actions/.github/workflows/project-add-issue.yml@main
|
||||
secrets: inherit
|
||||
with:
|
||||
issue-id: ${{ github.event.issue.node_id }}
|
||||
Vendored
+3
-2
@@ -5,6 +5,7 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
|
||||
|
||||
{
|
||||
"name": "Python: Current File",
|
||||
"type": "python",
|
||||
@@ -17,8 +18,8 @@
|
||||
"name": "Pytest",
|
||||
"type": "python",
|
||||
"request": "launch",
|
||||
"program": "poetry",
|
||||
"args": ["run", "pytest"],
|
||||
"program": "pytest",
|
||||
"args": [],
|
||||
"console": "integratedTerminal",
|
||||
"justMyCode": true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
version: "3.9"
|
||||
name: "speckle-server"
|
||||
|
||||
services:
|
||||
####
|
||||
# Speckle Server dependencies
|
||||
#######
|
||||
postgres:
|
||||
image: "postgres:14.5-alpine"
|
||||
restart: always
|
||||
environment:
|
||||
POSTGRES_DB: speckle
|
||||
POSTGRES_USER: speckle
|
||||
POSTGRES_PASSWORD: speckle
|
||||
volumes:
|
||||
- postgres-data:/var/lib/postgresql/data/
|
||||
healthcheck:
|
||||
# the -U user has to match the POSTGRES_USER value
|
||||
test: ["CMD-SHELL", "pg_isready -U speckle"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 30
|
||||
|
||||
redis:
|
||||
image: "redis:6.0-alpine"
|
||||
restart: always
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 30
|
||||
|
||||
minio:
|
||||
image: "minio/minio"
|
||||
command: server /data --console-address ":9001"
|
||||
restart: always
|
||||
volumes:
|
||||
- minio-data:/data
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD-SHELL",
|
||||
"curl -s -o /dev/null http://127.0.0.1:9000/minio/index.html",
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 30s
|
||||
retries: 30
|
||||
start_period: 10s
|
||||
|
||||
####
|
||||
# Speckle Server
|
||||
#######
|
||||
speckle-frontend:
|
||||
image: speckle/speckle-frontend:2
|
||||
restart: always
|
||||
ports:
|
||||
- "0.0.0.0:8080:8080"
|
||||
environment:
|
||||
FILE_SIZE_LIMIT_MB: 100
|
||||
|
||||
speckle-server:
|
||||
image: speckle/speckle-server:2
|
||||
restart: always
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD",
|
||||
"node",
|
||||
"-e",
|
||||
"require('node:http').request({headers: {'Content-Type': 'application/json'}, port:3000, hostname:'127.0.0.1', path:'/graphql?query={serverInfo{version}}', method: 'GET' }, (res) => { body = ''; res.on('data', (chunk) => {body += chunk;}); res.on('end', () => {process.exit(res.statusCode != 200 || body.toLowerCase().includes('error'));}); }).end();",
|
||||
]
|
||||
interval: 10s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
ports:
|
||||
- "0.0.0.0:3000:3000"
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
minio:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
# TODO: Change this to the URL of the speckle server, as accessed from the network
|
||||
CANONICAL_URL: "http://127.0.0.1:8080"
|
||||
SPECKLE_AUTOMATE_URL: "http://127.0.0.1:3030"
|
||||
|
||||
# TODO: Change thvolumes:
|
||||
REDIS_URL: "redis://redis"
|
||||
|
||||
S3_ENDPOINT: "http://minio:9000"
|
||||
S3_ACCESS_KEY: "minioadmin"
|
||||
S3_SECRET_KEY: "minioadmin"
|
||||
S3_BUCKET: "speckle-server"
|
||||
S3_CREATE_BUCKET: "true"
|
||||
|
||||
FILE_SIZE_LIMIT_MB: 100
|
||||
|
||||
# TODO: Change this to a unique secret for this server
|
||||
SESSION_SECRET: "TODO:ReplaceWithLongString"
|
||||
|
||||
STRATEGY_LOCAL: "true"
|
||||
DEBUG: "speckle:*"
|
||||
|
||||
POSTGRES_URL: "postgres"
|
||||
POSTGRES_USER: "speckle"
|
||||
POSTGRES_PASSWORD: "speckle"
|
||||
POSTGRES_DB: "speckle"
|
||||
ENABLE_MP: "false"
|
||||
|
||||
preview-service:
|
||||
image: speckle/speckle-preview-service:2
|
||||
restart: always
|
||||
depends_on:
|
||||
speckle-server:
|
||||
condition: service_healthy
|
||||
mem_limit: "1000m"
|
||||
memswap_limit: "1000m"
|
||||
environment:
|
||||
DEBUG: "preview-service:*"
|
||||
PG_CONNECTION_STRING: "postgres://speckle:speckle@postgres/speckle"
|
||||
|
||||
webhook-service:
|
||||
image: speckle/speckle-webhook-service:2
|
||||
restart: always
|
||||
depends_on:
|
||||
speckle-server:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
DEBUG: "webhook-service:*"
|
||||
PG_CONNECTION_STRING: "postgres://speckle:speckle@postgres/speckle"
|
||||
WAIT_HOSTS: postgres:5432
|
||||
|
||||
fileimport-service:
|
||||
image: speckle/speckle-fileimport-service:2
|
||||
restart: always
|
||||
depends_on:
|
||||
speckle-server:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
DEBUG: "fileimport-service:*"
|
||||
PG_CONNECTION_STRING: "postgres://speckle:speckle@postgres/speckle"
|
||||
WAIT_HOSTS: postgres:5432
|
||||
|
||||
S3_ENDPOINT: "http://minio:9000"
|
||||
S3_ACCESS_KEY: "minioadmin"
|
||||
S3_SECRET_KEY: "minioadmin"
|
||||
S3_BUCKET: "speckle-server"
|
||||
|
||||
SPECKLE_SERVER_URL: "http://speckle-server:3000"
|
||||
|
||||
networks:
|
||||
default:
|
||||
name: speckle-server
|
||||
|
||||
volumes:
|
||||
postgres-data:
|
||||
redis-data:
|
||||
minio-data:
|
||||
Generated
+651
-601
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -15,7 +15,7 @@ packages = [
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.7.2, <4.0"
|
||||
pydantic = "^1.9"
|
||||
pydantic = "^2.0"
|
||||
appdirs = "^1.4.4"
|
||||
gql = {extras = ["requests", "websockets"], version = "^3.3.0"}
|
||||
ujson = "^5.3.0"
|
||||
|
||||
+50
-126
@@ -4,12 +4,14 @@ from warnings import warn
|
||||
|
||||
from deprecated import deprecated
|
||||
from gql import Client
|
||||
from gql.transport.exceptions import TransportServerError
|
||||
from gql.transport.requests import RequestsHTTPTransport
|
||||
from gql.transport.websockets import WebsocketsTransport
|
||||
|
||||
from specklepy.api import resources
|
||||
from specklepy.api.credentials import Account, get_account_from_token
|
||||
from specklepy.api.resources import (
|
||||
user,
|
||||
active_user,
|
||||
branch,
|
||||
commit,
|
||||
@@ -18,13 +20,14 @@ from specklepy.api.resources import (
|
||||
server,
|
||||
stream,
|
||||
subscriptions,
|
||||
user,
|
||||
)
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
||||
|
||||
from specklepy.core.api.client import SpeckleClient as CoreSpeckleClient
|
||||
|
||||
class SpeckleClient:
|
||||
|
||||
class SpeckleClient(CoreSpeckleClient):
|
||||
"""
|
||||
The `SpeckleClient` is your entry point for interacting with
|
||||
your Speckle Server's GraphQL API.
|
||||
@@ -59,122 +62,12 @@ class SpeckleClient:
|
||||
USE_SSL = True
|
||||
|
||||
def __init__(self, host: str = DEFAULT_HOST, use_ssl: bool = USE_SSL) -> None:
|
||||
metrics.track(metrics.CLIENT, custom_props={"name": "create"})
|
||||
ws_protocol = "ws"
|
||||
http_protocol = "http"
|
||||
|
||||
if use_ssl:
|
||||
ws_protocol = "wss"
|
||||
http_protocol = "https"
|
||||
|
||||
# sanitise host input by removing protocol and trailing slash
|
||||
host = re.sub(r"((^\w+:|^)\/\/)|(\/$)", "", host)
|
||||
|
||||
self.url = f"{http_protocol}://{host}"
|
||||
self.graphql = f"{self.url}/graphql"
|
||||
self.ws_url = f"{ws_protocol}://{host}/graphql"
|
||||
super().__init__(
|
||||
host=host,
|
||||
use_ssl=use_ssl,
|
||||
)
|
||||
self.account = Account()
|
||||
|
||||
self.httpclient = Client(
|
||||
transport=RequestsHTTPTransport(url=self.graphql, verify=True, retries=3)
|
||||
)
|
||||
self.wsclient = None
|
||||
|
||||
self._init_resources()
|
||||
|
||||
# ? Check compatibility with the server - i think we can skip this at this point? save a request
|
||||
# try:
|
||||
# server_info = self.server.get()
|
||||
# if isinstance(server_info, Exception):
|
||||
# raise server_info
|
||||
# if not isinstance(server_info, ServerInfo):
|
||||
# raise Exception("Couldn't get ServerInfo")
|
||||
# except Exception as ex:
|
||||
# raise SpeckleException(
|
||||
# f"{self.url} is not a compatible Speckle Server", ex
|
||||
# ) from ex
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"SpeckleClient( server: {self.url}, authenticated:"
|
||||
f" {self.account.token is not None} )"
|
||||
)
|
||||
|
||||
@deprecated(
|
||||
version="2.6.0",
|
||||
reason=(
|
||||
"Renamed: please use `authenticate_with_account` or"
|
||||
" `authenticate_with_token` instead."
|
||||
),
|
||||
)
|
||||
def authenticate(self, token: str) -> None:
|
||||
"""Authenticate the client using a personal access token
|
||||
The token is saved in the client object and a synchronous GraphQL
|
||||
entrypoint is created
|
||||
|
||||
Arguments:
|
||||
token {str} -- an api token
|
||||
"""
|
||||
self.authenticate_with_token(token)
|
||||
self._set_up_client()
|
||||
|
||||
def authenticate_with_token(self, token: str) -> None:
|
||||
"""
|
||||
Authenticate the client using a personal access token.
|
||||
The token is saved in the client object and a synchronous GraphQL
|
||||
entrypoint is created
|
||||
|
||||
Arguments:
|
||||
token {str} -- an api token
|
||||
"""
|
||||
self.account = get_account_from_token(token, self.url)
|
||||
metrics.track(metrics.CLIENT, self.account, {"name": "authenticate with token"})
|
||||
self._set_up_client()
|
||||
|
||||
def authenticate_with_account(self, account: Account) -> None:
|
||||
"""Authenticate the client using an Account object
|
||||
The account is saved in the client object and a synchronous GraphQL
|
||||
entrypoint is created
|
||||
|
||||
Arguments:
|
||||
account {Account} -- the account object which can be found with
|
||||
`get_default_account` or `get_local_accounts`
|
||||
"""
|
||||
metrics.track(metrics.CLIENT, account, {"name": "authenticate with account"})
|
||||
self.account = account
|
||||
self._set_up_client()
|
||||
|
||||
def _set_up_client(self) -> None:
|
||||
metrics.track(metrics.CLIENT, self.account, {"name": "set up client"})
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.account.token}",
|
||||
"Content-Type": "application/json",
|
||||
"apollographql-client-name": metrics.HOST_APP,
|
||||
"apollographql-client-version": metrics.HOST_APP_VERSION,
|
||||
}
|
||||
httptransport = RequestsHTTPTransport(
|
||||
url=self.graphql, headers=headers, verify=True, retries=3
|
||||
)
|
||||
wstransport = WebsocketsTransport(
|
||||
url=self.ws_url,
|
||||
init_payload={"Authorization": f"Bearer {self.account.token}"},
|
||||
)
|
||||
self.httpclient = Client(transport=httptransport)
|
||||
self.wsclient = Client(transport=wstransport)
|
||||
|
||||
self._init_resources()
|
||||
|
||||
if self.user.get() is None:
|
||||
warn(
|
||||
SpeckleWarning(
|
||||
"Possibly invalid token - could not authenticate Speckle Client"
|
||||
f" for server {self.url}"
|
||||
)
|
||||
)
|
||||
|
||||
def execute_query(self, query: str) -> Dict:
|
||||
return self.httpclient.execute(query)
|
||||
|
||||
def _init_resources(self) -> None:
|
||||
self.server = server.Resource(
|
||||
account=self.account, basepath=self.url, client=self.httpclient
|
||||
@@ -223,13 +116,44 @@ class SpeckleClient:
|
||||
client=self.wsclient,
|
||||
)
|
||||
|
||||
def __getattr__(self, name):
|
||||
try:
|
||||
attr = getattr(resources, name)
|
||||
return attr.Resource(
|
||||
account=self.account, basepath=self.url, client=self.httpclient
|
||||
)
|
||||
except AttributeError:
|
||||
raise SpeckleException(
|
||||
f"Method {name} is not supported by the SpeckleClient class"
|
||||
)
|
||||
@deprecated(
|
||||
version="2.6.0",
|
||||
reason=(
|
||||
"Renamed: please use `authenticate_with_account` or"
|
||||
" `authenticate_with_token` instead."
|
||||
),
|
||||
)
|
||||
def authenticate(self, token: str) -> None:
|
||||
"""Authenticate the client using a personal access token
|
||||
The token is saved in the client object and a synchronous GraphQL
|
||||
entrypoint is created
|
||||
|
||||
Arguments:
|
||||
token {str} -- an api token
|
||||
"""
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Client Authenticate_deprecated"})
|
||||
return super().authenticate(token)
|
||||
|
||||
def authenticate_with_token(self, token: str) -> None:
|
||||
"""
|
||||
Authenticate the client using a personal access token.
|
||||
The token is saved in the client object and a synchronous GraphQL
|
||||
entrypoint is created
|
||||
|
||||
Arguments:
|
||||
token {str} -- an api token
|
||||
"""
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Client Authenticate With Token"})
|
||||
return super().authenticate_with_token(token)
|
||||
|
||||
def authenticate_with_account(self, account: Account) -> None:
|
||||
"""Authenticate the client using an Account object
|
||||
The account is saved in the client object and a synchronous GraphQL
|
||||
entrypoint is created
|
||||
|
||||
Arguments:
|
||||
account {Account} -- the account object which can be found with
|
||||
`get_default_account` or `get_local_accounts`
|
||||
"""
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Client Authenticate With Account"})
|
||||
return super().authenticate_with_account(account)
|
||||
|
||||
@@ -9,36 +9,12 @@ from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
from specklepy.transports.sqlite import SQLiteTransport
|
||||
|
||||
|
||||
class UserInfo(BaseModel):
|
||||
name: Optional[str] = None
|
||||
email: Optional[str] = None
|
||||
company: Optional[str] = None
|
||||
id: Optional[str] = None
|
||||
|
||||
|
||||
class Account(BaseModel):
|
||||
isDefault: bool = False
|
||||
token: Optional[str] = None
|
||||
refreshToken: Optional[str] = None
|
||||
serverInfo: ServerInfo = Field(default_factory=ServerInfo)
|
||||
userInfo: UserInfo = Field(default_factory=UserInfo)
|
||||
id: Optional[str] = None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"Account(email: {self.userInfo.email}, server: {self.serverInfo.url},"
|
||||
f" isDefault: {self.isDefault})"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
@classmethod
|
||||
def from_token(cls, token: str, server_url: str = None):
|
||||
acct = cls(token=token)
|
||||
acct.serverInfo.url = server_url
|
||||
return acct
|
||||
# following imports seem to be unnecessary, but they need to stay
|
||||
# to not break the scripts using these functions as non-core
|
||||
from specklepy.core.api.credentials import (Account, UserInfo,
|
||||
StreamWrapper, # deprecated
|
||||
get_local_accounts as core_get_local_accounts,
|
||||
get_account_from_token as core_get_account_from_token)
|
||||
|
||||
|
||||
def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
|
||||
@@ -51,53 +27,19 @@ def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
|
||||
List[Account] -- list of all local accounts or an empty list if
|
||||
no accounts were found
|
||||
"""
|
||||
accounts: List[Account] = []
|
||||
try:
|
||||
account_storage = SQLiteTransport(scope="Accounts", base_path=base_path)
|
||||
res = account_storage.get_all_objects()
|
||||
account_storage.close()
|
||||
if res:
|
||||
accounts.extend(Account.parse_raw(r[1]) for r in res)
|
||||
except SpeckleException:
|
||||
# cannot open SQLiteTransport, probably because of the lack
|
||||
# of disk write permissions
|
||||
pass
|
||||
|
||||
json_acct_files = []
|
||||
json_path = str(speckle_path_provider.accounts_folder_path())
|
||||
try:
|
||||
os.makedirs(json_path, exist_ok=True)
|
||||
json_acct_files.extend(
|
||||
file for file in os.listdir(json_path) if file.endswith(".json")
|
||||
)
|
||||
|
||||
except Exception:
|
||||
# cannot find or get the json account paths
|
||||
pass
|
||||
|
||||
if json_acct_files:
|
||||
try:
|
||||
accounts.extend(
|
||||
Account.parse_file(os.path.join(json_path, json_file))
|
||||
for json_file in json_acct_files
|
||||
)
|
||||
except Exception as ex:
|
||||
raise SpeckleException(
|
||||
"Invalid json accounts could not be read. Please fix or remove them.",
|
||||
ex,
|
||||
) from ex
|
||||
accounts = core_get_local_accounts(base_path)
|
||||
|
||||
metrics.track(
|
||||
metrics.ACCOUNTS,
|
||||
metrics.SDK,
|
||||
next(
|
||||
(acc for acc in accounts if acc.isDefault),
|
||||
accounts[0] if accounts else None,
|
||||
),
|
||||
{"name": "Get Local Accounts"}
|
||||
)
|
||||
|
||||
return accounts
|
||||
|
||||
|
||||
def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
|
||||
"""
|
||||
Gets this environment's default account if any. If there is no default,
|
||||
@@ -108,7 +50,7 @@ def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
|
||||
Returns:
|
||||
Account -- the default account or None if no local accounts were found
|
||||
"""
|
||||
accounts = get_local_accounts(base_path=base_path)
|
||||
accounts = core_get_local_accounts(base_path=base_path)
|
||||
if not accounts:
|
||||
return None
|
||||
|
||||
@@ -119,8 +61,7 @@ def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
|
||||
metrics.initialise_tracker(default)
|
||||
|
||||
return default
|
||||
|
||||
|
||||
|
||||
def get_account_from_token(token: str, server_url: str = None) -> Account:
|
||||
"""Gets the local account for the token if it exists
|
||||
Arguments:
|
||||
@@ -130,31 +71,7 @@ def get_account_from_token(token: str, server_url: str = None) -> Account:
|
||||
Account -- the local account with this token or a shell account containing
|
||||
just the token and url if no local account is found
|
||||
"""
|
||||
accounts = get_local_accounts()
|
||||
if not accounts:
|
||||
return Account.from_token(token, server_url)
|
||||
account = core_get_account_from_token(token, server_url)
|
||||
|
||||
acct = next((acc for acc in accounts if acc.token == token), None)
|
||||
if acct:
|
||||
return acct
|
||||
|
||||
if server_url:
|
||||
url = server_url.lower()
|
||||
acct = next(
|
||||
(acc for acc in accounts if url in acc.serverInfo.url.lower()), None
|
||||
)
|
||||
if acct:
|
||||
return acct
|
||||
|
||||
return Account.from_token(token, server_url)
|
||||
|
||||
|
||||
class StreamWrapper:
|
||||
def __init__(self, url: str = None) -> None:
|
||||
raise SpeckleException(
|
||||
message=(
|
||||
"The StreamWrapper has moved as of v2.6.0! Please import from"
|
||||
" specklepy.api.wrapper"
|
||||
),
|
||||
exception=DeprecationWarning(),
|
||||
)
|
||||
metrics.track( metrics.SDK, account, {"name": "Get Account From Token"} )
|
||||
return account
|
||||
|
||||
@@ -2,115 +2,17 @@ from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from unicodedata import name
|
||||
|
||||
|
||||
class HostAppVersion(Enum):
|
||||
v = "v"
|
||||
v6 = "v6"
|
||||
v7 = "v7"
|
||||
v2019 = "v2019"
|
||||
v2020 = "v2020"
|
||||
v2021 = "v2021"
|
||||
v2022 = "v2022"
|
||||
v2023 = "v2023"
|
||||
v2024 = "v2024"
|
||||
v2025 = "v2025"
|
||||
vSandbox = "vSandbox"
|
||||
vRevit = "vRevit"
|
||||
vRevit2021 = "vRevit2021"
|
||||
vRevit2022 = "vRevit2022"
|
||||
vRevit2023 = "vRevit2023"
|
||||
vRevit2024 = "vRevit2024"
|
||||
vRevit2025 = "vRevit2025"
|
||||
v25 = "v25"
|
||||
v26 = "v26"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return self.value
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
@dataclass
|
||||
class HostApplication:
|
||||
name: str
|
||||
slug: str
|
||||
|
||||
def get_version(self, version: HostAppVersion) -> str:
|
||||
return f"{name.replace(' ', '')}{str(version).strip('v')}"
|
||||
|
||||
|
||||
RHINO = HostApplication("Rhino", "rhino")
|
||||
GRASSHOPPER = HostApplication("Grasshopper", "grasshopper")
|
||||
REVIT = HostApplication("Revit", "revit")
|
||||
DYNAMO = HostApplication("Dynamo", "dynamo")
|
||||
UNITY = HostApplication("Unity", "unity")
|
||||
GSA = HostApplication("GSA", "gsa")
|
||||
CIVIL = HostApplication("Civil 3D", "civil3d")
|
||||
AUTOCAD = HostApplication("AutoCAD", "autocad")
|
||||
MICROSTATION = HostApplication("MicroStation", "microstation")
|
||||
OPENROADS = HostApplication("OpenRoads", "openroads")
|
||||
OPENRAIL = HostApplication("OpenRail", "openrail")
|
||||
OPENBUILDINGS = HostApplication("OpenBuildings", "openbuildings")
|
||||
ETABS = HostApplication("ETABS", "etabs")
|
||||
SAP2000 = HostApplication("SAP2000", "sap2000")
|
||||
CSIBRIDGE = HostApplication("CSIBridge", "csibridge")
|
||||
SAFE = HostApplication("SAFE", "safe")
|
||||
TEKLASTRUCTURES = HostApplication("Tekla Structures", "teklastructures")
|
||||
DXF = HostApplication("DXF Converter", "dxf")
|
||||
EXCEL = HostApplication("Excel", "excel")
|
||||
UNREAL = HostApplication("Unreal", "unreal")
|
||||
POWERBI = HostApplication("Power BI", "powerbi")
|
||||
BLENDER = HostApplication("Blender", "blender")
|
||||
QGIS = HostApplication("QGIS", "qgis")
|
||||
ARCGIS = HostApplication("ArcGIS", "arcgis")
|
||||
SKETCHUP = HostApplication("SketchUp", "sketchup")
|
||||
ARCHICAD = HostApplication("Archicad", "archicad")
|
||||
TOPSOLID = HostApplication("TopSolid", "topsolid")
|
||||
PYTHON = HostApplication("Python", "python")
|
||||
NET = HostApplication(".NET", "net")
|
||||
OTHER = HostApplication("Other", "other")
|
||||
|
||||
_app_name_host_app_mapping = {
|
||||
"dynamo": DYNAMO,
|
||||
"revit": REVIT,
|
||||
"autocad": AUTOCAD,
|
||||
"civil": CIVIL,
|
||||
"rhino": RHINO,
|
||||
"grasshopper": GRASSHOPPER,
|
||||
"unity": UNITY,
|
||||
"gsa": GSA,
|
||||
"microstation": MICROSTATION,
|
||||
"openroads": OPENROADS,
|
||||
"openrail": OPENRAIL,
|
||||
"openbuildings": OPENBUILDINGS,
|
||||
"etabs": ETABS,
|
||||
"sap": SAP2000,
|
||||
"csibridge": CSIBRIDGE,
|
||||
"safe": SAFE,
|
||||
"teklastructures": TEKLASTRUCTURES,
|
||||
"dxf": DXF,
|
||||
"excel": EXCEL,
|
||||
"unreal": UNREAL,
|
||||
"powerbi": POWERBI,
|
||||
"blender": BLENDER,
|
||||
"qgis": QGIS,
|
||||
"arcgis": ARCGIS,
|
||||
"sketchup": SKETCHUP,
|
||||
"archicad": ARCHICAD,
|
||||
"topsolid": TOPSOLID,
|
||||
"python": PYTHON,
|
||||
"net": NET,
|
||||
}
|
||||
|
||||
|
||||
def get_host_app_from_string(app_name: str) -> HostApplication:
|
||||
app_name = app_name.lower().replace(" ", "")
|
||||
for partial_app_name, host_app in _app_name_host_app_mapping.items():
|
||||
if partial_app_name in app_name:
|
||||
return host_app
|
||||
return HostApplication(app_name, app_name)
|
||||
|
||||
# following imports seem to be unnecessary, but they need to stay
|
||||
# to not break the scripts using these functions as non-core
|
||||
from specklepy.core.api.host_applications import (HostApplication, HostAppVersion,
|
||||
get_host_app_from_string,
|
||||
_app_name_host_app_mapping,
|
||||
RHINO,GRASSHOPPER,REVIT,DYNAMO,UNITY,GSA,
|
||||
CIVIL,AUTOCAD,MICROSTATION,OPENROADS,
|
||||
OPENRAIL,OPENBUILDINGS,ETABS,SAP2000,CSIBRIDGE,
|
||||
SAFE,TEKLASTRUCTURES,DXF,EXCEL,UNREAL,POWERBI,
|
||||
BLENDER,QGIS,ARCGIS,SKETCHUP,ARCHICAD,TOPSOLID,
|
||||
PYTHON,NET,OTHER)
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(HostAppVersion.v)
|
||||
|
||||
+7
-193
@@ -3,196 +3,10 @@ from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class Collaborator(BaseModel):
|
||||
id: Optional[str]
|
||||
name: Optional[str]
|
||||
role: Optional[str]
|
||||
avatar: Optional[str]
|
||||
|
||||
|
||||
class Commit(BaseModel):
|
||||
id: Optional[str]
|
||||
message: Optional[str]
|
||||
authorName: Optional[str]
|
||||
authorId: Optional[str]
|
||||
authorAvatar: Optional[str]
|
||||
branchName: Optional[str]
|
||||
createdAt: Optional[datetime]
|
||||
sourceApplication: Optional[str]
|
||||
referencedObject: Optional[str]
|
||||
totalChildrenCount: Optional[int]
|
||||
parents: Optional[List[str]]
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"Commit( id: {self.id}, message: {self.message}, referencedObject:"
|
||||
f" {self.referencedObject}, authorName: {self.authorName}, branchName:"
|
||||
f" {self.branchName}, createdAt: {self.createdAt} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class Commits(BaseModel):
|
||||
totalCount: Optional[int]
|
||||
cursor: Optional[datetime]
|
||||
items: List[Commit] = []
|
||||
|
||||
|
||||
class Object(BaseModel):
|
||||
id: Optional[str]
|
||||
speckleType: Optional[str]
|
||||
applicationId: Optional[str]
|
||||
totalChildrenCount: Optional[int]
|
||||
createdAt: Optional[datetime]
|
||||
|
||||
|
||||
class Branch(BaseModel):
|
||||
id: Optional[str]
|
||||
name: Optional[str]
|
||||
description: Optional[str]
|
||||
commits: Optional[Commits]
|
||||
|
||||
|
||||
class Branches(BaseModel):
|
||||
totalCount: Optional[int]
|
||||
cursor: Optional[datetime]
|
||||
items: List[Branch] = []
|
||||
|
||||
|
||||
class Stream(BaseModel):
|
||||
id: Optional[str] = None
|
||||
name: Optional[str]
|
||||
role: Optional[str] = None
|
||||
isPublic: Optional[bool] = None
|
||||
description: Optional[str] = None
|
||||
createdAt: Optional[datetime] = None
|
||||
updatedAt: Optional[datetime] = None
|
||||
collaborators: List[Collaborator] = Field(default_factory=list)
|
||||
branches: Optional[Branches] = None
|
||||
commit: Optional[Commit] = None
|
||||
object: Optional[Object] = None
|
||||
commentCount: Optional[int] = None
|
||||
favoritedDate: Optional[datetime] = None
|
||||
favoritesCount: Optional[int] = None
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"Stream( id: {self.id}, name: {self.name}, description:"
|
||||
f" {self.description}, isPublic: {self.isPublic})"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class Streams(BaseModel):
|
||||
totalCount: Optional[int]
|
||||
cursor: Optional[datetime]
|
||||
items: List[Stream] = []
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
id: Optional[str]
|
||||
email: Optional[str]
|
||||
name: Optional[str]
|
||||
bio: Optional[str]
|
||||
company: Optional[str]
|
||||
avatar: Optional[str]
|
||||
verified: Optional[bool]
|
||||
role: Optional[str]
|
||||
streams: Optional[Streams]
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"User( id: {self.id}, name: {self.name}, email: {self.email}, company:"
|
||||
f" {self.company} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class LimitedUser(BaseModel):
|
||||
"""Limited user type, for showing public info about a user to another user."""
|
||||
|
||||
id: str
|
||||
name: Optional[str]
|
||||
bio: Optional[str]
|
||||
company: Optional[str]
|
||||
avatar: Optional[str]
|
||||
verified: Optional[bool]
|
||||
role: Optional[str]
|
||||
|
||||
|
||||
class PendingStreamCollaborator(BaseModel):
|
||||
id: Optional[str]
|
||||
inviteId: Optional[str]
|
||||
streamId: Optional[str]
|
||||
streamName: Optional[str]
|
||||
title: Optional[str]
|
||||
role: Optional[str]
|
||||
invitedBy: Optional[User]
|
||||
user: Optional[User]
|
||||
token: Optional[str]
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"PendingStreamCollaborator( inviteId: {self.inviteId}, streamId:"
|
||||
f" {self.streamId}, role: {self.role}, title: {self.title}, invitedBy:"
|
||||
f" {self.user.name if self.user else None})"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class Activity(BaseModel):
|
||||
actionType: Optional[str]
|
||||
info: Optional[dict]
|
||||
userId: Optional[str]
|
||||
streamId: Optional[str]
|
||||
resourceId: Optional[str]
|
||||
resourceType: Optional[str]
|
||||
message: Optional[str]
|
||||
time: Optional[datetime]
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"Activity( streamId: {self.streamId}, actionType: {self.actionType},"
|
||||
f" message: {self.message}, userId: {self.userId} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class ActivityCollection(BaseModel):
|
||||
totalCount: Optional[int]
|
||||
items: Optional[List[Activity]]
|
||||
cursor: Optional[datetime]
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"ActivityCollection( totalCount: {self.totalCount}, items:"
|
||||
f" {len(self.items) if self.items else 0}, cursor:"
|
||||
f" {self.cursor.isoformat() if self.cursor else None} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class ServerInfo(BaseModel):
|
||||
name: Optional[str] = None
|
||||
company: Optional[str] = None
|
||||
url: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
adminContact: Optional[str] = None
|
||||
canonicalUrl: Optional[str] = None
|
||||
roles: Optional[List[dict]] = None
|
||||
scopes: Optional[List[dict]] = None
|
||||
authStrategies: Optional[List[dict]] = None
|
||||
version: Optional[str] = None
|
||||
# following imports seem to be unnecessary, but they need to stay
|
||||
# to not break the scripts using these functions as non-core
|
||||
from specklepy.core.api.models import (Collaborator, Commit,
|
||||
Commits, Object, Branch, Branches,
|
||||
Stream, Streams, User, LimitedUser,
|
||||
PendingStreamCollaborator, Activity,
|
||||
ActivityCollection, ServerInfo)
|
||||
|
||||
@@ -7,6 +7,11 @@ from specklepy.serialization.base_object_serializer import BaseObjectSerializer
|
||||
from specklepy.transports.abstract_transport import AbstractTransport
|
||||
from specklepy.transports.sqlite import SQLiteTransport
|
||||
|
||||
from specklepy.core.api.operations import (send as core_send,
|
||||
receive as _untracked_receive,
|
||||
serialize as core_serialize,
|
||||
deserialize as core_deserialize)
|
||||
|
||||
|
||||
def send(
|
||||
base: Base,
|
||||
@@ -24,47 +29,18 @@ def send(
|
||||
Returns:
|
||||
str -- the object id of the sent object
|
||||
"""
|
||||
|
||||
if not transports and not use_default_cache:
|
||||
raise SpeckleException(
|
||||
message=(
|
||||
"You need to provide at least one transport: cannot send with an empty"
|
||||
" transport list and no default cache"
|
||||
)
|
||||
)
|
||||
|
||||
if isinstance(transports, AbstractTransport):
|
||||
transports = [transports]
|
||||
|
||||
if transports is None:
|
||||
metrics.track(metrics.SEND)
|
||||
transports = []
|
||||
else:
|
||||
metrics.track(metrics.SEND, getattr(transports[0], "account", None))
|
||||
|
||||
if use_default_cache:
|
||||
transports.insert(0, SQLiteTransport())
|
||||
|
||||
serializer = BaseObjectSerializer(write_transports=transports)
|
||||
|
||||
obj_hash, _ = serializer.write_json(base=base)
|
||||
|
||||
return obj_hash
|
||||
return core_send(base, transports, use_default_cache)
|
||||
|
||||
|
||||
def receive(
|
||||
obj_id: str,
|
||||
remote_transport: Optional[AbstractTransport] = None,
|
||||
local_transport: Optional[AbstractTransport] = None,
|
||||
) -> Base:
|
||||
metrics.track(metrics.RECEIVE, getattr(remote_transport, "account", None))
|
||||
return _untracked_receive(obj_id, remote_transport, local_transport)
|
||||
|
||||
|
||||
def _untracked_receive(
|
||||
obj_id: str,
|
||||
remote_transport: Optional[AbstractTransport] = None,
|
||||
local_transport: Optional[AbstractTransport] = None,
|
||||
) -> Base:
|
||||
"""Receives an object from a transport.
|
||||
|
||||
@@ -77,29 +53,8 @@ def _untracked_receive(
|
||||
Returns:
|
||||
Base -- the base object
|
||||
"""
|
||||
if not local_transport:
|
||||
local_transport = SQLiteTransport()
|
||||
|
||||
serializer = BaseObjectSerializer(read_transport=local_transport)
|
||||
|
||||
# try local transport first. if the parent is there, we assume all the children are there and continue with deserialization using the local transport
|
||||
obj_string = local_transport.get_object(obj_id)
|
||||
if obj_string:
|
||||
return serializer.read_json(obj_string=obj_string)
|
||||
|
||||
if not remote_transport:
|
||||
raise SpeckleException(
|
||||
message=(
|
||||
"Could not find the specified object using the local transport, and you"
|
||||
" didn't provide a fallback remote from which to pull it."
|
||||
)
|
||||
)
|
||||
|
||||
obj_string = remote_transport.copy_object_and_children(
|
||||
id=obj_id, target_transport=local_transport
|
||||
)
|
||||
|
||||
return serializer.read_json(obj_string=obj_string)
|
||||
metrics.track(metrics.RECEIVE, getattr(remote_transport, "account", None))
|
||||
return _untracked_receive(obj_id, remote_transport, local_transport)
|
||||
|
||||
|
||||
def serialize(base: Base, write_transports: List[AbstractTransport] = []) -> str:
|
||||
@@ -116,11 +71,8 @@ def serialize(base: Base, write_transports: List[AbstractTransport] = []) -> str
|
||||
Returns:
|
||||
str -- the serialized object
|
||||
"""
|
||||
metrics.track(metrics.SERIALIZE)
|
||||
serializer = BaseObjectSerializer(write_transports=write_transports)
|
||||
|
||||
return serializer.write_json(base)[1]
|
||||
|
||||
metrics.track(metrics.SDK, custom_props={"name": "Serialize"})
|
||||
return core_serialize(base, write_transports)
|
||||
|
||||
def deserialize(
|
||||
obj_string: str, read_transport: Optional[AbstractTransport] = None
|
||||
@@ -141,13 +93,8 @@ def deserialize(
|
||||
Returns:
|
||||
Base -- the deserialized object
|
||||
"""
|
||||
metrics.track(metrics.DESERIALIZE)
|
||||
if not read_transport:
|
||||
read_transport = SQLiteTransport()
|
||||
|
||||
serializer = BaseObjectSerializer(read_transport=read_transport)
|
||||
|
||||
return serializer.read_json(obj_string=obj_string)
|
||||
metrics.track(metrics.SDK, custom_props={"name": "Deserialize"})
|
||||
return core_deserialize(obj_string, read_transport)
|
||||
|
||||
|
||||
__all__ = ["receive", "send", "serialize", "deserialize"]
|
||||
|
||||
+13
-103
@@ -1,3 +1,4 @@
|
||||
from threading import Lock
|
||||
from typing import Any, Dict, List, Optional, Tuple, Type, Union
|
||||
|
||||
from gql.client import Client
|
||||
@@ -13,8 +14,12 @@ from specklepy.logging.exceptions import (
|
||||
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
|
||||
from specklepy.transports.sqlite import SQLiteTransport
|
||||
|
||||
# following imports seem to be unnecessary, but they need to stay
|
||||
# to not break the scripts using these functions as non-core
|
||||
from specklepy.core.api.resource import ResourceBase as CoreResourceBase
|
||||
|
||||
class ResourceBase(object):
|
||||
|
||||
class ResourceBase(CoreResourceBase):
|
||||
def __init__(
|
||||
self,
|
||||
account: Account,
|
||||
@@ -23,106 +28,11 @@ class ResourceBase(object):
|
||||
name: str,
|
||||
server_version: Optional[Tuple[Any, ...]] = None,
|
||||
) -> None:
|
||||
self.account = account
|
||||
self.basepath = basepath
|
||||
self.client = client
|
||||
self.name = name
|
||||
self.server_version = server_version
|
||||
self.schema: Optional[Type] = None
|
||||
|
||||
def _step_into_response(self, response: dict, return_type: Union[str, List, None]):
|
||||
"""Step into the dict to get the relevant data"""
|
||||
if return_type is None:
|
||||
return response
|
||||
if isinstance(return_type, str):
|
||||
return response[return_type]
|
||||
if isinstance(return_type, List):
|
||||
for key in return_type:
|
||||
response = response[key]
|
||||
return response
|
||||
|
||||
def _parse_response(self, response: Union[dict, list, None], schema=None):
|
||||
"""Try to create a class instance from the response"""
|
||||
if response is None:
|
||||
return None
|
||||
if isinstance(response, list):
|
||||
return [self._parse_response(response=r, schema=schema) for r in response]
|
||||
if schema:
|
||||
return schema.parse_obj(response)
|
||||
elif self.schema:
|
||||
try:
|
||||
return self.schema.parse_obj(response)
|
||||
except Exception:
|
||||
s = BaseObjectSerializer(read_transport=SQLiteTransport())
|
||||
return s.recompose_base(response)
|
||||
else:
|
||||
return response
|
||||
|
||||
def make_request(
|
||||
self,
|
||||
query: DocumentNode,
|
||||
params: Optional[Dict] = None,
|
||||
return_type: Union[str, List, None] = None,
|
||||
schema=None,
|
||||
parse_response: bool = True,
|
||||
) -> Any:
|
||||
"""Executes the GraphQL query"""
|
||||
try:
|
||||
response = self.client.execute(query, variable_values=params)
|
||||
except Exception as ex:
|
||||
if isinstance(ex, TransportQueryError):
|
||||
return GraphQLException(
|
||||
message=(
|
||||
f"Failed to execute the GraphQL {self.name} request. Errors:"
|
||||
f" {ex.errors}"
|
||||
),
|
||||
errors=ex.errors,
|
||||
data=ex.data,
|
||||
)
|
||||
else:
|
||||
return SpeckleException(
|
||||
message=(
|
||||
f"Failed to execute the GraphQL {self.name} request. Inner"
|
||||
f" exception: {ex}"
|
||||
),
|
||||
exception=ex,
|
||||
)
|
||||
|
||||
response = self._step_into_response(response=response, return_type=return_type)
|
||||
|
||||
if parse_response:
|
||||
return self._parse_response(response=response, schema=schema)
|
||||
else:
|
||||
return response
|
||||
|
||||
def _check_server_version_at_least(
|
||||
self, target_version: Tuple[Any, ...], unsupported_message: Optional[str] = None
|
||||
):
|
||||
"""Use this check to guard against making unsupported requests on older servers.
|
||||
|
||||
Arguments:
|
||||
target_version {tuple}
|
||||
the minimum server version in the format (major, minor, patch, (tag, build))
|
||||
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
|
||||
"""
|
||||
if not unsupported_message:
|
||||
unsupported_message = (
|
||||
"The client method used is not supported on Speckle Server versions"
|
||||
f" prior to v{'.'.join(target_version)}"
|
||||
)
|
||||
# if version is dev, it should be supported... (or not)
|
||||
if self.server_version == ("dev",):
|
||||
return
|
||||
if self.server_version and self.server_version < target_version:
|
||||
raise UnsupportedException(unsupported_message)
|
||||
|
||||
def _check_invites_supported(self):
|
||||
"""Invites are only supported for Speckle Server >= 2.6.4.
|
||||
Use this check to guard against making unsupported requests on older servers.
|
||||
"""
|
||||
self._check_server_version_at_least(
|
||||
(2, 6, 4),
|
||||
"Stream invites are only supported as of Speckle Server v2.6.4. Please"
|
||||
" update your Speckle Server to use this method or use the"
|
||||
" `grant_permission` flow instead.",
|
||||
super().__init__(
|
||||
account = account,
|
||||
basepath = basepath,
|
||||
client = client,
|
||||
name = name,
|
||||
server_version = server_version
|
||||
)
|
||||
|
||||
@@ -8,10 +8,10 @@ from specklepy.api.resource import ResourceBase
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
NAME = "active_user"
|
||||
from specklepy.core.api.resources.active_user import Resource as CoreResource
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
class Resource(CoreResource):
|
||||
"""API Access class for users"""
|
||||
|
||||
def __init__(self, account, basepath, client, server_version) -> None:
|
||||
@@ -19,7 +19,6 @@ class Resource(ResourceBase):
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.schema = User
|
||||
@@ -35,28 +34,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
User -- the retrieved user
|
||||
"""
|
||||
metrics.track(metrics.USER, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query User {
|
||||
activeUser {
|
||||
id
|
||||
email
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
profiles
|
||||
role
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {}
|
||||
|
||||
return self.make_request(query=query, params=params, return_type="activeUser")
|
||||
metrics.track(metrics.SDK, custom_props={"name": "User Active Get"})
|
||||
return super().get()
|
||||
|
||||
def update(
|
||||
self,
|
||||
@@ -76,28 +55,8 @@ class Resource(ResourceBase):
|
||||
Returns @deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT):
|
||||
bool -- True if your profile was updated successfully
|
||||
"""
|
||||
metrics.track(metrics.USER, self.account, {"name": "update"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation UserUpdate($user: UserUpdateInput!) {
|
||||
userUpdate(user: $user)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"name": name, "company": company, "bio": bio, "avatar": avatar}
|
||||
|
||||
params = {"user": {k: v for k, v in params.items() if v is not None}}
|
||||
|
||||
if not params["user"]:
|
||||
return SpeckleException(
|
||||
message=(
|
||||
"You must provide at least one field to update your user profile"
|
||||
)
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="userUpdate", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User Active Update"})
|
||||
return super().update(name, company, bio, avatar)
|
||||
|
||||
def activity(
|
||||
self,
|
||||
@@ -126,56 +85,8 @@ class Resource(ResourceBase):
|
||||
(ie: return all activity _after_ this time)
|
||||
cursor {datetime} -- timestamp cursor for pagination
|
||||
"""
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query UserActivity(
|
||||
$action_type: String,
|
||||
$before:DateTime,
|
||||
$after: DateTime,
|
||||
$cursor: DateTime,
|
||||
$limit: Int
|
||||
){
|
||||
activeUser {
|
||||
activity(
|
||||
actionType: $action_type,
|
||||
before: $before,
|
||||
after: $after,
|
||||
cursor: $cursor,
|
||||
limit: $limit
|
||||
) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
actionType
|
||||
info
|
||||
userId
|
||||
streamId
|
||||
resourceId
|
||||
resourceType
|
||||
message
|
||||
time
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"limit": limit,
|
||||
"action_type": action_type,
|
||||
"before": before.astimezone(timezone.utc).isoformat() if before else before,
|
||||
"after": after.astimezone(timezone.utc).isoformat() if after else after,
|
||||
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["activeUser", "activity"],
|
||||
schema=ActivityCollection,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User Active Activity"})
|
||||
return super().activity(limit, action_type, before, after, cursor)
|
||||
|
||||
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
|
||||
"""Get all of the active user's pending stream invites
|
||||
@@ -186,36 +97,8 @@ class Resource(ResourceBase):
|
||||
List[PendingStreamCollaborator]
|
||||
-- a list of pending invites for the current user
|
||||
"""
|
||||
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvites {
|
||||
streamInvites{
|
||||
id
|
||||
token
|
||||
inviteId
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
return_type="streamInvites",
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User Active Invites All Get"})
|
||||
return super().get_all_pending_invites()
|
||||
|
||||
def get_pending_invite(
|
||||
self, stream_id: str, token: Optional[str] = None
|
||||
@@ -233,37 +116,5 @@ class Resource(ResourceBase):
|
||||
PendingStreamCollaborator
|
||||
-- the invite for the given stream (or None if it isn't found)
|
||||
"""
|
||||
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvite($streamId: String!, $token: String) {
|
||||
streamInvite(streamId: $streamId, token: $token) {
|
||||
id
|
||||
token
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"streamId": stream_id}
|
||||
if token:
|
||||
params["token"] = token
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInvite",
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User Active Invite Get"})
|
||||
return super().get_pending_invite(stream_id, token)
|
||||
|
||||
@@ -6,10 +6,10 @@ from specklepy.api.models import Branch
|
||||
from specklepy.api.resource import ResourceBase
|
||||
from specklepy.logging import metrics
|
||||
|
||||
NAME = "branch"
|
||||
from specklepy.core.api.resources.branch import Resource as CoreResource
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
class Resource(CoreResource):
|
||||
"""API Access class for branches"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
@@ -17,7 +17,6 @@ class Resource(ResourceBase):
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
self.schema = Branch
|
||||
|
||||
@@ -33,25 +32,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
id {str} -- the newly created branch's id
|
||||
"""
|
||||
metrics.track(metrics.BRANCH, self.account, {"name": "create"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation BranchCreate($branch: BranchCreateInput!) {
|
||||
branchCreate(branch: $branch)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"branch": {
|
||||
"streamId": stream_id,
|
||||
"name": name,
|
||||
"description": description,
|
||||
}
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="branchCreate", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Branch Create"})
|
||||
return super().create(stream_id, name, description)
|
||||
|
||||
def get(self, stream_id: str, name: str, commits_limit: int = 10):
|
||||
"""Get a branch by name from a stream
|
||||
@@ -64,42 +46,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
Branch -- the fetched branch with its latest commits
|
||||
"""
|
||||
metrics.track(metrics.BRANCH, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query BranchGet($stream_id: String!, $name: String!, $commits_limit: Int!) {
|
||||
stream(id: $stream_id) {
|
||||
branch(name: $name) {
|
||||
id,
|
||||
name,
|
||||
description,
|
||||
commits (limit: $commits_limit) {
|
||||
totalCount,
|
||||
cursor,
|
||||
items {
|
||||
id,
|
||||
referencedObject,
|
||||
sourceApplication,
|
||||
totalChildrenCount,
|
||||
message,
|
||||
authorName,
|
||||
authorId,
|
||||
branchName,
|
||||
parents,
|
||||
createdAt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"stream_id": stream_id, "name": name, "commits_limit": commits_limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["stream", "branch"]
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Branch Get"})
|
||||
return super().get(stream_id, name, commits_limit)
|
||||
|
||||
def list(self, stream_id: str, branches_limit: int = 10, commits_limit: int = 10):
|
||||
"""Get a list of branches from a given stream
|
||||
@@ -112,50 +60,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
List[Branch] -- the branches on the stream
|
||||
"""
|
||||
metrics.track(metrics.BRANCH, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query BranchesGet(
|
||||
$stream_id: String!,
|
||||
$branches_limit: Int!,
|
||||
$commits_limit: Int!
|
||||
) {
|
||||
stream(id: $stream_id) {
|
||||
branches(limit: $branches_limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
description
|
||||
commits(limit: $commits_limit) {
|
||||
totalCount
|
||||
items{
|
||||
id
|
||||
message
|
||||
referencedObject
|
||||
sourceApplication
|
||||
parents
|
||||
authorId
|
||||
authorName
|
||||
branchName
|
||||
createdAt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"stream_id": stream_id,
|
||||
"branches_limit": branches_limit,
|
||||
"commits_limit": commits_limit,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["stream", "branches", "items"]
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Branch List"})
|
||||
return super().list(stream_id, branches_limit, commits_limit)
|
||||
|
||||
def update(
|
||||
self,
|
||||
@@ -175,29 +81,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if update is successful
|
||||
"""
|
||||
metrics.track(metrics.BRANCH, self.account, {"name": "update"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation BranchUpdate($branch: BranchUpdateInput!) {
|
||||
branchUpdate(branch: $branch)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"branch": {
|
||||
"streamId": stream_id,
|
||||
"id": branch_id,
|
||||
}
|
||||
}
|
||||
|
||||
if name:
|
||||
params["branch"]["name"] = name
|
||||
if description:
|
||||
params["branch"]["description"] = description
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="branchUpdate", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Branch Update"})
|
||||
return super().update(stream_id, branch_id, name, description)
|
||||
|
||||
def delete(self, stream_id: str, branch_id: str):
|
||||
"""Delete a branch
|
||||
@@ -209,17 +94,5 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if deletion is successful
|
||||
"""
|
||||
metrics.track(metrics.BRANCH, self.account, {"name": "delete"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation BranchDelete($branch: BranchDeleteInput!) {
|
||||
branchDelete(branch: $branch)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"branch": {"streamId": stream_id, "id": branch_id}}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="branchDelete", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Branch Delete"})
|
||||
return super().delete(stream_id, branch_id)
|
||||
|
||||
@@ -6,10 +6,10 @@ from specklepy.api.models import Commit
|
||||
from specklepy.api.resource import ResourceBase
|
||||
from specklepy.logging import metrics
|
||||
|
||||
NAME = "commit"
|
||||
from specklepy.core.api.resources.commit import Resource as CoreResource
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
class Resource(CoreResource):
|
||||
"""API Access class for commits"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
@@ -17,7 +17,6 @@ class Resource(ResourceBase):
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
self.schema = Commit
|
||||
|
||||
@@ -32,32 +31,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
Commit -- the retrieved commit object
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query Commit($stream_id: String!, $commit_id: String!) {
|
||||
stream(id: $stream_id) {
|
||||
commit(id: $commit_id) {
|
||||
id
|
||||
message
|
||||
referencedObject
|
||||
authorId
|
||||
authorName
|
||||
authorAvatar
|
||||
branchName
|
||||
createdAt
|
||||
sourceApplication
|
||||
totalChildrenCount
|
||||
parents
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"stream_id": stream_id, "commit_id": commit_id}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["stream", "commit"]
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Get"})
|
||||
return super().get(stream_id, commit_id)
|
||||
|
||||
def list(self, stream_id: str, limit: int = 10) -> List[Commit]:
|
||||
"""
|
||||
@@ -70,36 +45,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
List[Commit] -- a list of the most recent commit objects
|
||||
"""
|
||||
metrics.track(metrics.COMMIT, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query Commits($stream_id: String!, $limit: Int!) {
|
||||
stream(id: $stream_id) {
|
||||
commits(limit: $limit) {
|
||||
items {
|
||||
id
|
||||
message
|
||||
referencedObject
|
||||
authorName
|
||||
authorId
|
||||
authorName
|
||||
authorAvatar
|
||||
branchName
|
||||
createdAt
|
||||
sourceApplication
|
||||
totalChildrenCount
|
||||
parents
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"stream_id": stream_id, "limit": limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["stream", "commits", "items"]
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Commit List"})
|
||||
return super().list(stream_id, limit)
|
||||
|
||||
def create(
|
||||
self,
|
||||
@@ -128,28 +75,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
str -- the id of the created commit
|
||||
"""
|
||||
metrics.track(metrics.COMMIT, self.account, {"name": "create"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation CommitCreate ($commit: CommitCreateInput!)
|
||||
{ commitCreate(commit: $commit)}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"commit": {
|
||||
"streamId": stream_id,
|
||||
"branchName": branch_name,
|
||||
"objectId": object_id,
|
||||
"message": message,
|
||||
"sourceApplication": source_application,
|
||||
}
|
||||
}
|
||||
if parents:
|
||||
params["commit"]["parents"] = parents
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="commitCreate", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Create"})
|
||||
return super().create(stream_id, object_id, branch_name, message, source_application, parents)
|
||||
|
||||
def update(self, stream_id: str, commit_id: str, message: str) -> bool:
|
||||
"""
|
||||
@@ -164,20 +91,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if the operation succeeded
|
||||
"""
|
||||
metrics.track(metrics.COMMIT, self.account, {"name": "update"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation CommitUpdate($commit: CommitUpdateInput!)
|
||||
{ commitUpdate(commit: $commit)}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"commit": {"streamId": stream_id, "id": commit_id, "message": message}
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="commitUpdate", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Update"})
|
||||
return super().update(stream_id, commit_id, message)
|
||||
|
||||
def delete(self, stream_id: str, commit_id: str) -> bool:
|
||||
"""
|
||||
@@ -191,18 +106,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if the operation succeeded
|
||||
"""
|
||||
metrics.track(metrics.COMMIT, self.account, {"name": "delete"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation CommitDelete($commit: CommitDeleteInput!)
|
||||
{ commitDelete(commit: $commit)}
|
||||
"""
|
||||
)
|
||||
params = {"commit": {"streamId": stream_id, "id": commit_id}}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="commitDelete", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Delete"})
|
||||
return super().delete(stream_id, commit_id)
|
||||
|
||||
def received(
|
||||
self,
|
||||
@@ -214,30 +119,5 @@ class Resource(ResourceBase):
|
||||
"""
|
||||
Mark a commit object a received by the source application.
|
||||
"""
|
||||
metrics.track(metrics.COMMIT, self.account, {"name": "received"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation CommitReceive($receivedInput:CommitReceivedInput!){
|
||||
commitReceive(input:$receivedInput)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"receivedInput": {
|
||||
"sourceApplication": source_application,
|
||||
"streamId": stream_id,
|
||||
"commitId": commit_id,
|
||||
"message": "message",
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="commitReceive",
|
||||
parse_response=False,
|
||||
)
|
||||
except Exception as ex:
|
||||
print(ex.with_traceback)
|
||||
return False
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Received"})
|
||||
return super().received(stream_id, commit_id, source_application, message)
|
||||
|
||||
@@ -5,10 +5,12 @@ from gql import gql
|
||||
from specklepy.api.resource import ResourceBase
|
||||
from specklepy.objects.base import Base
|
||||
|
||||
NAME = "object"
|
||||
from specklepy.logging import metrics
|
||||
|
||||
from specklepy.core.api.resources.object import Resource as CoreResource
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
class Resource(CoreResource):
|
||||
"""API Access class for objects"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
@@ -16,7 +18,6 @@ class Resource(ResourceBase):
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
self.schema = Base
|
||||
|
||||
@@ -31,31 +32,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
Base -- the returned Base object
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query Object($stream_id: String!, $object_id: String!) {
|
||||
stream(id: $stream_id) {
|
||||
id
|
||||
name
|
||||
object(id: $object_id) {
|
||||
id
|
||||
speckleType
|
||||
applicationId
|
||||
createdAt
|
||||
totalChildrenCount
|
||||
data
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"stream_id": stream_id, "object_id": object_id}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["stream", "object", "data"],
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Object Get"})
|
||||
return super().get(stream_id, object_id)
|
||||
|
||||
def create(self, stream_id: str, objects: List[Dict]) -> str:
|
||||
"""
|
||||
@@ -78,15 +56,6 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
str -- the id of the object
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation ObjectCreate($object_input: ObjectCreateInput!) {
|
||||
objectCreate(objectInput: $object_input)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"object_input": {"streamId": stream_id, "objects": objects}}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="objectCreate", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Object Create"})
|
||||
return super().create(stream_id, objects)
|
||||
|
||||
@@ -8,10 +8,10 @@ from specklepy.api.resource import ResourceBase
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
NAME = "other_user"
|
||||
from specklepy.core.api.resources.other_user import Resource as CoreResource
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
class Resource(CoreResource):
|
||||
"""API Access class for other users, that are not the currently active user."""
|
||||
|
||||
def __init__(self, account, basepath, client, server_version) -> None:
|
||||
@@ -19,7 +19,6 @@ class Resource(ResourceBase):
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.schema = LimitedUser
|
||||
@@ -34,26 +33,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
LimitedUser -- the retrieved profile of another user
|
||||
"""
|
||||
metrics.track(metrics.OTHER_USER, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query OtherUser($id: String!) {
|
||||
otherUser(id: $id) {
|
||||
id
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
role
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"id": id}
|
||||
|
||||
return self.make_request(query=query, params=params, return_type="otherUser")
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Other User Get"})
|
||||
return super().get(id)
|
||||
|
||||
def search(
|
||||
self, search_query: str, limit: int = 25
|
||||
@@ -72,28 +53,8 @@ class Resource(ResourceBase):
|
||||
message="User search query must be at least 3 characters"
|
||||
)
|
||||
|
||||
metrics.track(metrics.OTHER_USER, self.account, {"name": "search"})
|
||||
query = gql(
|
||||
"""
|
||||
query UserSearch($search_query: String!, $limit: Int!) {
|
||||
userSearch(query: $search_query, limit: $limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"search_query": search_query, "limit": limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["userSearch", "items"]
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Other User Search"})
|
||||
return super().search(search_query, limit)
|
||||
|
||||
def activity(
|
||||
self,
|
||||
@@ -121,55 +82,6 @@ class Resource(ResourceBase):
|
||||
(ie: return all activity _after_ this time)
|
||||
cursor {datetime} -- timestamp cursor for pagination
|
||||
"""
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query UserActivity(
|
||||
$user_id: String!,
|
||||
$action_type: String,
|
||||
$before:DateTime,
|
||||
$after: DateTime,
|
||||
$cursor: DateTime,
|
||||
$limit: Int
|
||||
){
|
||||
otherUser(id: $user_id) {
|
||||
activity(
|
||||
actionType: $action_type,
|
||||
before: $before,
|
||||
after: $after,
|
||||
cursor: $cursor,
|
||||
limit: $limit
|
||||
) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
actionType
|
||||
info
|
||||
userId
|
||||
streamId
|
||||
resourceId
|
||||
resourceType
|
||||
message
|
||||
time
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"user_id": user_id,
|
||||
"limit": limit,
|
||||
"action_type": action_type,
|
||||
"before": before.astimezone(timezone.utc).isoformat() if before else before,
|
||||
"after": after.astimezone(timezone.utc).isoformat() if after else after,
|
||||
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["otherUser", "activity"],
|
||||
schema=ActivityCollection,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Other User Activity"})
|
||||
return super().activity(user_id, limit, action_type, before, after, cursor)
|
||||
|
||||
@@ -8,10 +8,10 @@ from specklepy.api.resource import ResourceBase
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import GraphQLException
|
||||
|
||||
NAME = "server"
|
||||
from specklepy.core.api.resources.server import Resource as CoreResource
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
class Resource(CoreResource):
|
||||
"""API Access class for the server"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
@@ -19,7 +19,6 @@ class Resource(ResourceBase):
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
|
||||
def get(self) -> ServerInfo:
|
||||
@@ -28,39 +27,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
dict -- the server info in dictionary form
|
||||
"""
|
||||
metrics.track(metrics.SERVER, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query Server {
|
||||
serverInfo {
|
||||
name
|
||||
company
|
||||
description
|
||||
adminContact
|
||||
canonicalUrl
|
||||
version
|
||||
roles {
|
||||
name
|
||||
description
|
||||
resourceTarget
|
||||
}
|
||||
scopes {
|
||||
name
|
||||
description
|
||||
}
|
||||
authStrategies{
|
||||
id
|
||||
name
|
||||
icon
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query, return_type="serverInfo", schema=ServerInfo
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Server Get"})
|
||||
return super().get()
|
||||
|
||||
def version(self) -> Tuple[Any, ...]:
|
||||
"""Get the server version
|
||||
@@ -70,30 +38,7 @@ class Resource(ResourceBase):
|
||||
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
|
||||
"""
|
||||
# not tracking as it will be called along with other mutations / queries as a check
|
||||
query = gql(
|
||||
"""
|
||||
query Server {
|
||||
serverInfo {
|
||||
version
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
ver = self.make_request(
|
||||
query=query, return_type=["serverInfo", "version"], parse_response=False
|
||||
)
|
||||
if isinstance(ver, Exception):
|
||||
raise GraphQLException(
|
||||
f"Could not get server version for {self.basepath}", [ver]
|
||||
)
|
||||
|
||||
# pylint: disable=consider-using-generator; (list comp is faster)
|
||||
return tuple(
|
||||
[
|
||||
int(segment) if segment.isdigit() else segment
|
||||
for segment in re.split(r"\.|-", ver)
|
||||
]
|
||||
)
|
||||
return super().version()
|
||||
|
||||
def apps(self) -> Dict:
|
||||
"""Get the apps registered on the server
|
||||
@@ -101,28 +46,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
dict -- a dictionary of apps registered on the server
|
||||
"""
|
||||
metrics.track(metrics.SERVER, self.account, {"name": "apps"})
|
||||
query = gql(
|
||||
"""
|
||||
query Apps {
|
||||
apps{
|
||||
id
|
||||
name
|
||||
description
|
||||
termsAndConditionsLink
|
||||
trustByDefault
|
||||
logo
|
||||
author {
|
||||
id
|
||||
name
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
return self.make_request(query=query, return_type="apps", parse_response=False)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Server Apps"})
|
||||
return super().apps()
|
||||
|
||||
def create_token(self, name: str, scopes: List[str], lifespan: int) -> str:
|
||||
"""Create a personal API token
|
||||
@@ -135,22 +60,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
str -- the new API token. note: this is the only time you'll see the token!
|
||||
"""
|
||||
metrics.track(metrics.SERVER, self.account, {"name": "create_token"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation TokenCreate($token: ApiTokenCreateInput!) {
|
||||
apiTokenCreate(token: $token)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"token": {"scopes": scopes, "name": name, "lifespan": lifespan}}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="apiTokenCreate",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Server Create Token"})
|
||||
return super().create_token(name, scopes, lifespan)
|
||||
|
||||
def revoke_token(self, token: str) -> bool:
|
||||
"""Revokes (deletes) a personal API token
|
||||
@@ -161,19 +72,5 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if the token was successfully deleted
|
||||
"""
|
||||
metrics.track(metrics.SERVER, self.account, {"name": "revoke_token"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation TokenRevoke($token: String!) {
|
||||
apiTokenRevoke(token: $token)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"token": token}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="apiTokenRevoke",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Server Revoke Token"})
|
||||
return super().revoke_token(token)
|
||||
|
||||
@@ -9,10 +9,10 @@ from specklepy.api.resource import ResourceBase
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException, UnsupportedException
|
||||
|
||||
NAME = "stream"
|
||||
from specklepy.core.api.resources.stream import Resource as CoreResource
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
class Resource(CoreResource):
|
||||
"""API Access class for streams"""
|
||||
|
||||
def __init__(self, account, basepath, client, server_version) -> None:
|
||||
@@ -20,7 +20,6 @@ class Resource(ResourceBase):
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
server_version=server_version,
|
||||
)
|
||||
|
||||
@@ -37,56 +36,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
Stream -- the retrieved stream
|
||||
"""
|
||||
metrics.track(metrics.STREAM, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query Stream($id: String!, $branch_limit: Int!, $commit_limit: Int!) {
|
||||
stream(id: $id) {
|
||||
id
|
||||
name
|
||||
role
|
||||
description
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
commentCount
|
||||
favoritesCount
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
avatar
|
||||
}
|
||||
branches(limit: $branch_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
name
|
||||
description
|
||||
commits(limit: $commit_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
message
|
||||
authorId
|
||||
createdAt
|
||||
authorName
|
||||
referencedObject
|
||||
sourceApplication
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"id": id, "branch_limit": branch_limit, "commit_limit": commit_limit}
|
||||
|
||||
return self.make_request(query=query, params=params, return_type="stream")
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Get"})
|
||||
return super().get(id, branch_limit, commit_limit)
|
||||
|
||||
def list(self, stream_limit: int = 10) -> List[Stream]:
|
||||
"""Get a list of the user's streams
|
||||
@@ -97,50 +48,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
List[Stream] -- A list of Stream objects
|
||||
"""
|
||||
metrics.track(metrics.STREAM, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query User($stream_limit: Int!) {
|
||||
user {
|
||||
id
|
||||
bio
|
||||
name
|
||||
email
|
||||
avatar
|
||||
company
|
||||
verified
|
||||
profiles
|
||||
role
|
||||
streams(limit: $stream_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
name
|
||||
role
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
description
|
||||
commentCount
|
||||
favoritesCount
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"stream_limit": stream_limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["user", "streams", "items"]
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream List"})
|
||||
return super().list(stream_limit)
|
||||
|
||||
def create(
|
||||
self,
|
||||
@@ -159,22 +68,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
id {str} -- the id of the newly created stream
|
||||
"""
|
||||
metrics.track(metrics.STREAM, self.account, {"name": "create"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamCreate($stream: StreamCreateInput!) {
|
||||
streamCreate(stream: $stream)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"stream": {"name": name, "description": description, "isPublic": is_public}
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="streamCreate", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Create"})
|
||||
return super().create(name, description, is_public)
|
||||
|
||||
def update(
|
||||
self,
|
||||
@@ -195,27 +90,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- whether the stream update was successful
|
||||
"""
|
||||
metrics.track(metrics.STREAM, self.account, {"name": "update"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamUpdate($stream: StreamUpdateInput!) {
|
||||
streamUpdate(stream: $stream)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"id": id,
|
||||
"name": name,
|
||||
"description": description,
|
||||
"isPublic": is_public,
|
||||
}
|
||||
# remove None values so graphql doesn't cry
|
||||
params = {"stream": {k: v for k, v in params.items() if v is not None}}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="streamUpdate", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Update"})
|
||||
return super().update(id, name, description, is_public)
|
||||
|
||||
def delete(self, id: str) -> bool:
|
||||
"""Delete a stream given its id
|
||||
@@ -226,20 +102,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- whether the deletion was successful
|
||||
"""
|
||||
metrics.track(metrics.STREAM, self.account, {"name": "delete"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamDelete($id: String!) {
|
||||
streamDelete(id: $id)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"id": id}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="streamDelete", parse_response=False
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Delete"})
|
||||
return super().delete(id)
|
||||
|
||||
def search(
|
||||
self,
|
||||
@@ -259,67 +123,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
List[Stream] -- a list of Streams that match the search query
|
||||
"""
|
||||
metrics.track(metrics.STREAM, self.account, {"name": "search"})
|
||||
query = gql(
|
||||
"""
|
||||
query StreamSearch(
|
||||
$search_query: String!,
|
||||
$limit: Int!,
|
||||
$branch_limit:Int!,
|
||||
$commit_limit:Int!
|
||||
) {
|
||||
streams(query: $search_query, limit: $limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
role
|
||||
description
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
avatar
|
||||
}
|
||||
branches(limit: $branch_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
name
|
||||
description
|
||||
commits(limit: $commit_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
referencedObject
|
||||
message
|
||||
authorName
|
||||
authorId
|
||||
createdAt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"search_query": search_query,
|
||||
"limit": limit,
|
||||
"branch_limit": branch_limit,
|
||||
"commit_limit": commit_limit,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["streams", "items"]
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Search"})
|
||||
return super().search(search_query, limit, branch_limit, commit_limit)
|
||||
|
||||
def favorite(self, stream_id: str, favorited: bool = True):
|
||||
"""Favorite or unfavorite the given stream.
|
||||
@@ -332,86 +137,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
Stream -- the stream with its `id`, `name`, and `favoritedDate`
|
||||
"""
|
||||
metrics.track(metrics.STREAM, self.account, {"name": "favorite"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamFavorite($stream_id: String!, $favorited: Boolean!) {
|
||||
streamFavorite(streamId: $stream_id, favorited: $favorited) {
|
||||
id
|
||||
name
|
||||
favoritedDate
|
||||
favoritesCount
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"stream_id": stream_id,
|
||||
"favorited": favorited,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["streamFavorite"]
|
||||
)
|
||||
|
||||
@deprecated(
|
||||
version="2.6.4",
|
||||
reason=(
|
||||
"As of Speckle Server v2.6.4, this method is deprecated. Users need to be"
|
||||
" invited and accept the invite before being added to a stream"
|
||||
),
|
||||
)
|
||||
def grant_permission(self, stream_id: str, user_id: str, role: str):
|
||||
"""Grant permissions to a user on a given stream
|
||||
|
||||
Valid for Speckle Server version < 2.6.4
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to grant permissions to
|
||||
user_id {str} -- the id of the user to grant permissions for
|
||||
role {str} -- the role to grant the user
|
||||
|
||||
Returns:
|
||||
bool -- True if the operation was successful
|
||||
"""
|
||||
metrics.track(metrics.PERMISSION, self.account, {"name": "add", "role": role})
|
||||
# we're checking for the actual version info, and if the version is 'dev' we treat it
|
||||
# as an up to date instance
|
||||
if self.server_version and (
|
||||
self.server_version == ("dev",) or self.server_version >= (2, 6, 4)
|
||||
):
|
||||
raise UnsupportedException(
|
||||
"Server mutation `grant_permission` is no longer supported as of"
|
||||
" Speckle Server v2.6.4. Please use the new `update_permission` method"
|
||||
" to change an existing user's permission or use the `invite` method to"
|
||||
" invite a user to a stream."
|
||||
)
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamGrantPermission(
|
||||
$permission_params: StreamGrantPermissionInput !
|
||||
) {
|
||||
streamGrantPermission(permissionParams: $permission_params)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"permission_params": {
|
||||
"streamId": stream_id,
|
||||
"userId": user_id,
|
||||
"role": role,
|
||||
}
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamGrantPermission",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Favorite"})
|
||||
return super().favorite(stream_id, favorited)
|
||||
|
||||
def get_all_pending_invites(
|
||||
self, stream_id: str
|
||||
@@ -428,46 +155,8 @@ class Resource(ResourceBase):
|
||||
List[PendingStreamCollaborator]
|
||||
-- a list of pending invites for the specified stream
|
||||
"""
|
||||
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvites($streamId: String!) {
|
||||
stream(id: $streamId){
|
||||
pendingCollaborators {
|
||||
id
|
||||
token
|
||||
inviteId
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy{
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
user {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"streamId": stream_id}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["stream", "pendingCollaborators"],
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Get"})
|
||||
return super().get_all_pending_invites(stream_id)
|
||||
|
||||
def invite(
|
||||
self,
|
||||
@@ -493,38 +182,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if the operation was successful
|
||||
"""
|
||||
metrics.track(metrics.INVITE, self.account, {"name": "create"})
|
||||
self._check_invites_supported()
|
||||
|
||||
if email is None and user_id is None:
|
||||
raise SpeckleException(
|
||||
"You must provide either an email or a user id to use the"
|
||||
" `stream.invite` method"
|
||||
)
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamInviteCreate($input: StreamInviteCreateInput!) {
|
||||
streamInviteCreate(input: $input)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"email": email,
|
||||
"userId": user_id,
|
||||
"streamId": stream_id,
|
||||
"message": message,
|
||||
"role": role,
|
||||
}
|
||||
params = {"input": {k: v for k, v in params.items() if v is not None}}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInviteCreate",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Create"})
|
||||
return super().invite(stream_id, email, user_id, role, message)
|
||||
|
||||
def invite_batch(
|
||||
self,
|
||||
@@ -549,42 +208,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if the operation was successful
|
||||
"""
|
||||
metrics.track(metrics.INVITE, self.account, {"name": "batch create"})
|
||||
self._check_invites_supported()
|
||||
if emails is None and user_ids is None:
|
||||
raise SpeckleException(
|
||||
"You must provide either an email or a user id to use the"
|
||||
" `stream.invite` method"
|
||||
)
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamInviteBatchCreate($input: [StreamInviteCreateInput!]!) {
|
||||
streamInviteBatchCreate(input: $input)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
email_invites = [
|
||||
{"streamId": stream_id, "message": message, "email": email}
|
||||
for email in emails
|
||||
if emails is not None
|
||||
]
|
||||
|
||||
user_invites = [
|
||||
{"streamId": stream_id, "message": message, "userId": user_id}
|
||||
for user_id in user_ids
|
||||
if user_ids is not None
|
||||
]
|
||||
|
||||
params = {"input": [*email_invites, *user_invites]}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInviteBatchCreate",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Batch Create"})
|
||||
return super().invite_batch(stream_id, emails, user_ids, message)
|
||||
|
||||
def invite_cancel(self, stream_id: str, invite_id: str) -> bool:
|
||||
"""Cancel an existing stream invite
|
||||
@@ -598,25 +223,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- true if the operation was successful
|
||||
"""
|
||||
metrics.track(metrics.INVITE, self.account, {"name": "cancel"})
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamInviteCancel($streamId: String!, $inviteId: String!) {
|
||||
streamInviteCancel(streamId: $streamId, inviteId: $inviteId)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"streamId": stream_id, "inviteId": invite_id}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInviteCancel",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Cancel"})
|
||||
return super().invite_cancel(stream_id, invite_id)
|
||||
|
||||
def invite_use(self, stream_id: str, token: str, accept: bool = True) -> bool:
|
||||
"""Accept or decline a stream invite
|
||||
@@ -632,29 +240,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- true if the operation was successful
|
||||
"""
|
||||
metrics.track(metrics.INVITE, self.account, {"name": "use"})
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamInviteUse(
|
||||
$accept: Boolean!,
|
||||
$streamId: String!,
|
||||
$token: String!
|
||||
) {
|
||||
streamInviteUse(accept: $accept, streamId: $streamId, token: $token)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"streamId": stream_id, "token": token, "accept": accept}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInviteUse",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Invite Use"})
|
||||
return super().invite_use(stream_id, token, accept)
|
||||
|
||||
def update_permission(self, stream_id: str, user_id: str, role: str):
|
||||
"""Updates permissions for a user on a given stream
|
||||
@@ -669,41 +256,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if the operation was successful
|
||||
"""
|
||||
metrics.track(
|
||||
metrics.PERMISSION, self.account, {"name": "update", "role": role}
|
||||
)
|
||||
if self.server_version and (
|
||||
self.server_version != ("dev",) and self.server_version < (2, 6, 4)
|
||||
):
|
||||
raise UnsupportedException(
|
||||
"Server mutation `update_permission` is only supported as of Speckle"
|
||||
" Server v2.6.4. Please update your Speckle Server to use this method"
|
||||
" or use the `grant_permission` method instead."
|
||||
)
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamUpdatePermission(
|
||||
$permission_params: StreamUpdatePermissionInput!
|
||||
) {
|
||||
streamUpdatePermission(permissionParams: $permission_params)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"permission_params": {
|
||||
"streamId": stream_id,
|
||||
"userId": user_id,
|
||||
"role": role,
|
||||
}
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamUpdatePermission",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Permission Update", "role": role})
|
||||
return super().update_permission(stream_id, user_id, role)
|
||||
|
||||
def revoke_permission(self, stream_id: str, user_id: str):
|
||||
"""Revoke permissions from a user on a given stream
|
||||
@@ -715,25 +269,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if the operation was successful
|
||||
"""
|
||||
metrics.track(metrics.PERMISSION, self.account, {"name": "revoke"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamRevokePermission(
|
||||
$permission_params: StreamRevokePermissionInput!
|
||||
) {
|
||||
streamRevokePermission(permissionParams: $permission_params)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"permission_params": {"streamId": stream_id, "userId": user_id}}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamRevokePermission",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Permission Revoke"})
|
||||
return super().revoke_permission(stream_id, user_id)
|
||||
|
||||
def activity(
|
||||
self,
|
||||
@@ -762,64 +299,6 @@ class Resource(ResourceBase):
|
||||
-- oldest cutoff for activity (ie: return all activity _after_ this time)
|
||||
cursor {datetime} -- timestamp cursor for pagination
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query StreamActivity(
|
||||
$stream_id: String!,
|
||||
$action_type: String,
|
||||
$before:DateTime,
|
||||
$after: DateTime,
|
||||
$cursor: DateTime,
|
||||
$limit: Int
|
||||
){
|
||||
stream(id: $stream_id) {
|
||||
activity(
|
||||
actionType: $action_type,
|
||||
before: $before,
|
||||
after: $after,
|
||||
cursor: $cursor,
|
||||
limit: $limit
|
||||
) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
actionType
|
||||
info
|
||||
userId
|
||||
streamId
|
||||
resourceId
|
||||
resourceType
|
||||
message
|
||||
time
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
try:
|
||||
params = {
|
||||
"stream_id": stream_id,
|
||||
"limit": limit,
|
||||
"action_type": action_type,
|
||||
"before": before.astimezone(timezone.utc).isoformat()
|
||||
if before
|
||||
else before,
|
||||
"after": after.astimezone(timezone.utc).isoformat() if after else after,
|
||||
"cursor": cursor.astimezone(timezone.utc).isoformat()
|
||||
if cursor
|
||||
else cursor,
|
||||
}
|
||||
except AttributeError as e:
|
||||
raise SpeckleException(
|
||||
"Could not get stream activity - `before`, `after`, and `cursor` must"
|
||||
" be in `datetime` format if provided",
|
||||
ValueError(),
|
||||
) from e
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["stream", "activity"],
|
||||
schema=ActivityCollection,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Activity"})
|
||||
return super().activity(stream_id, action_type, limit, before, after, cursor)
|
||||
|
||||
@@ -8,8 +8,8 @@ from specklepy.api.resource import ResourceBase
|
||||
from specklepy.api.resources.stream import Stream
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
NAME = "subscribe"
|
||||
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.core.api.resources.subscriptions import Resource as CoreResource
|
||||
|
||||
def check_wsclient(function):
|
||||
@wraps(function)
|
||||
@@ -24,7 +24,7 @@ def check_wsclient(function):
|
||||
return check_wsclient_wrapper
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
class Resource(CoreResource):
|
||||
"""API Access class for subscriptions"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
@@ -32,7 +32,6 @@ class Resource(ResourceBase):
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
|
||||
@check_wsclient
|
||||
@@ -47,14 +46,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
Stream -- the update stream
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
subscription { userStreamAdded }
|
||||
"""
|
||||
)
|
||||
return await self.subscribe(
|
||||
query=query, callback=callback, return_type="userStreamAdded", schema=Stream
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Subscription Stream Added"})
|
||||
return super().stream_added(callback)
|
||||
|
||||
@check_wsclient
|
||||
async def stream_updated(self, id: str, callback: Optional[Callable] = None):
|
||||
@@ -71,20 +64,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
Stream -- the update stream
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
subscription Update($id: String!) { streamUpdated(streamId: $id) }
|
||||
"""
|
||||
)
|
||||
params = {"id": id}
|
||||
|
||||
return await self.subscribe(
|
||||
query=query,
|
||||
params=params,
|
||||
callback=callback,
|
||||
return_type="streamUpdated",
|
||||
schema=Stream,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Subscription Stream Updated"})
|
||||
return super().stream_updated(id, callback)
|
||||
|
||||
@check_wsclient
|
||||
async def stream_removed(self, callback: Optional[Callable] = None):
|
||||
@@ -102,18 +83,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
dict -- dict containing 'id' of stream removed and optionally 'revokedBy'
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
subscription { userStreamRemoved }
|
||||
"""
|
||||
)
|
||||
|
||||
return await self.subscribe(
|
||||
query=query,
|
||||
callback=callback,
|
||||
return_type="userStreamRemoved",
|
||||
parse_response=False,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "Subscription Stream Removed"})
|
||||
return super().stream_removed(callback)
|
||||
|
||||
@check_wsclient
|
||||
async def subscribe(
|
||||
|
||||
@@ -6,10 +6,11 @@ from gql import gql
|
||||
|
||||
from specklepy.api.models import ActivityCollection, PendingStreamCollaborator, User
|
||||
from specklepy.api.resource import ResourceBase
|
||||
from specklepy.logging import metrics
|
||||
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
NAME = "user"
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.core.api.resources.user import Resource as CoreResource
|
||||
|
||||
DEPRECATION_VERSION = "2.9.0"
|
||||
DEPRECATION_TEXT = (
|
||||
@@ -18,7 +19,7 @@ DEPRECATION_TEXT = (
|
||||
)
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
class Resource(CoreResource):
|
||||
"""API Access class for users"""
|
||||
|
||||
def __init__(self, account, basepath, client, server_version) -> None:
|
||||
@@ -26,7 +27,6 @@ class Resource(ResourceBase):
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.schema = User
|
||||
@@ -44,29 +44,9 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
User -- the retrieved user
|
||||
"""
|
||||
metrics.track(metrics.USER, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query User($id: String) {
|
||||
user(id: $id) {
|
||||
id
|
||||
email
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
profiles
|
||||
role
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"id": id}
|
||||
|
||||
return self.make_request(query=query, params=params, return_type="user")
|
||||
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User Get_deprecated"})
|
||||
return super().get(id)
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def search(
|
||||
self, search_query: str, limit: int = 25
|
||||
@@ -81,33 +61,8 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
List[User] -- a list of User objects that match the search query
|
||||
"""
|
||||
if len(search_query) < 3:
|
||||
return SpeckleException(
|
||||
message="User search query must be at least 3 characters"
|
||||
)
|
||||
|
||||
metrics.track(metrics.USER, self.account, {"name": "search"})
|
||||
query = gql(
|
||||
"""
|
||||
query UserSearch($search_query: String!, $limit: Int!) {
|
||||
userSearch(query: $search_query, limit: $limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"search_query": search_query, "limit": limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["userSearch", "items"]
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User Search_deprecated"})
|
||||
return super().search(search_query, limit)
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def update(
|
||||
@@ -128,28 +83,9 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
bool -- True if your profile was updated successfully
|
||||
"""
|
||||
metrics.track(metrics.USER, self.account, {"name": "update"})
|
||||
query = gql(
|
||||
"""
|
||||
mutation UserUpdate($user: UserUpdateInput!) {
|
||||
userUpdate(user: $user)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"name": name, "company": company, "bio": bio, "avatar": avatar}
|
||||
|
||||
params = {"user": {k: v for k, v in params.items() if v is not None}}
|
||||
|
||||
if not params["user"]:
|
||||
return SpeckleException(
|
||||
message=(
|
||||
"You must provide at least one field to update your user profile"
|
||||
)
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="userUpdate", parse_response=False
|
||||
)
|
||||
#metrics.track(metrics.USER, self.account, {"name": "update"})
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User Update_deprecated"})
|
||||
return super().update(name, company, bio, avatar)
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def activity(
|
||||
@@ -180,58 +116,9 @@ class Resource(ResourceBase):
|
||||
-- oldest cutoff for activity (ie: return all activity _after_ this time)
|
||||
cursor {datetime} -- timestamp cursor for pagination
|
||||
"""
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query UserActivity(
|
||||
$user_id: String,
|
||||
$action_type: String,
|
||||
$before:DateTime,
|
||||
$after: DateTime,
|
||||
$cursor: DateTime,
|
||||
$limit: Int
|
||||
){
|
||||
user(id: $user_id) {
|
||||
activity(
|
||||
actionType: $action_type,
|
||||
before: $before,
|
||||
after: $after,
|
||||
cursor: $cursor,
|
||||
limit: $limit
|
||||
) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
actionType
|
||||
info
|
||||
userId
|
||||
streamId
|
||||
resourceId
|
||||
resourceType
|
||||
message
|
||||
time
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"user_id": user_id,
|
||||
"limit": limit,
|
||||
"action_type": action_type,
|
||||
"before": before.astimezone(timezone.utc).isoformat() if before else before,
|
||||
"after": after.astimezone(timezone.utc).isoformat() if after else after,
|
||||
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["user", "activity"],
|
||||
schema=ActivityCollection,
|
||||
)
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User Activity_deprecated"})
|
||||
return super().activity(user_id, limit, action_type, before, after, cursor)
|
||||
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
|
||||
@@ -243,36 +130,10 @@ class Resource(ResourceBase):
|
||||
List[PendingStreamCollaborator]
|
||||
-- a list of pending invites for the current user
|
||||
"""
|
||||
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvites {
|
||||
streamInvites{
|
||||
id
|
||||
token
|
||||
inviteId
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
return_type="streamInvites",
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
#metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User GetAllInvites_deprecated"})
|
||||
return super().get_all_pending_invites()
|
||||
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def get_pending_invite(
|
||||
@@ -291,37 +152,7 @@ class Resource(ResourceBase):
|
||||
PendingStreamCollaborator
|
||||
-- the invite for the given stream (or None if it isn't found)
|
||||
"""
|
||||
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvite($streamId: String!, $token: String) {
|
||||
streamInvite(streamId: $streamId, token: $token) {
|
||||
id
|
||||
token
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"streamId": stream_id}
|
||||
if token:
|
||||
params["token"] = token
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInvite",
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
#metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||
metrics.track(metrics.SDK, self.account, {"name": "User GetInvite_deprecated"})
|
||||
return super().get_pending_invite(stream_id, token)
|
||||
|
||||
+10
-104
@@ -7,12 +7,13 @@ from specklepy.api.credentials import (
|
||||
get_account_from_token,
|
||||
get_local_accounts,
|
||||
)
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
||||
from specklepy.transports.server.server import ServerTransport
|
||||
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.core.api.wrapper import StreamWrapper as CoreStreamWrapper
|
||||
|
||||
class StreamWrapper:
|
||||
class StreamWrapper(CoreStreamWrapper):
|
||||
"""
|
||||
The `StreamWrapper` gives you some handy helpers to deal with urls and
|
||||
get authenticated clients and transports.
|
||||
@@ -49,93 +50,16 @@ class StreamWrapper:
|
||||
_client: SpeckleClient = None
|
||||
_account: Account = None
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"StreamWrapper( server: {self.host}, stream_id: {self.stream_id}, type:"
|
||||
f" {self.type} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
if self.object_id:
|
||||
return "object"
|
||||
elif self.commit_id:
|
||||
return "commit"
|
||||
elif self.branch_name:
|
||||
return "branch"
|
||||
else:
|
||||
return "stream" if self.stream_id else "invalid"
|
||||
|
||||
def __init__(self, url: str) -> None:
|
||||
self.stream_url = url
|
||||
parsed = urlparse(url)
|
||||
self.host = parsed.netloc
|
||||
self.use_ssl = parsed.scheme == "https"
|
||||
segments = parsed.path.strip("/").split("/", 3)
|
||||
metrics.track(metrics.STREAM_WRAPPER, self.get_account())
|
||||
|
||||
if not segments or len(segments) < 2:
|
||||
raise SpeckleException(
|
||||
f"Cannot parse {url} into a stream wrapper class - invalid URL"
|
||||
" provided."
|
||||
)
|
||||
|
||||
while segments:
|
||||
segment = segments.pop(0)
|
||||
if segments and segment.lower() == "streams":
|
||||
self.stream_id = segments.pop(0)
|
||||
elif segments and segment.lower() == "commits":
|
||||
self.commit_id = segments.pop(0)
|
||||
elif segments and segment.lower() == "branches":
|
||||
self.branch_name = unquote(segments.pop(0))
|
||||
elif segments and segment.lower() == "objects":
|
||||
self.object_id = segments.pop(0)
|
||||
elif segment.lower() == "globals":
|
||||
self.branch_name = "globals"
|
||||
if segments:
|
||||
self.commit_id = segments.pop(0)
|
||||
else:
|
||||
raise SpeckleException(
|
||||
f"Cannot parse {url} into a stream wrapper class - invalid URL"
|
||||
" provided."
|
||||
)
|
||||
|
||||
if not self.stream_id:
|
||||
raise SpeckleException(
|
||||
f"Cannot parse {url} into a stream wrapper class - no stream id found."
|
||||
)
|
||||
|
||||
@property
|
||||
def server_url(self):
|
||||
return f"{'https' if self.use_ssl else 'http'}://{self.host}"
|
||||
super().__init__(url = url)
|
||||
|
||||
def get_account(self, token: str = None) -> Account:
|
||||
"""
|
||||
Gets an account object for this server from the local accounts db
|
||||
(added via Speckle Manager or a json file)
|
||||
"""
|
||||
if self._account and self._account.token:
|
||||
return self._account
|
||||
|
||||
self._account = next(
|
||||
(
|
||||
a
|
||||
for a in get_local_accounts()
|
||||
if self.host == urlparse(a.serverInfo.url).netloc
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
if not self._account:
|
||||
self._account = get_account_from_token(token, self.server_url)
|
||||
|
||||
if self._client:
|
||||
self._client.authenticate_with_account(self._account)
|
||||
|
||||
return self._account
|
||||
metrics.track(metrics.SDK, custom_props={"name": "Stream Wrapper Get Account"})
|
||||
return super().get_account(token)
|
||||
|
||||
def get_client(self, token: str = None) -> SpeckleClient:
|
||||
"""
|
||||
@@ -152,25 +76,8 @@ class StreamWrapper:
|
||||
SpeckleClient
|
||||
-- authenticated with a corresponding local account or the provided token
|
||||
"""
|
||||
if self._client and token is None:
|
||||
return self._client
|
||||
|
||||
if not self._account or not self._account.token:
|
||||
self.get_account(token)
|
||||
|
||||
if not self._client:
|
||||
self._client = SpeckleClient(host=self.host, use_ssl=self.use_ssl)
|
||||
|
||||
if self._account.token is None and token is None:
|
||||
warn(f"No local account found for server {self.host}", SpeckleWarning)
|
||||
return self._client
|
||||
|
||||
if self._account.token:
|
||||
self._client.authenticate_with_account(self._account)
|
||||
else:
|
||||
self._client.authenticate_with_token(token)
|
||||
|
||||
return self._client
|
||||
metrics.track(metrics.SDK, custom_props={"name": "Stream Wrapper Get Client"})
|
||||
return super().get_client(token)
|
||||
|
||||
def get_transport(self, token: str = None) -> ServerTransport:
|
||||
"""
|
||||
@@ -183,6 +90,5 @@ class StreamWrapper:
|
||||
ServerTransport -- constructed for this stream
|
||||
with a pre-authenticated client
|
||||
"""
|
||||
if not self._account or not self._account.token:
|
||||
self.get_account(token)
|
||||
return ServerTransport(self.stream_id, account=self._account)
|
||||
metrics.track(metrics.SDK, custom_props={"name": "Stream Wrapper Get Transport"})
|
||||
return super().get_transport(token)
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
import re
|
||||
from typing import Dict
|
||||
from warnings import warn
|
||||
|
||||
from deprecated import deprecated
|
||||
from gql import Client
|
||||
from gql.transport.exceptions import TransportServerError
|
||||
from gql.transport.requests import RequestsHTTPTransport
|
||||
from gql.transport.websockets import WebsocketsTransport
|
||||
|
||||
from specklepy.core.api import resources
|
||||
from specklepy.core.api.credentials import Account, get_account_from_token
|
||||
from specklepy.core.api.resources import (
|
||||
user,
|
||||
active_user,
|
||||
branch,
|
||||
commit,
|
||||
object,
|
||||
other_user,
|
||||
server,
|
||||
stream,
|
||||
subscriptions,
|
||||
)
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
||||
|
||||
|
||||
class SpeckleClient:
|
||||
"""
|
||||
The `SpeckleClient` is your entry point for interacting with
|
||||
your Speckle Server's GraphQL API.
|
||||
You'll need to have access to a server to use it,
|
||||
or you can use our public server `speckle.xyz`.
|
||||
|
||||
To authenticate the client, you'll need to have downloaded
|
||||
the [Speckle Manager](https://speckle.guide/#speckle-manager)
|
||||
and added your account.
|
||||
|
||||
```py
|
||||
from specklepy.api.client import SpeckleClient
|
||||
from specklepy.api.credentials import get_default_account
|
||||
|
||||
# initialise the client
|
||||
client = SpeckleClient(host="speckle.xyz") # or whatever your host is
|
||||
# client = SpeckleClient(host="localhost:3000", use_ssl=False) or use local server
|
||||
|
||||
# authenticate the client with an account (account has been added in Speckle Manager)
|
||||
account = get_default_account()
|
||||
client.authenticate_with_account(account)
|
||||
|
||||
# create a new stream. this returns the stream id
|
||||
new_stream_id = client.stream.create(name="a shiny new stream")
|
||||
|
||||
# use that stream id to get the stream from the server
|
||||
new_stream = client.stream.get(id=new_stream_id)
|
||||
```
|
||||
"""
|
||||
|
||||
DEFAULT_HOST = "speckle.xyz"
|
||||
USE_SSL = True
|
||||
|
||||
def __init__(self, host: str = DEFAULT_HOST, use_ssl: bool = USE_SSL) -> None:
|
||||
ws_protocol = "ws"
|
||||
http_protocol = "http"
|
||||
|
||||
if use_ssl:
|
||||
ws_protocol = "wss"
|
||||
http_protocol = "https"
|
||||
|
||||
# sanitise host input by removing protocol and trailing slash
|
||||
host = re.sub(r"((^\w+:|^)\/\/)|(\/$)", "", host)
|
||||
|
||||
self.url = f"{http_protocol}://{host}"
|
||||
self.graphql = f"{self.url}/graphql"
|
||||
self.ws_url = f"{ws_protocol}://{host}/graphql"
|
||||
self.account = Account()
|
||||
|
||||
self.httpclient = Client(
|
||||
transport=RequestsHTTPTransport(url=self.graphql, verify=True, retries=3)
|
||||
)
|
||||
self.wsclient = None
|
||||
|
||||
self._init_resources()
|
||||
|
||||
# ? Check compatibility with the server - i think we can skip this at this point? save a request
|
||||
# try:
|
||||
# server_info = self.server.get()
|
||||
# if isinstance(server_info, Exception):
|
||||
# raise server_info
|
||||
# if not isinstance(server_info, ServerInfo):
|
||||
# raise Exception("Couldn't get ServerInfo")
|
||||
# except Exception as ex:
|
||||
# raise SpeckleException(
|
||||
# f"{self.url} is not a compatible Speckle Server", ex
|
||||
# ) from ex
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"SpeckleClient( server: {self.url}, authenticated:"
|
||||
f" {self.account.token is not None} )"
|
||||
)
|
||||
|
||||
@deprecated(
|
||||
version="2.6.0",
|
||||
reason=(
|
||||
"Renamed: please use `authenticate_with_account` or"
|
||||
" `authenticate_with_token` instead."
|
||||
),
|
||||
)
|
||||
def authenticate(self, token: str) -> None:
|
||||
"""Authenticate the client using a personal access token
|
||||
The token is saved in the client object and a synchronous GraphQL
|
||||
entrypoint is created
|
||||
|
||||
Arguments:
|
||||
token {str} -- an api token
|
||||
"""
|
||||
self.authenticate_with_token(token)
|
||||
self._set_up_client()
|
||||
|
||||
def authenticate_with_token(self, token: str) -> None:
|
||||
"""
|
||||
Authenticate the client using a personal access token.
|
||||
The token is saved in the client object and a synchronous GraphQL
|
||||
entrypoint is created
|
||||
|
||||
Arguments:
|
||||
token {str} -- an api token
|
||||
"""
|
||||
self.account = get_account_from_token(token, self.url)
|
||||
self._set_up_client()
|
||||
|
||||
def authenticate_with_account(self, account: Account) -> None:
|
||||
"""Authenticate the client using an Account object
|
||||
The account is saved in the client object and a synchronous GraphQL
|
||||
entrypoint is created
|
||||
|
||||
Arguments:
|
||||
account {Account} -- the account object which can be found with
|
||||
`get_default_account` or `get_local_accounts`
|
||||
"""
|
||||
self.account = account
|
||||
self._set_up_client()
|
||||
|
||||
def _set_up_client(self) -> None:
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.account.token}",
|
||||
"Content-Type": "application/json",
|
||||
"apollographql-client-name": metrics.HOST_APP,
|
||||
"apollographql-client-version": metrics.HOST_APP_VERSION,
|
||||
}
|
||||
httptransport = RequestsHTTPTransport(
|
||||
url=self.graphql, headers=headers, verify=True, retries=3
|
||||
)
|
||||
wstransport = WebsocketsTransport(
|
||||
url=self.ws_url,
|
||||
init_payload={"Authorization": f"Bearer {self.account.token}"},
|
||||
)
|
||||
self.httpclient = Client(transport=httptransport)
|
||||
self.wsclient = Client(transport=wstransport)
|
||||
|
||||
self._init_resources()
|
||||
|
||||
try:
|
||||
user_or_error = self.active_user.get()
|
||||
if isinstance(user_or_error, SpeckleException):
|
||||
if isinstance(user_or_error.exception, TransportServerError):
|
||||
raise user_or_error.exception
|
||||
else:
|
||||
raise user_or_error
|
||||
except TransportServerError as ex:
|
||||
if ex.code == 403:
|
||||
warn(
|
||||
SpeckleWarning(
|
||||
"Possibly invalid token - could not authenticate Speckle Client"
|
||||
f" for server {self.url}"
|
||||
)
|
||||
)
|
||||
else:
|
||||
raise ex
|
||||
|
||||
def execute_query(self, query: str) -> Dict:
|
||||
return self.httpclient.execute(query)
|
||||
|
||||
def _init_resources(self) -> None:
|
||||
self.server = server.Resource(
|
||||
account=self.account, basepath=self.url, client=self.httpclient
|
||||
)
|
||||
server_version = None
|
||||
try:
|
||||
server_version = self.server.version()
|
||||
except Exception:
|
||||
pass
|
||||
self.user = user.Resource(
|
||||
account=self.account,
|
||||
basepath=self.url,
|
||||
client=self.httpclient,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.other_user = other_user.Resource(
|
||||
account=self.account,
|
||||
basepath=self.url,
|
||||
client=self.httpclient,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.active_user = active_user.Resource(
|
||||
account=self.account,
|
||||
basepath=self.url,
|
||||
client=self.httpclient,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.stream = stream.Resource(
|
||||
account=self.account,
|
||||
basepath=self.url,
|
||||
client=self.httpclient,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.commit = commit.Resource(
|
||||
account=self.account, basepath=self.url, client=self.httpclient
|
||||
)
|
||||
self.branch = branch.Resource(
|
||||
account=self.account, basepath=self.url, client=self.httpclient
|
||||
)
|
||||
self.object = object.Resource(
|
||||
account=self.account, basepath=self.url, client=self.httpclient
|
||||
)
|
||||
self.subscribe = subscriptions.Resource(
|
||||
account=self.account,
|
||||
basepath=self.ws_url,
|
||||
client=self.wsclient,
|
||||
)
|
||||
|
||||
def __getattr__(self, name):
|
||||
try:
|
||||
attr = getattr(resources, name)
|
||||
return attr.Resource(
|
||||
account=self.account, basepath=self.url, client=self.httpclient
|
||||
)
|
||||
except AttributeError:
|
||||
raise SpeckleException(
|
||||
f"Method {name} is not supported by the SpeckleClient class"
|
||||
)
|
||||
@@ -0,0 +1,154 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
|
||||
|
||||
from specklepy.core.api.models import ServerInfo
|
||||
from specklepy.core.helpers import speckle_path_provider
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
from specklepy.transports.sqlite import SQLiteTransport
|
||||
|
||||
|
||||
class UserInfo(BaseModel):
|
||||
name: Optional[str] = None
|
||||
email: Optional[str] = None
|
||||
company: Optional[str] = None
|
||||
id: Optional[str] = None
|
||||
|
||||
|
||||
class Account(BaseModel):
|
||||
isDefault: bool = False
|
||||
token: Optional[str] = None
|
||||
refreshToken: Optional[str] = None
|
||||
serverInfo: ServerInfo = Field(default_factory=ServerInfo)
|
||||
userInfo: UserInfo = Field(default_factory=UserInfo)
|
||||
id: Optional[str] = None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"Account(email: {self.userInfo.email}, server: {self.serverInfo.url},"
|
||||
f" isDefault: {self.isDefault})"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
@classmethod
|
||||
def from_token(cls, token: str, server_url: str = None):
|
||||
acct = cls(token=token)
|
||||
acct.serverInfo.url = server_url
|
||||
return acct
|
||||
|
||||
|
||||
def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
|
||||
"""Gets all the accounts present in this environment
|
||||
|
||||
Arguments:
|
||||
base_path {str} -- custom base path if you are not using the system default
|
||||
|
||||
Returns:
|
||||
List[Account] -- list of all local accounts or an empty list if
|
||||
no accounts were found
|
||||
"""
|
||||
accounts: List[Account] = []
|
||||
try:
|
||||
account_storage = SQLiteTransport(scope="Accounts", base_path=base_path)
|
||||
res = account_storage.get_all_objects()
|
||||
account_storage.close()
|
||||
if res:
|
||||
accounts.extend(Account.model_validate_json(r[1]) for r in res)
|
||||
except SpeckleException:
|
||||
# cannot open SQLiteTransport, probably because of the lack
|
||||
# of disk write permissions
|
||||
pass
|
||||
|
||||
json_acct_files = []
|
||||
json_path = str(speckle_path_provider.accounts_folder_path())
|
||||
try:
|
||||
os.makedirs(json_path, exist_ok=True)
|
||||
json_acct_files.extend(
|
||||
file for file in os.listdir(json_path) if file.endswith(".json")
|
||||
)
|
||||
|
||||
except Exception:
|
||||
# cannot find or get the json account paths
|
||||
pass
|
||||
|
||||
if json_acct_files:
|
||||
try:
|
||||
accounts.extend(
|
||||
Account.model_validate_json(Path(json_path, json_file).read_text())
|
||||
# Account.parse_file(os.path.join(json_path, json_file))
|
||||
for json_file in json_acct_files
|
||||
)
|
||||
except Exception as ex:
|
||||
raise SpeckleException(
|
||||
"Invalid json accounts could not be read. Please fix or remove them.",
|
||||
ex,
|
||||
) from ex
|
||||
|
||||
return accounts
|
||||
|
||||
|
||||
def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
|
||||
"""
|
||||
Gets this environment's default account if any. If there is no default,
|
||||
the first found will be returned and set as default.
|
||||
Arguments:
|
||||
base_path {str} -- custom base path if you are not using the system default
|
||||
|
||||
Returns:
|
||||
Account -- the default account or None if no local accounts were found
|
||||
"""
|
||||
accounts = get_local_accounts(base_path=base_path)
|
||||
if not accounts:
|
||||
return None
|
||||
|
||||
default = next((acc for acc in accounts if acc.isDefault), None)
|
||||
if not default:
|
||||
default = accounts[0]
|
||||
default.isDefault = True
|
||||
#metrics.initialise_tracker(default)
|
||||
|
||||
return default
|
||||
|
||||
|
||||
def get_account_from_token(token: str, server_url: str = None) -> Account:
|
||||
"""Gets the local account for the token if it exists
|
||||
Arguments:
|
||||
token {str} -- the api token
|
||||
|
||||
Returns:
|
||||
Account -- the local account with this token or a shell account containing
|
||||
just the token and url if no local account is found
|
||||
"""
|
||||
accounts = get_local_accounts()
|
||||
if not accounts:
|
||||
return Account.from_token(token, server_url)
|
||||
|
||||
acct = next((acc for acc in accounts if acc.token == token), None)
|
||||
if acct:
|
||||
return acct
|
||||
|
||||
if server_url:
|
||||
url = server_url.lower()
|
||||
acct = next(
|
||||
(acc for acc in accounts if url in acc.serverInfo.url.lower()), None
|
||||
)
|
||||
if acct:
|
||||
return acct
|
||||
|
||||
return Account.from_token(token, server_url)
|
||||
|
||||
|
||||
class StreamWrapper:
|
||||
def __init__(self, url: str = None) -> None:
|
||||
raise SpeckleException(
|
||||
message=(
|
||||
"The StreamWrapper has moved as of v2.6.0! Please import from"
|
||||
" specklepy.api.wrapper"
|
||||
),
|
||||
exception=DeprecationWarning(),
|
||||
)
|
||||
@@ -0,0 +1,116 @@
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from unicodedata import name
|
||||
|
||||
|
||||
class HostAppVersion(Enum):
|
||||
v = "v"
|
||||
v6 = "v6"
|
||||
v7 = "v7"
|
||||
v2019 = "v2019"
|
||||
v2020 = "v2020"
|
||||
v2021 = "v2021"
|
||||
v2022 = "v2022"
|
||||
v2023 = "v2023"
|
||||
v2024 = "v2024"
|
||||
v2025 = "v2025"
|
||||
vSandbox = "vSandbox"
|
||||
vRevit = "vRevit"
|
||||
vRevit2021 = "vRevit2021"
|
||||
vRevit2022 = "vRevit2022"
|
||||
vRevit2023 = "vRevit2023"
|
||||
vRevit2024 = "vRevit2024"
|
||||
vRevit2025 = "vRevit2025"
|
||||
v25 = "v25"
|
||||
v26 = "v26"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return self.value
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
@dataclass
|
||||
class HostApplication:
|
||||
name: str
|
||||
slug: str
|
||||
|
||||
def get_version(self, version: HostAppVersion) -> str:
|
||||
return f"{name.replace(' ', '')}{str(version).strip('v')}"
|
||||
|
||||
|
||||
RHINO = HostApplication("Rhino", "rhino")
|
||||
GRASSHOPPER = HostApplication("Grasshopper", "grasshopper")
|
||||
REVIT = HostApplication("Revit", "revit")
|
||||
DYNAMO = HostApplication("Dynamo", "dynamo")
|
||||
UNITY = HostApplication("Unity", "unity")
|
||||
GSA = HostApplication("GSA", "gsa")
|
||||
CIVIL = HostApplication("Civil 3D", "civil3d")
|
||||
AUTOCAD = HostApplication("AutoCAD", "autocad")
|
||||
MICROSTATION = HostApplication("MicroStation", "microstation")
|
||||
OPENROADS = HostApplication("OpenRoads", "openroads")
|
||||
OPENRAIL = HostApplication("OpenRail", "openrail")
|
||||
OPENBUILDINGS = HostApplication("OpenBuildings", "openbuildings")
|
||||
ETABS = HostApplication("ETABS", "etabs")
|
||||
SAP2000 = HostApplication("SAP2000", "sap2000")
|
||||
CSIBRIDGE = HostApplication("CSIBridge", "csibridge")
|
||||
SAFE = HostApplication("SAFE", "safe")
|
||||
TEKLASTRUCTURES = HostApplication("Tekla Structures", "teklastructures")
|
||||
DXF = HostApplication("DXF Converter", "dxf")
|
||||
EXCEL = HostApplication("Excel", "excel")
|
||||
UNREAL = HostApplication("Unreal", "unreal")
|
||||
POWERBI = HostApplication("Power BI", "powerbi")
|
||||
BLENDER = HostApplication("Blender", "blender")
|
||||
QGIS = HostApplication("QGIS", "qgis")
|
||||
ARCGIS = HostApplication("ArcGIS", "arcgis")
|
||||
SKETCHUP = HostApplication("SketchUp", "sketchup")
|
||||
ARCHICAD = HostApplication("Archicad", "archicad")
|
||||
TOPSOLID = HostApplication("TopSolid", "topsolid")
|
||||
PYTHON = HostApplication("Python", "python")
|
||||
NET = HostApplication(".NET", "net")
|
||||
OTHER = HostApplication("Other", "other")
|
||||
|
||||
_app_name_host_app_mapping = {
|
||||
"dynamo": DYNAMO,
|
||||
"revit": REVIT,
|
||||
"autocad": AUTOCAD,
|
||||
"civil": CIVIL,
|
||||
"rhino": RHINO,
|
||||
"grasshopper": GRASSHOPPER,
|
||||
"unity": UNITY,
|
||||
"gsa": GSA,
|
||||
"microstation": MICROSTATION,
|
||||
"openroads": OPENROADS,
|
||||
"openrail": OPENRAIL,
|
||||
"openbuildings": OPENBUILDINGS,
|
||||
"etabs": ETABS,
|
||||
"sap": SAP2000,
|
||||
"csibridge": CSIBRIDGE,
|
||||
"safe": SAFE,
|
||||
"teklastructures": TEKLASTRUCTURES,
|
||||
"dxf": DXF,
|
||||
"excel": EXCEL,
|
||||
"unreal": UNREAL,
|
||||
"powerbi": POWERBI,
|
||||
"blender": BLENDER,
|
||||
"qgis": QGIS,
|
||||
"arcgis": ARCGIS,
|
||||
"sketchup": SKETCHUP,
|
||||
"archicad": ARCHICAD,
|
||||
"topsolid": TOPSOLID,
|
||||
"python": PYTHON,
|
||||
"net": NET,
|
||||
}
|
||||
|
||||
|
||||
def get_host_app_from_string(app_name: str) -> HostApplication:
|
||||
app_name = app_name.lower().replace(" ", "")
|
||||
for partial_app_name, host_app in _app_name_host_app_mapping.items():
|
||||
if partial_app_name in app_name:
|
||||
return host_app
|
||||
return HostApplication(app_name, app_name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(HostAppVersion.v)
|
||||
@@ -0,0 +1,198 @@
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class Collaborator(BaseModel):
|
||||
id: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
role: Optional[str] = None
|
||||
avatar: Optional[str] = None
|
||||
|
||||
|
||||
class Commit(BaseModel):
|
||||
id: Optional[str] = None
|
||||
message: Optional[str] = None
|
||||
authorName: Optional[str] = None
|
||||
authorId: Optional[str] = None
|
||||
authorAvatar: Optional[str] = None
|
||||
branchName: Optional[str] = None
|
||||
createdAt: Optional[datetime] = None
|
||||
sourceApplication: Optional[str] = None
|
||||
referencedObject: Optional[str] = None
|
||||
totalChildrenCount: Optional[int] = None
|
||||
parents: Optional[List[str]] = None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"Commit( id: {self.id}, message: {self.message}, referencedObject:"
|
||||
f" {self.referencedObject}, authorName: {self.authorName}, branchName:"
|
||||
f" {self.branchName}, createdAt: {self.createdAt} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class Commits(BaseModel):
|
||||
totalCount: Optional[int] = None
|
||||
cursor: Optional[datetime] = None
|
||||
items: List[Commit] = []
|
||||
|
||||
|
||||
class Object(BaseModel):
|
||||
id: Optional[str] = None
|
||||
speckleType: Optional[str] = None
|
||||
applicationId: Optional[str] = None
|
||||
totalChildrenCount: Optional[int] = None
|
||||
createdAt: Optional[datetime] = None
|
||||
|
||||
|
||||
class Branch(BaseModel):
|
||||
id: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
commits: Optional[Commits] = None
|
||||
|
||||
|
||||
class Branches(BaseModel):
|
||||
totalCount: Optional[int] = None
|
||||
cursor: Optional[datetime] = None
|
||||
items: List[Branch] = []
|
||||
|
||||
|
||||
class Stream(BaseModel):
|
||||
id: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
role: Optional[str] = None
|
||||
isPublic: Optional[bool] = None
|
||||
description: Optional[str] = None
|
||||
createdAt: Optional[datetime] = None
|
||||
updatedAt: Optional[datetime] = None
|
||||
collaborators: List[Collaborator] = Field(default_factory=list)
|
||||
branches: Optional[Branches] = None
|
||||
commit: Optional[Commit] = None
|
||||
object: Optional[Object] = None
|
||||
commentCount: Optional[int] = None
|
||||
favoritedDate: Optional[datetime] = None
|
||||
favoritesCount: Optional[int] = None
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"Stream( id: {self.id}, name: {self.name}, description:"
|
||||
f" {self.description}, isPublic: {self.isPublic})"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class Streams(BaseModel):
|
||||
totalCount: Optional[int] = None
|
||||
cursor: Optional[datetime] = None
|
||||
items: List[Stream] = []
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
id: Optional[str] = None
|
||||
email: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
bio: Optional[str] = None
|
||||
company: Optional[str] = None
|
||||
avatar: Optional[str] = None
|
||||
verified: Optional[bool] = None
|
||||
role: Optional[str] = None
|
||||
streams: Optional[Streams] = None
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"User( id: {self.id}, name: {self.name}, email: {self.email}, company:"
|
||||
f" {self.company} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class LimitedUser(BaseModel):
|
||||
"""Limited user type, for showing public info about a user to another user."""
|
||||
|
||||
id: str
|
||||
name: Optional[str] = None
|
||||
bio: Optional[str] = None
|
||||
company: Optional[str] = None
|
||||
avatar: Optional[str] = None
|
||||
verified: Optional[bool] = None
|
||||
role: Optional[str] = None
|
||||
|
||||
|
||||
class PendingStreamCollaborator(BaseModel):
|
||||
id: Optional[str] = None
|
||||
inviteId: Optional[str] = None
|
||||
streamId: Optional[str] = None
|
||||
streamName: Optional[str] = None
|
||||
title: Optional[str] = None
|
||||
role: Optional[str] = None
|
||||
invitedBy: Optional[User] = None
|
||||
user: Optional[User] = None
|
||||
token: Optional[str] = None
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"PendingStreamCollaborator( inviteId: {self.inviteId}, streamId:"
|
||||
f" {self.streamId}, role: {self.role}, title: {self.title}, invitedBy:"
|
||||
f" {self.user.name if self.user else None})"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class Activity(BaseModel):
|
||||
actionType: Optional[str] = None
|
||||
info: Optional[dict] = None
|
||||
userId: Optional[str] = None
|
||||
streamId: Optional[str] = None
|
||||
resourceId: Optional[str] = None
|
||||
resourceType: Optional[str] = None
|
||||
message: Optional[str] = None
|
||||
time: Optional[datetime] = None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"Activity( streamId: {self.streamId}, actionType: {self.actionType},"
|
||||
f" message: {self.message}, userId: {self.userId} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class ActivityCollection(BaseModel):
|
||||
totalCount: Optional[int] = None
|
||||
items: Optional[List[Activity]] = None
|
||||
cursor: Optional[datetime] = None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"ActivityCollection( totalCount: {self.totalCount}, items:"
|
||||
f" {len(self.items) if self.items else 0}, cursor:"
|
||||
f" {self.cursor.isoformat() if self.cursor else None} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class ServerInfo(BaseModel):
|
||||
name: Optional[str] = None
|
||||
company: Optional[str] = None
|
||||
url: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
adminContact: Optional[str] = None
|
||||
canonicalUrl: Optional[str] = None
|
||||
roles: Optional[List[dict]] = None
|
||||
scopes: Optional[List[dict]] = None
|
||||
authStrategies: Optional[List[dict]] = None
|
||||
version: Optional[str] = None
|
||||
@@ -0,0 +1,139 @@
|
||||
from typing import List, Optional
|
||||
|
||||
#from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
from specklepy.objects.base import Base
|
||||
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
|
||||
from specklepy.transports.abstract_transport import AbstractTransport
|
||||
from specklepy.transports.sqlite import SQLiteTransport
|
||||
|
||||
|
||||
def send(
|
||||
base: Base,
|
||||
transports: Optional[List[AbstractTransport]] = None,
|
||||
use_default_cache: bool = True,
|
||||
):
|
||||
"""Sends an object via the provided transports. Defaults to the local cache.
|
||||
|
||||
Arguments:
|
||||
obj {Base} -- the object you want to send
|
||||
transports {list} -- where you want to send them
|
||||
use_default_cache {bool} -- toggle for the default cache.
|
||||
If set to false, it will only send to the provided transports
|
||||
|
||||
Returns:
|
||||
str -- the object id of the sent object
|
||||
"""
|
||||
|
||||
if not transports and not use_default_cache:
|
||||
raise SpeckleException(
|
||||
message=(
|
||||
"You need to provide at least one transport: cannot send with an empty"
|
||||
" transport list and no default cache"
|
||||
)
|
||||
)
|
||||
|
||||
if isinstance(transports, AbstractTransport):
|
||||
transports = [transports]
|
||||
|
||||
if transports is None:
|
||||
transports = []
|
||||
|
||||
if use_default_cache:
|
||||
transports.insert(0, SQLiteTransport())
|
||||
|
||||
serializer = BaseObjectSerializer(write_transports=transports)
|
||||
|
||||
obj_hash, _ = serializer.write_json(base=base)
|
||||
|
||||
return obj_hash
|
||||
|
||||
|
||||
def receive(
|
||||
obj_id: str,
|
||||
remote_transport: Optional[AbstractTransport] = None,
|
||||
local_transport: Optional[AbstractTransport] = None,
|
||||
) -> Base:
|
||||
"""Receives an object from a transport.
|
||||
|
||||
Arguments:
|
||||
obj_id {str} -- the id of the object to receive
|
||||
remote_transport {Transport} -- the transport to receive from
|
||||
local_transport {Transport} -- the local cache to check for existing objects
|
||||
(defaults to `SQLiteTransport`)
|
||||
|
||||
Returns:
|
||||
Base -- the base object
|
||||
"""
|
||||
if not local_transport:
|
||||
local_transport = SQLiteTransport()
|
||||
|
||||
serializer = BaseObjectSerializer(read_transport=local_transport)
|
||||
|
||||
# try local transport first. if the parent is there, we assume all the children are there and continue with deserialization using the local transport
|
||||
obj_string = local_transport.get_object(obj_id)
|
||||
if obj_string:
|
||||
return serializer.read_json(obj_string=obj_string)
|
||||
|
||||
if not remote_transport:
|
||||
raise SpeckleException(
|
||||
message=(
|
||||
"Could not find the specified object using the local transport, and you"
|
||||
" didn't provide a fallback remote from which to pull it."
|
||||
)
|
||||
)
|
||||
|
||||
obj_string = remote_transport.copy_object_and_children(
|
||||
id=obj_id, target_transport=local_transport
|
||||
)
|
||||
|
||||
return serializer.read_json(obj_string=obj_string)
|
||||
|
||||
|
||||
def serialize(base: Base, write_transports: List[AbstractTransport] = []) -> str:
|
||||
"""
|
||||
Serialize a base object. If no write transports are provided,
|
||||
the object will be serialized
|
||||
without detaching or chunking any of the attributes.
|
||||
|
||||
Arguments:
|
||||
base {Base} -- the object to serialize
|
||||
write_transports {List[AbstractTransport]}
|
||||
-- optional: the transports to write to
|
||||
|
||||
Returns:
|
||||
str -- the serialized object
|
||||
"""
|
||||
serializer = BaseObjectSerializer(write_transports=write_transports)
|
||||
|
||||
return serializer.write_json(base)[1]
|
||||
|
||||
|
||||
def deserialize(
|
||||
obj_string: str, read_transport: Optional[AbstractTransport] = None
|
||||
) -> Base:
|
||||
"""
|
||||
Deserialize a string object into a Base object.
|
||||
|
||||
If the object contains referenced child objects that are not stored in the local db,
|
||||
a read transport needs to be provided in order to recompose
|
||||
the base with the children objects.
|
||||
|
||||
Arguments:
|
||||
obj_string {str} -- the string object to deserialize
|
||||
read_transport {AbstractTransport}
|
||||
-- the transport to fetch children objects from
|
||||
(defaults to SQLiteTransport)
|
||||
|
||||
Returns:
|
||||
Base -- the deserialized object
|
||||
"""
|
||||
if not read_transport:
|
||||
read_transport = SQLiteTransport()
|
||||
|
||||
serializer = BaseObjectSerializer(read_transport=read_transport)
|
||||
|
||||
return serializer.read_json(obj_string=obj_string)
|
||||
|
||||
|
||||
__all__ = ["receive", "send", "serialize", "deserialize"]
|
||||
@@ -0,0 +1,131 @@
|
||||
from threading import Lock
|
||||
from typing import Any, Dict, List, Optional, Tuple, Type, Union
|
||||
|
||||
from gql.client import Client
|
||||
from gql.transport.exceptions import TransportQueryError
|
||||
from graphql import DocumentNode
|
||||
|
||||
from specklepy.core.api.credentials import Account
|
||||
from specklepy.logging.exceptions import (
|
||||
GraphQLException,
|
||||
SpeckleException,
|
||||
UnsupportedException,
|
||||
)
|
||||
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
|
||||
from specklepy.transports.sqlite import SQLiteTransport
|
||||
|
||||
|
||||
class ResourceBase(object):
|
||||
def __init__(
|
||||
self,
|
||||
account: Account,
|
||||
basepath: str,
|
||||
client: Client,
|
||||
name: str,
|
||||
server_version: Optional[Tuple[Any, ...]] = None,
|
||||
) -> None:
|
||||
self.account = account
|
||||
self.basepath = basepath
|
||||
self.client = client
|
||||
self.name = name
|
||||
self.server_version = server_version
|
||||
self.schema: Optional[Type] = None
|
||||
self.__lock = Lock()
|
||||
|
||||
def _step_into_response(self, response: dict, return_type: Union[str, List, None]):
|
||||
"""Step into the dict to get the relevant data"""
|
||||
if return_type is None:
|
||||
return response
|
||||
if isinstance(return_type, str):
|
||||
return response[return_type]
|
||||
if isinstance(return_type, List):
|
||||
for key in return_type:
|
||||
response = response[key]
|
||||
return response
|
||||
|
||||
def _parse_response(self, response: Union[dict, list, None], schema=None):
|
||||
"""Try to create a class instance from the response"""
|
||||
if response is None:
|
||||
return None
|
||||
if isinstance(response, list):
|
||||
return [self._parse_response(response=r, schema=schema) for r in response]
|
||||
if schema:
|
||||
return schema.model_validate(response)
|
||||
elif self.schema:
|
||||
try:
|
||||
return self.schema.model_validate(response)
|
||||
except Exception:
|
||||
s = BaseObjectSerializer(read_transport=SQLiteTransport())
|
||||
return s.recompose_base(response)
|
||||
else:
|
||||
return response
|
||||
|
||||
def make_request(
|
||||
self,
|
||||
query: DocumentNode,
|
||||
params: Optional[Dict] = None,
|
||||
return_type: Union[str, List, None] = None,
|
||||
schema=None,
|
||||
parse_response: bool = True,
|
||||
) -> Any:
|
||||
"""Executes the GraphQL query"""
|
||||
try:
|
||||
with self.__lock:
|
||||
response = self.client.execute(query, variable_values=params)
|
||||
except Exception as ex:
|
||||
if isinstance(ex, TransportQueryError):
|
||||
return GraphQLException(
|
||||
message=(
|
||||
f"Failed to execute the GraphQL {self.name} request. Errors:"
|
||||
f" {ex.errors}"
|
||||
),
|
||||
errors=ex.errors,
|
||||
data=ex.data,
|
||||
)
|
||||
else:
|
||||
return SpeckleException(
|
||||
message=(
|
||||
f"Failed to execute the GraphQL {self.name} request. Inner"
|
||||
f" exception: {ex}"
|
||||
),
|
||||
exception=ex,
|
||||
)
|
||||
|
||||
response = self._step_into_response(response=response, return_type=return_type)
|
||||
|
||||
if parse_response:
|
||||
return self._parse_response(response=response, schema=schema)
|
||||
else:
|
||||
return response
|
||||
|
||||
def _check_server_version_at_least(
|
||||
self, target_version: Tuple[Any, ...], unsupported_message: Optional[str] = None
|
||||
):
|
||||
"""Use this check to guard against making unsupported requests on older servers.
|
||||
|
||||
Arguments:
|
||||
target_version {tuple}
|
||||
the minimum server version in the format (major, minor, patch, (tag, build))
|
||||
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
|
||||
"""
|
||||
if not unsupported_message:
|
||||
unsupported_message = (
|
||||
"The client method used is not supported on Speckle Server versions"
|
||||
f" prior to v{'.'.join(target_version)}"
|
||||
)
|
||||
# if version is dev, it should be supported... (or not)
|
||||
if self.server_version == ("dev",):
|
||||
return
|
||||
if self.server_version and self.server_version < target_version:
|
||||
raise UnsupportedException(unsupported_message)
|
||||
|
||||
def _check_invites_supported(self):
|
||||
"""Invites are only supported for Speckle Server >= 2.6.4.
|
||||
Use this check to guard against making unsupported requests on older servers.
|
||||
"""
|
||||
self._check_server_version_at_least(
|
||||
(2, 6, 4),
|
||||
"Stream invites are only supported as of Speckle Server v2.6.4. Please"
|
||||
" update your Speckle Server to use this method or use the"
|
||||
" `grant_permission` flow instead.",
|
||||
)
|
||||
@@ -0,0 +1,264 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import List, Optional
|
||||
|
||||
from gql import gql
|
||||
|
||||
from specklepy.core.api.models import ActivityCollection, PendingStreamCollaborator, User
|
||||
from specklepy.core.api.resource import ResourceBase
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
NAME = "active_user"
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
"""API Access class for users"""
|
||||
|
||||
def __init__(self, account, basepath, client, server_version) -> None:
|
||||
super().__init__(
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.schema = User
|
||||
|
||||
def get(self) -> User:
|
||||
"""Gets the profile of a user. If no id argument is provided,
|
||||
will return the current authenticated user's profile
|
||||
(as extracted from the authorization header).
|
||||
|
||||
Arguments:
|
||||
id {str} -- the user id
|
||||
|
||||
Returns:
|
||||
User -- the retrieved user
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query User {
|
||||
activeUser {
|
||||
id
|
||||
email
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
profiles
|
||||
role
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {}
|
||||
|
||||
return self.make_request(query=query, params=params, return_type="activeUser")
|
||||
|
||||
def update(
|
||||
self,
|
||||
name: Optional[str] = None,
|
||||
company: Optional[str] = None,
|
||||
bio: Optional[str] = None,
|
||||
avatar: Optional[str] = None,
|
||||
):
|
||||
"""Updates your user profile. All arguments are optional.
|
||||
|
||||
Arguments:
|
||||
name {str} -- your name
|
||||
company {str} -- the company you may or may not work for
|
||||
bio {str} -- tell us about yourself
|
||||
avatar {str} -- a nice photo of yourself
|
||||
|
||||
Returns @deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT):
|
||||
bool -- True if your profile was updated successfully
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation UserUpdate($user: UserUpdateInput!) {
|
||||
userUpdate(user: $user)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"name": name, "company": company, "bio": bio, "avatar": avatar}
|
||||
|
||||
params = {"user": {k: v for k, v in params.items() if v is not None}}
|
||||
|
||||
if not params["user"]:
|
||||
return SpeckleException(
|
||||
message=(
|
||||
"You must provide at least one field to update your user profile"
|
||||
)
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="userUpdate", parse_response=False
|
||||
)
|
||||
|
||||
def activity(
|
||||
self,
|
||||
limit: int = 20,
|
||||
action_type: Optional[str] = None,
|
||||
before: Optional[datetime] = None,
|
||||
after: Optional[datetime] = None,
|
||||
cursor: Optional[datetime] = None,
|
||||
):
|
||||
"""
|
||||
Get the activity from a given stream in an Activity collection.
|
||||
Step into the activity `items` for the list of activity.
|
||||
If no id argument is provided, will return the current authenticated user's
|
||||
activity (as extracted from the authorization header).
|
||||
|
||||
Note: all timestamps arguments should be `datetime` of any tz as they will be
|
||||
converted to UTC ISO format strings
|
||||
|
||||
user_id {str} -- the id of the user to get the activity from
|
||||
action_type {str} -- filter results to a single action type
|
||||
(eg: `commit_create` or `commit_receive`)
|
||||
limit {int} -- max number of Activity items to return
|
||||
before {datetime} -- latest cutoff for activity
|
||||
(ie: return all activity _before_ this time)
|
||||
after {datetime} -- oldest cutoff for activity
|
||||
(ie: return all activity _after_ this time)
|
||||
cursor {datetime} -- timestamp cursor for pagination
|
||||
"""
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query UserActivity(
|
||||
$action_type: String,
|
||||
$before:DateTime,
|
||||
$after: DateTime,
|
||||
$cursor: DateTime,
|
||||
$limit: Int
|
||||
){
|
||||
activeUser {
|
||||
activity(
|
||||
actionType: $action_type,
|
||||
before: $before,
|
||||
after: $after,
|
||||
cursor: $cursor,
|
||||
limit: $limit
|
||||
) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
actionType
|
||||
info
|
||||
userId
|
||||
streamId
|
||||
resourceId
|
||||
resourceType
|
||||
message
|
||||
time
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"limit": limit,
|
||||
"action_type": action_type,
|
||||
"before": before.astimezone(timezone.utc).isoformat() if before else before,
|
||||
"after": after.astimezone(timezone.utc).isoformat() if after else after,
|
||||
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["activeUser", "activity"],
|
||||
schema=ActivityCollection,
|
||||
)
|
||||
|
||||
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
|
||||
"""Get all of the active user's pending stream invites
|
||||
|
||||
Requires Speckle Server version >= 2.6.4
|
||||
|
||||
Returns:
|
||||
List[PendingStreamCollaborator]
|
||||
-- a list of pending invites for the current user
|
||||
"""
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvites {
|
||||
streamInvites{
|
||||
id
|
||||
token
|
||||
inviteId
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
return_type="streamInvites",
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
|
||||
def get_pending_invite(
|
||||
self, stream_id: str, token: Optional[str] = None
|
||||
) -> Optional[PendingStreamCollaborator]:
|
||||
"""Get a particular pending invite for the active user on a given stream.
|
||||
If no invite_id is provided, any valid invite will be returned.
|
||||
|
||||
Requires Speckle Server version >= 2.6.4
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to look for invites on
|
||||
token {str} -- the token of the invite to look for (optional)
|
||||
|
||||
Returns:
|
||||
PendingStreamCollaborator
|
||||
-- the invite for the given stream (or None if it isn't found)
|
||||
"""
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvite($streamId: String!, $token: String) {
|
||||
streamInvite(streamId: $streamId, token: $token) {
|
||||
id
|
||||
token
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"streamId": stream_id}
|
||||
if token:
|
||||
params["token"] = token
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInvite",
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
@@ -0,0 +1,219 @@
|
||||
from typing import Optional
|
||||
|
||||
from gql import gql
|
||||
|
||||
from specklepy.core.api.models import Branch
|
||||
from specklepy.core.api.resource import ResourceBase
|
||||
|
||||
NAME = "branch"
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
"""API Access class for branches"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
super().__init__(
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
self.schema = Branch
|
||||
|
||||
def create(
|
||||
self, stream_id: str, name: str, description: str = "No description provided"
|
||||
) -> str:
|
||||
"""Create a new branch on this stream
|
||||
|
||||
Arguments:
|
||||
name {str} -- the name of the new branch
|
||||
description {str} -- a short description of the branch
|
||||
|
||||
Returns:
|
||||
id {str} -- the newly created branch's id
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation BranchCreate($branch: BranchCreateInput!) {
|
||||
branchCreate(branch: $branch)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"branch": {
|
||||
"streamId": stream_id,
|
||||
"name": name,
|
||||
"description": description,
|
||||
}
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="branchCreate", parse_response=False
|
||||
)
|
||||
|
||||
def get(self, stream_id: str, name: str, commits_limit: int = 10):
|
||||
"""Get a branch by name from a stream
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to get the branch from
|
||||
name {str} -- the name of the branch to get
|
||||
commits_limit {int} -- maximum number of commits to get
|
||||
|
||||
Returns:
|
||||
Branch -- the fetched branch with its latest commits
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query BranchGet($stream_id: String!, $name: String!, $commits_limit: Int!) {
|
||||
stream(id: $stream_id) {
|
||||
branch(name: $name) {
|
||||
id,
|
||||
name,
|
||||
description,
|
||||
commits (limit: $commits_limit) {
|
||||
totalCount,
|
||||
cursor,
|
||||
items {
|
||||
id,
|
||||
referencedObject,
|
||||
sourceApplication,
|
||||
totalChildrenCount,
|
||||
message,
|
||||
authorName,
|
||||
authorId,
|
||||
branchName,
|
||||
parents,
|
||||
createdAt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"stream_id": stream_id, "name": name, "commits_limit": commits_limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["stream", "branch"]
|
||||
)
|
||||
|
||||
def list(self, stream_id: str, branches_limit: int = 10, commits_limit: int = 10):
|
||||
"""Get a list of branches from a given stream
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to get the branches from
|
||||
branches_limit {int} -- maximum number of branches to get
|
||||
commits_limit {int} -- maximum number of commits to get
|
||||
|
||||
Returns:
|
||||
List[Branch] -- the branches on the stream
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query BranchesGet(
|
||||
$stream_id: String!,
|
||||
$branches_limit: Int!,
|
||||
$commits_limit: Int!
|
||||
) {
|
||||
stream(id: $stream_id) {
|
||||
branches(limit: $branches_limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
description
|
||||
commits(limit: $commits_limit) {
|
||||
totalCount
|
||||
items{
|
||||
id
|
||||
message
|
||||
referencedObject
|
||||
sourceApplication
|
||||
parents
|
||||
authorId
|
||||
authorName
|
||||
branchName
|
||||
createdAt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"stream_id": stream_id,
|
||||
"branches_limit": branches_limit,
|
||||
"commits_limit": commits_limit,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["stream", "branches", "items"]
|
||||
)
|
||||
|
||||
def update(
|
||||
self,
|
||||
stream_id: str,
|
||||
branch_id: str,
|
||||
name: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
):
|
||||
"""Update a branch
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream containing the branch to update
|
||||
branch_id {str} -- the id of the branch to update
|
||||
name {str} -- optional: the updated branch name
|
||||
description {str} -- optional: the updated branch description
|
||||
|
||||
Returns:
|
||||
bool -- True if update is successful
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation BranchUpdate($branch: BranchUpdateInput!) {
|
||||
branchUpdate(branch: $branch)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"branch": {
|
||||
"streamId": stream_id,
|
||||
"id": branch_id,
|
||||
}
|
||||
}
|
||||
|
||||
if name:
|
||||
params["branch"]["name"] = name
|
||||
if description:
|
||||
params["branch"]["description"] = description
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="branchUpdate", parse_response=False
|
||||
)
|
||||
|
||||
def delete(self, stream_id: str, branch_id: str):
|
||||
"""Delete a branch
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream containing the branch to delete
|
||||
branch_id {str} -- the branch to delete
|
||||
|
||||
Returns:
|
||||
bool -- True if deletion is successful
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation BranchDelete($branch: BranchDeleteInput!) {
|
||||
branchDelete(branch: $branch)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"branch": {"streamId": stream_id, "id": branch_id}}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="branchDelete", parse_response=False
|
||||
)
|
||||
@@ -0,0 +1,237 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from gql import gql
|
||||
|
||||
from specklepy.core.api.models import Commit
|
||||
from specklepy.core.api.resource import ResourceBase
|
||||
|
||||
NAME = "commit"
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
"""API Access class for commits"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
super().__init__(
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
self.schema = Commit
|
||||
|
||||
def get(self, stream_id: str, commit_id: str) -> Commit:
|
||||
"""
|
||||
Gets a commit given a stream and the commit id
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the stream where we can find the commit
|
||||
commit_id {str} -- the id of the commit you want to get
|
||||
|
||||
Returns:
|
||||
Commit -- the retrieved commit object
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query Commit($stream_id: String!, $commit_id: String!) {
|
||||
stream(id: $stream_id) {
|
||||
commit(id: $commit_id) {
|
||||
id
|
||||
message
|
||||
referencedObject
|
||||
authorId
|
||||
authorName
|
||||
authorAvatar
|
||||
branchName
|
||||
createdAt
|
||||
sourceApplication
|
||||
totalChildrenCount
|
||||
parents
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"stream_id": stream_id, "commit_id": commit_id}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["stream", "commit"]
|
||||
)
|
||||
|
||||
def list(self, stream_id: str, limit: int = 10) -> List[Commit]:
|
||||
"""
|
||||
Get a list of commits on a given stream
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the stream where the commits are
|
||||
limit {int} -- the maximum number of commits to fetch (default = 10)
|
||||
|
||||
Returns:
|
||||
List[Commit] -- a list of the most recent commit objects
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query Commits($stream_id: String!, $limit: Int!) {
|
||||
stream(id: $stream_id) {
|
||||
commits(limit: $limit) {
|
||||
items {
|
||||
id
|
||||
message
|
||||
referencedObject
|
||||
authorName
|
||||
authorId
|
||||
authorName
|
||||
authorAvatar
|
||||
branchName
|
||||
createdAt
|
||||
sourceApplication
|
||||
totalChildrenCount
|
||||
parents
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"stream_id": stream_id, "limit": limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["stream", "commits", "items"]
|
||||
)
|
||||
|
||||
def create(
|
||||
self,
|
||||
stream_id: str,
|
||||
object_id: str,
|
||||
branch_name: str = "main",
|
||||
message: str = "",
|
||||
source_application: str = "python",
|
||||
parents: List[str] = None,
|
||||
) -> str:
|
||||
"""
|
||||
Creates a commit on a branch
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the stream you want to commit to
|
||||
object_id {str} -- the hash of your commit object
|
||||
branch_name {str}
|
||||
-- the name of the branch to commit to (defaults to "main")
|
||||
message {str}
|
||||
-- optional: a message to give more information about the commit
|
||||
source_application{str}
|
||||
-- optional: the application from which the commit was created
|
||||
(defaults to "python")
|
||||
parents {List[str]} -- optional: the id of the parent commits
|
||||
|
||||
Returns:
|
||||
str -- the id of the created commit
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation CommitCreate ($commit: CommitCreateInput!)
|
||||
{ commitCreate(commit: $commit)}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"commit": {
|
||||
"streamId": stream_id,
|
||||
"branchName": branch_name,
|
||||
"objectId": object_id,
|
||||
"message": message,
|
||||
"sourceApplication": source_application,
|
||||
}
|
||||
}
|
||||
if parents:
|
||||
params["commit"]["parents"] = parents
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="commitCreate", parse_response=False
|
||||
)
|
||||
|
||||
def update(self, stream_id: str, commit_id: str, message: str) -> bool:
|
||||
"""
|
||||
Update a commit
|
||||
|
||||
Arguments:
|
||||
stream_id {str}
|
||||
-- the id of the stream that contains the commit you'd like to update
|
||||
commit_id {str} -- the id of the commit you'd like to update
|
||||
message {str} -- the updated commit message
|
||||
|
||||
Returns:
|
||||
bool -- True if the operation succeeded
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation CommitUpdate($commit: CommitUpdateInput!)
|
||||
{ commitUpdate(commit: $commit)}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"commit": {"streamId": stream_id, "id": commit_id, "message": message}
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="commitUpdate", parse_response=False
|
||||
)
|
||||
|
||||
def delete(self, stream_id: str, commit_id: str) -> bool:
|
||||
"""
|
||||
Delete a commit
|
||||
|
||||
Arguments:
|
||||
stream_id {str}
|
||||
-- the id of the stream that contains the commit you'd like to delete
|
||||
commit_id {str} -- the id of the commit you'd like to delete
|
||||
|
||||
Returns:
|
||||
bool -- True if the operation succeeded
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation CommitDelete($commit: CommitDeleteInput!)
|
||||
{ commitDelete(commit: $commit)}
|
||||
"""
|
||||
)
|
||||
params = {"commit": {"streamId": stream_id, "id": commit_id}}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="commitDelete", parse_response=False
|
||||
)
|
||||
|
||||
def received(
|
||||
self,
|
||||
stream_id: str,
|
||||
commit_id: str,
|
||||
source_application: str = "python",
|
||||
message: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Mark a commit object a received by the source application.
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation CommitReceive($receivedInput:CommitReceivedInput!){
|
||||
commitReceive(input:$receivedInput)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {
|
||||
"receivedInput": {
|
||||
"sourceApplication": source_application,
|
||||
"streamId": stream_id,
|
||||
"commitId": commit_id,
|
||||
"message": "message",
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="commitReceive",
|
||||
parse_response=False,
|
||||
)
|
||||
except Exception as ex:
|
||||
print(ex.with_traceback)
|
||||
return False
|
||||
@@ -0,0 +1,92 @@
|
||||
from typing import Dict, List
|
||||
|
||||
from gql import gql
|
||||
|
||||
from specklepy.core.api.resource import ResourceBase
|
||||
from specklepy.objects.base import Base
|
||||
|
||||
NAME = "object"
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
"""API Access class for objects"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
super().__init__(
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
self.schema = Base
|
||||
|
||||
def get(self, stream_id: str, object_id: str) -> Base:
|
||||
"""
|
||||
Get a stream object
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream for the object
|
||||
object_id {str} -- the hash of the object you want to get
|
||||
|
||||
Returns:
|
||||
Base -- the returned Base object
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query Object($stream_id: String!, $object_id: String!) {
|
||||
stream(id: $stream_id) {
|
||||
id
|
||||
name
|
||||
object(id: $object_id) {
|
||||
id
|
||||
speckleType
|
||||
applicationId
|
||||
createdAt
|
||||
totalChildrenCount
|
||||
data
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"stream_id": stream_id, "object_id": object_id}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["stream", "object", "data"],
|
||||
)
|
||||
|
||||
def create(self, stream_id: str, objects: List[Dict]) -> str:
|
||||
"""
|
||||
Not advised - generally, you want to use `operations.send()`.
|
||||
|
||||
Create a new object on a stream.
|
||||
To send a base object, you can prepare it by running it through the
|
||||
`BaseObjectSerializer.traverse_base()` function to get a valid (serialisable)
|
||||
object to send.
|
||||
|
||||
NOTE: this does not create a commit - you can create one with
|
||||
`SpeckleClient.commit.create`.
|
||||
Dynamic fields will be located in the 'data' dict of the received `Base` object
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream you want to send the object to
|
||||
objects {List[Dict]}
|
||||
-- a list of base dictionary objects (NOTE: must be json serialisable)
|
||||
|
||||
Returns:
|
||||
str -- the id of the object
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation ObjectCreate($object_input: ObjectCreateInput!) {
|
||||
objectCreate(objectInput: $object_input)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"object_input": {"streamId": stream_id, "objects": objects}}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="objectCreate", parse_response=False
|
||||
)
|
||||
@@ -0,0 +1,172 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from gql import gql
|
||||
|
||||
from specklepy.core.api.models import ActivityCollection, LimitedUser
|
||||
from specklepy.core.api.resource import ResourceBase
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
NAME = "other_user"
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
"""API Access class for other users, that are not the currently active user."""
|
||||
|
||||
def __init__(self, account, basepath, client, server_version) -> None:
|
||||
super().__init__(
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.schema = LimitedUser
|
||||
|
||||
def get(self, id: str) -> LimitedUser:
|
||||
"""
|
||||
Gets the profile of another user.
|
||||
|
||||
Arguments:
|
||||
id {str} -- the user id
|
||||
|
||||
Returns:
|
||||
LimitedUser -- the retrieved profile of another user
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query OtherUser($id: String!) {
|
||||
otherUser(id: $id) {
|
||||
id
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
role
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"id": id}
|
||||
|
||||
return self.make_request(query=query, params=params, return_type="otherUser")
|
||||
|
||||
def search(
|
||||
self, search_query: str, limit: int = 25
|
||||
) -> Union[List[LimitedUser], SpeckleException]:
|
||||
"""Searches for user by name or email. The search query must be at least
|
||||
3 characters long
|
||||
|
||||
Arguments:
|
||||
search_query {str} -- a string to search for
|
||||
limit {int} -- the maximum number of results to return
|
||||
Returns:
|
||||
List[LimitedUser] -- a list of User objects that match the search query
|
||||
"""
|
||||
if len(search_query) < 3:
|
||||
return SpeckleException(
|
||||
message="User search query must be at least 3 characters"
|
||||
)
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query UserSearch($search_query: String!, $limit: Int!) {
|
||||
userSearch(query: $search_query, limit: $limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"search_query": search_query, "limit": limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["userSearch", "items"]
|
||||
)
|
||||
|
||||
def activity(
|
||||
self,
|
||||
user_id: str,
|
||||
limit: int = 20,
|
||||
action_type: Optional[str] = None,
|
||||
before: Optional[datetime] = None,
|
||||
after: Optional[datetime] = None,
|
||||
cursor: Optional[datetime] = None,
|
||||
) -> ActivityCollection:
|
||||
"""
|
||||
Get the activity from a given stream in an Activity collection.
|
||||
Step into the activity `items` for the list of activity.
|
||||
|
||||
Note: all timestamps arguments should be `datetime` of
|
||||
any tz as they will be converted to UTC ISO format strings
|
||||
|
||||
user_id {str} -- the id of the user to get the activity from
|
||||
action_type {str} -- filter results to a single action type
|
||||
(eg: `commit_create` or `commit_receive`)
|
||||
limit {int} -- max number of Activity items to return
|
||||
before {datetime} -- latest cutoff for activity
|
||||
(ie: return all activity _before_ this time)
|
||||
after {datetime} -- oldest cutoff for activity
|
||||
(ie: return all activity _after_ this time)
|
||||
cursor {datetime} -- timestamp cursor for pagination
|
||||
"""
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query UserActivity(
|
||||
$user_id: String!,
|
||||
$action_type: String,
|
||||
$before:DateTime,
|
||||
$after: DateTime,
|
||||
$cursor: DateTime,
|
||||
$limit: Int
|
||||
){
|
||||
otherUser(id: $user_id) {
|
||||
activity(
|
||||
actionType: $action_type,
|
||||
before: $before,
|
||||
after: $after,
|
||||
cursor: $cursor,
|
||||
limit: $limit
|
||||
) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
actionType
|
||||
info
|
||||
userId
|
||||
streamId
|
||||
resourceId
|
||||
resourceType
|
||||
message
|
||||
time
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"user_id": user_id,
|
||||
"limit": limit,
|
||||
"action_type": action_type,
|
||||
"before": before.astimezone(timezone.utc).isoformat() if before else before,
|
||||
"after": after.astimezone(timezone.utc).isoformat() if after else after,
|
||||
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["otherUser", "activity"],
|
||||
schema=ActivityCollection,
|
||||
)
|
||||
@@ -0,0 +1,174 @@
|
||||
import re
|
||||
from typing import Any, Dict, List, Tuple
|
||||
|
||||
from gql import gql
|
||||
|
||||
from specklepy.core.api.models import ServerInfo
|
||||
from specklepy.core.api.resource import ResourceBase
|
||||
from specklepy.logging.exceptions import GraphQLException
|
||||
|
||||
NAME = "server"
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
"""API Access class for the server"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
super().__init__(
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
|
||||
def get(self) -> ServerInfo:
|
||||
"""Get the server info
|
||||
|
||||
Returns:
|
||||
dict -- the server info in dictionary form
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query Server {
|
||||
serverInfo {
|
||||
name
|
||||
company
|
||||
description
|
||||
adminContact
|
||||
canonicalUrl
|
||||
version
|
||||
roles {
|
||||
name
|
||||
description
|
||||
resourceTarget
|
||||
}
|
||||
scopes {
|
||||
name
|
||||
description
|
||||
}
|
||||
authStrategies{
|
||||
id
|
||||
name
|
||||
icon
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query, return_type="serverInfo", schema=ServerInfo
|
||||
)
|
||||
|
||||
def version(self) -> Tuple[Any, ...]:
|
||||
"""Get the server version
|
||||
|
||||
Returns:
|
||||
the server version in the format (major, minor, patch, (tag, build))
|
||||
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
|
||||
"""
|
||||
# not tracking as it will be called along with other mutations / queries as a check
|
||||
query = gql(
|
||||
"""
|
||||
query Server {
|
||||
serverInfo {
|
||||
version
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
ver = self.make_request(
|
||||
query=query, return_type=["serverInfo", "version"], parse_response=False
|
||||
)
|
||||
if isinstance(ver, Exception):
|
||||
raise GraphQLException(
|
||||
f"Could not get server version for {self.basepath}", [ver]
|
||||
)
|
||||
|
||||
# pylint: disable=consider-using-generator; (list comp is faster)
|
||||
return tuple(
|
||||
[
|
||||
int(segment) if segment.isdigit() else segment
|
||||
for segment in re.split(r"\.|-", ver)
|
||||
]
|
||||
)
|
||||
|
||||
def apps(self) -> Dict:
|
||||
"""Get the apps registered on the server
|
||||
|
||||
Returns:
|
||||
dict -- a dictionary of apps registered on the server
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query Apps {
|
||||
apps{
|
||||
id
|
||||
name
|
||||
description
|
||||
termsAndConditionsLink
|
||||
trustByDefault
|
||||
logo
|
||||
author {
|
||||
id
|
||||
name
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
return self.make_request(query=query, return_type="apps", parse_response=False)
|
||||
|
||||
def create_token(self, name: str, scopes: List[str], lifespan: int) -> str:
|
||||
"""Create a personal API token
|
||||
|
||||
Arguments:
|
||||
scopes {List[str]} -- the scopes to grant with this token
|
||||
name {str} -- a name for your new token
|
||||
lifespan {int} -- duration before the token expires
|
||||
|
||||
Returns:
|
||||
str -- the new API token. note: this is the only time you'll see the token!
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation TokenCreate($token: ApiTokenCreateInput!) {
|
||||
apiTokenCreate(token: $token)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"token": {"scopes": scopes, "name": name, "lifespan": lifespan}}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="apiTokenCreate",
|
||||
parse_response=False,
|
||||
)
|
||||
|
||||
def revoke_token(self, token: str) -> bool:
|
||||
"""Revokes (deletes) a personal API token
|
||||
|
||||
Arguments:
|
||||
token {str} -- the token to revoke (delete)
|
||||
|
||||
Returns:
|
||||
bool -- True if the token was successfully deleted
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation TokenRevoke($token: String!) {
|
||||
apiTokenRevoke(token: $token)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"token": token}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="apiTokenRevoke",
|
||||
parse_response=False,
|
||||
)
|
||||
@@ -0,0 +1,751 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import List, Optional
|
||||
|
||||
from deprecated import deprecated
|
||||
from gql import gql
|
||||
|
||||
from specklepy.core.api.models import ActivityCollection, PendingStreamCollaborator, Stream
|
||||
from specklepy.core.api.resource import ResourceBase
|
||||
from specklepy.logging.exceptions import SpeckleException, UnsupportedException
|
||||
|
||||
NAME = "stream"
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
"""API Access class for streams"""
|
||||
|
||||
def __init__(self, account, basepath, client, server_version) -> None:
|
||||
super().__init__(
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
server_version=server_version,
|
||||
)
|
||||
|
||||
self.schema = Stream
|
||||
|
||||
def get(self, id: str, branch_limit: int = 10, commit_limit: int = 10) -> Stream:
|
||||
"""Get the specified stream from the server
|
||||
|
||||
Arguments:
|
||||
id {str} -- the stream id
|
||||
branch_limit {int} -- the maximum number of branches to return
|
||||
commit_limit {int} -- the maximum number of commits to return
|
||||
|
||||
Returns:
|
||||
Stream -- the retrieved stream
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query Stream($id: String!, $branch_limit: Int!, $commit_limit: Int!) {
|
||||
stream(id: $id) {
|
||||
id
|
||||
name
|
||||
role
|
||||
description
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
commentCount
|
||||
favoritesCount
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
avatar
|
||||
}
|
||||
branches(limit: $branch_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
name
|
||||
description
|
||||
commits(limit: $commit_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
message
|
||||
authorId
|
||||
createdAt
|
||||
authorName
|
||||
referencedObject
|
||||
sourceApplication
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"id": id, "branch_limit": branch_limit, "commit_limit": commit_limit}
|
||||
|
||||
return self.make_request(query=query, params=params, return_type="stream")
|
||||
|
||||
def list(self, stream_limit: int = 10) -> List[Stream]:
|
||||
"""Get a list of the user's streams
|
||||
|
||||
Arguments:
|
||||
stream_limit {int} -- The maximum number of streams to return
|
||||
|
||||
Returns:
|
||||
List[Stream] -- A list of Stream objects
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query User($stream_limit: Int!) {
|
||||
user {
|
||||
id
|
||||
bio
|
||||
name
|
||||
email
|
||||
avatar
|
||||
company
|
||||
verified
|
||||
profiles
|
||||
role
|
||||
streams(limit: $stream_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
name
|
||||
role
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
description
|
||||
commentCount
|
||||
favoritesCount
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"stream_limit": stream_limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["user", "streams", "items"]
|
||||
)
|
||||
|
||||
def create(
|
||||
self,
|
||||
name: str = "Anonymous Python Stream",
|
||||
description: str = "No description provided",
|
||||
is_public: bool = True,
|
||||
) -> str:
|
||||
"""Create a new stream
|
||||
|
||||
Arguments:
|
||||
name {str} -- the name of the string
|
||||
description {str} -- a short description of the stream
|
||||
is_public {bool}
|
||||
-- whether or not the stream can be viewed by anyone with the id
|
||||
|
||||
Returns:
|
||||
id {str} -- the id of the newly created stream
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamCreate($stream: StreamCreateInput!) {
|
||||
streamCreate(stream: $stream)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"stream": {"name": name, "description": description, "isPublic": is_public}
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="streamCreate", parse_response=False
|
||||
)
|
||||
|
||||
def update(
|
||||
self,
|
||||
id: str,
|
||||
name: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
is_public: Optional[bool] = None,
|
||||
) -> bool:
|
||||
"""Update an existing stream
|
||||
|
||||
Arguments:
|
||||
id {str} -- the id of the stream to be updated
|
||||
name {str} -- the name of the string
|
||||
description {str} -- a short description of the stream
|
||||
is_public {bool}
|
||||
-- whether or not the stream can be viewed by anyone with the id
|
||||
|
||||
Returns:
|
||||
bool -- whether the stream update was successful
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamUpdate($stream: StreamUpdateInput!) {
|
||||
streamUpdate(stream: $stream)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"id": id,
|
||||
"name": name,
|
||||
"description": description,
|
||||
"isPublic": is_public,
|
||||
}
|
||||
# remove None values so graphql doesn't cry
|
||||
params = {"stream": {k: v for k, v in params.items() if v is not None}}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="streamUpdate", parse_response=False
|
||||
)
|
||||
|
||||
def delete(self, id: str) -> bool:
|
||||
"""Delete a stream given its id
|
||||
|
||||
Arguments:
|
||||
id {str} -- the id of the stream to delete
|
||||
|
||||
Returns:
|
||||
bool -- whether the deletion was successful
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamDelete($id: String!) {
|
||||
streamDelete(id: $id)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"id": id}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="streamDelete", parse_response=False
|
||||
)
|
||||
|
||||
def search(
|
||||
self,
|
||||
search_query: str,
|
||||
limit: int = 25,
|
||||
branch_limit: int = 10,
|
||||
commit_limit: int = 10,
|
||||
):
|
||||
"""Search for streams by name, description, or id
|
||||
|
||||
Arguments:
|
||||
search_query {str} -- a string to search for
|
||||
limit {int} -- the maximum number of results to return
|
||||
branch_limit {int} -- the maximum number of branches to return
|
||||
commit_limit {int} -- the maximum number of commits to return
|
||||
|
||||
Returns:
|
||||
List[Stream] -- a list of Streams that match the search query
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query StreamSearch(
|
||||
$search_query: String!,
|
||||
$limit: Int!,
|
||||
$branch_limit:Int!,
|
||||
$commit_limit:Int!
|
||||
) {
|
||||
streams(query: $search_query, limit: $limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
role
|
||||
description
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
avatar
|
||||
}
|
||||
branches(limit: $branch_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
name
|
||||
description
|
||||
commits(limit: $commit_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
referencedObject
|
||||
message
|
||||
authorName
|
||||
authorId
|
||||
createdAt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"search_query": search_query,
|
||||
"limit": limit,
|
||||
"branch_limit": branch_limit,
|
||||
"commit_limit": commit_limit,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["streams", "items"]
|
||||
)
|
||||
|
||||
def favorite(self, stream_id: str, favorited: bool = True):
|
||||
"""Favorite or unfavorite the given stream.
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to favorite / unfavorite
|
||||
favorited {bool}
|
||||
-- whether to favorite (True) or unfavorite (False) the stream
|
||||
|
||||
Returns:
|
||||
Stream -- the stream with its `id`, `name`, and `favoritedDate`
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamFavorite($stream_id: String!, $favorited: Boolean!) {
|
||||
streamFavorite(streamId: $stream_id, favorited: $favorited) {
|
||||
id
|
||||
name
|
||||
favoritedDate
|
||||
favoritesCount
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"stream_id": stream_id,
|
||||
"favorited": favorited,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["streamFavorite"]
|
||||
)
|
||||
|
||||
def get_all_pending_invites(
|
||||
self, stream_id: str
|
||||
) -> List[PendingStreamCollaborator]:
|
||||
"""Get all of the pending invites on a stream.
|
||||
You must be a `stream:owner` to query this.
|
||||
|
||||
Requires Speckle Server version >= 2.6.4
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the stream id from which to get the pending invites
|
||||
|
||||
Returns:
|
||||
List[PendingStreamCollaborator]
|
||||
-- a list of pending invites for the specified stream
|
||||
"""
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvites($streamId: String!) {
|
||||
stream(id: $streamId){
|
||||
pendingCollaborators {
|
||||
id
|
||||
token
|
||||
inviteId
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy{
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
user {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"streamId": stream_id}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["stream", "pendingCollaborators"],
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
|
||||
def invite(
|
||||
self,
|
||||
stream_id: str,
|
||||
email: Optional[str] = None,
|
||||
user_id: Optional[str] = None,
|
||||
role: str = "stream:contributor", # should default be reviewer?
|
||||
message: Optional[str] = None,
|
||||
):
|
||||
"""Invite someone to a stream using either their email or user id
|
||||
|
||||
Requires Speckle Server version >= 2.6.4
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to invite the user to
|
||||
email {str} -- the email of the user to invite (use this OR `user_id`)
|
||||
user_id {str} -- the id of the user to invite (use this OR `email`)
|
||||
role {str}
|
||||
-- the role to assign to the user (defaults to `stream:contributor`)
|
||||
message {str}
|
||||
-- a message to send along with this invite to the specified user
|
||||
|
||||
Returns:
|
||||
bool -- True if the operation was successful
|
||||
"""
|
||||
self._check_invites_supported()
|
||||
|
||||
if email is None and user_id is None:
|
||||
raise SpeckleException(
|
||||
"You must provide either an email or a user id to use the"
|
||||
" `stream.invite` method"
|
||||
)
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamInviteCreate($input: StreamInviteCreateInput!) {
|
||||
streamInviteCreate(input: $input)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"email": email,
|
||||
"userId": user_id,
|
||||
"streamId": stream_id,
|
||||
"message": message,
|
||||
"role": role,
|
||||
}
|
||||
params = {"input": {k: v for k, v in params.items() if v is not None}}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInviteCreate",
|
||||
parse_response=False,
|
||||
)
|
||||
|
||||
def invite_batch(
|
||||
self,
|
||||
stream_id: str,
|
||||
emails: Optional[List[str]] = None,
|
||||
user_ids: Optional[List[None]] = None,
|
||||
message: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""Invite a batch of users to a specified stream.
|
||||
|
||||
Requires Speckle Server version >= 2.6.4
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to invite the user to
|
||||
emails {List[str]}
|
||||
-- the email of the user to invite (use this and/or `user_ids`)
|
||||
user_id {List[str]}
|
||||
-- the id of the user to invite (use this and/or `emails`)
|
||||
message {str}
|
||||
-- a message to send along with this invite to the specified user
|
||||
|
||||
Returns:
|
||||
bool -- True if the operation was successful
|
||||
"""
|
||||
self._check_invites_supported()
|
||||
if emails is None and user_ids is None:
|
||||
raise SpeckleException(
|
||||
"You must provide either an email or a user id to use the"
|
||||
" `stream.invite` method"
|
||||
)
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamInviteBatchCreate($input: [StreamInviteCreateInput!]!) {
|
||||
streamInviteBatchCreate(input: $input)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
email_invites = [
|
||||
{"streamId": stream_id, "message": message, "email": email}
|
||||
for email in (emails if emails is not None else [])
|
||||
if email is not None
|
||||
]
|
||||
|
||||
user_invites = [
|
||||
{"streamId": stream_id, "message": message, "userId": user_id}
|
||||
for user_id in (user_ids if user_ids is not None else [])
|
||||
if user_id is not None
|
||||
]
|
||||
|
||||
|
||||
params = {"input": [*email_invites, *user_invites]}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInviteBatchCreate",
|
||||
parse_response=False,
|
||||
)
|
||||
|
||||
def invite_cancel(self, stream_id: str, invite_id: str) -> bool:
|
||||
"""Cancel an existing stream invite
|
||||
|
||||
Requires Speckle Server version >= 2.6.4
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream invite
|
||||
invite_id {str} -- the id of the invite to use
|
||||
|
||||
Returns:
|
||||
bool -- true if the operation was successful
|
||||
"""
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamInviteCancel($streamId: String!, $inviteId: String!) {
|
||||
streamInviteCancel(streamId: $streamId, inviteId: $inviteId)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"streamId": stream_id, "inviteId": invite_id}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInviteCancel",
|
||||
parse_response=False,
|
||||
)
|
||||
|
||||
def invite_use(self, stream_id: str, token: str, accept: bool = True) -> bool:
|
||||
"""Accept or decline a stream invite
|
||||
|
||||
Requires Speckle Server version >= 2.6.4
|
||||
|
||||
Arguments:
|
||||
stream_id {str}
|
||||
-- the id of the stream for which the user has a pending invite
|
||||
token {str} -- the token of the invite to use
|
||||
accept {bool} -- whether or not to accept the invite (defaults to True)
|
||||
|
||||
Returns:
|
||||
bool -- true if the operation was successful
|
||||
"""
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamInviteUse(
|
||||
$accept: Boolean!,
|
||||
$streamId: String!,
|
||||
$token: String!
|
||||
) {
|
||||
streamInviteUse(accept: $accept, streamId: $streamId, token: $token)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"streamId": stream_id, "token": token, "accept": accept}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInviteUse",
|
||||
parse_response=False,
|
||||
)
|
||||
|
||||
def update_permission(self, stream_id: str, user_id: str, role: str):
|
||||
"""Updates permissions for a user on a given stream
|
||||
|
||||
Valid for Speckle Server >=2.6.4
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to grant permissions to
|
||||
user_id {str} -- the id of the user to grant permissions for
|
||||
role {str} -- the role to grant the user
|
||||
|
||||
Returns:
|
||||
bool -- True if the operation was successful
|
||||
"""
|
||||
if self.server_version and (
|
||||
self.server_version != ("dev",) and self.server_version < (2, 6, 4)
|
||||
):
|
||||
raise UnsupportedException(
|
||||
"Server mutation `update_permission` is only supported as of Speckle"
|
||||
" Server v2.6.4. Please update your Speckle Server to use this method"
|
||||
" or use the `grant_permission` method instead."
|
||||
)
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamUpdatePermission(
|
||||
$permission_params: StreamUpdatePermissionInput!
|
||||
) {
|
||||
streamUpdatePermission(permissionParams: $permission_params)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"permission_params": {
|
||||
"streamId": stream_id,
|
||||
"userId": user_id,
|
||||
"role": role,
|
||||
}
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamUpdatePermission",
|
||||
parse_response=False,
|
||||
)
|
||||
|
||||
def revoke_permission(self, stream_id: str, user_id: str):
|
||||
"""Revoke permissions from a user on a given stream
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to revoke permissions from
|
||||
user_id {str} -- the id of the user to revoke permissions from
|
||||
|
||||
Returns:
|
||||
bool -- True if the operation was successful
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamRevokePermission(
|
||||
$permission_params: StreamRevokePermissionInput!
|
||||
) {
|
||||
streamRevokePermission(permissionParams: $permission_params)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"permission_params": {"streamId": stream_id, "userId": user_id}}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamRevokePermission",
|
||||
parse_response=False,
|
||||
)
|
||||
|
||||
def activity(
|
||||
self,
|
||||
stream_id: str,
|
||||
action_type: Optional[str] = None,
|
||||
limit: int = 20,
|
||||
before: Optional[datetime] = None,
|
||||
after: Optional[datetime] = None,
|
||||
cursor: Optional[datetime] = None,
|
||||
):
|
||||
"""
|
||||
Get the activity from a given stream in an Activity collection.
|
||||
Step into the activity `items` for the list of activity.
|
||||
|
||||
Note: all timestamps arguments should be `datetime` of any tz
|
||||
as they will be converted to UTC ISO format strings
|
||||
|
||||
stream_id {str} -- the id of the stream to get activity from
|
||||
action_type {str}
|
||||
-- filter results to a single action type
|
||||
(eg: `commit_create` or `commit_receive`)
|
||||
limit {int} -- max number of Activity items to return
|
||||
before {datetime}
|
||||
-- latest cutoff for activity (ie: return all activity _before_ this time)
|
||||
after {datetime}
|
||||
-- oldest cutoff for activity (ie: return all activity _after_ this time)
|
||||
cursor {datetime} -- timestamp cursor for pagination
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query StreamActivity(
|
||||
$stream_id: String!,
|
||||
$action_type: String,
|
||||
$before:DateTime,
|
||||
$after: DateTime,
|
||||
$cursor: DateTime,
|
||||
$limit: Int
|
||||
){
|
||||
stream(id: $stream_id) {
|
||||
activity(
|
||||
actionType: $action_type,
|
||||
before: $before,
|
||||
after: $after,
|
||||
cursor: $cursor,
|
||||
limit: $limit
|
||||
) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
actionType
|
||||
info
|
||||
userId
|
||||
streamId
|
||||
resourceId
|
||||
resourceType
|
||||
message
|
||||
time
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
try:
|
||||
params = {
|
||||
"stream_id": stream_id,
|
||||
"limit": limit,
|
||||
"action_type": action_type,
|
||||
"before": before.astimezone(timezone.utc).isoformat()
|
||||
if before
|
||||
else before,
|
||||
"after": after.astimezone(timezone.utc).isoformat() if after else after,
|
||||
"cursor": cursor.astimezone(timezone.utc).isoformat()
|
||||
if cursor
|
||||
else cursor,
|
||||
}
|
||||
except AttributeError as e:
|
||||
raise SpeckleException(
|
||||
"Could not get stream activity - `before`, `after`, and `cursor` must"
|
||||
" be in `datetime` format if provided",
|
||||
ValueError(),
|
||||
) from e
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["stream", "activity"],
|
||||
schema=ActivityCollection,
|
||||
)
|
||||
@@ -0,0 +1,138 @@
|
||||
from functools import wraps
|
||||
from typing import Callable, Dict, List, Optional, Union
|
||||
|
||||
from gql import gql
|
||||
from graphql import DocumentNode
|
||||
|
||||
from specklepy.core.api.resource import ResourceBase
|
||||
from specklepy.core.api.resources.stream import Stream
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
NAME = "subscribe"
|
||||
|
||||
|
||||
def check_wsclient(function):
|
||||
@wraps(function)
|
||||
async def check_wsclient_wrapper(self, *args, **kwargs):
|
||||
if self.client is None:
|
||||
raise SpeckleException(
|
||||
"You must authenticate before you can subscribe to events"
|
||||
)
|
||||
else:
|
||||
return await function(self, *args, **kwargs)
|
||||
|
||||
return check_wsclient_wrapper
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
"""API Access class for subscriptions"""
|
||||
|
||||
def __init__(self, account, basepath, client) -> None:
|
||||
super().__init__(
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
)
|
||||
|
||||
@check_wsclient
|
||||
async def stream_added(self, callback: Optional[Callable] = None):
|
||||
"""Subscribes to new stream added event for your profile.
|
||||
Use this to display an up-to-date list of streams.
|
||||
|
||||
Arguments:
|
||||
callback {Callable[Stream]} -- a function that takes the updated stream
|
||||
as an argument and executes each time a stream is added
|
||||
|
||||
Returns:
|
||||
Stream -- the update stream
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
subscription { userStreamAdded }
|
||||
"""
|
||||
)
|
||||
return await self.subscribe(
|
||||
query=query, callback=callback, return_type="userStreamAdded", schema=Stream
|
||||
)
|
||||
|
||||
@check_wsclient
|
||||
async def stream_updated(self, id: str, callback: Optional[Callable] = None):
|
||||
"""
|
||||
Subscribes to stream updated event.
|
||||
Use this in clients/components that pertain only to this stream.
|
||||
|
||||
Arguments:
|
||||
id {str} -- the stream id of the stream to subscribe to
|
||||
callback {Callable[Stream]}
|
||||
-- a function that takes the updated stream
|
||||
as an argument and executes each time the stream is updated
|
||||
|
||||
Returns:
|
||||
Stream -- the update stream
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
subscription Update($id: String!) { streamUpdated(streamId: $id) }
|
||||
"""
|
||||
)
|
||||
params = {"id": id}
|
||||
|
||||
return await self.subscribe(
|
||||
query=query,
|
||||
params=params,
|
||||
callback=callback,
|
||||
return_type="streamUpdated",
|
||||
schema=Stream,
|
||||
)
|
||||
|
||||
@check_wsclient
|
||||
async def stream_removed(self, callback: Optional[Callable] = None):
|
||||
"""Subscribes to stream removed event for your profile.
|
||||
Use this to display an up-to-date list of streams for your profile.
|
||||
NOTE: If someone revokes your permissions on a stream,
|
||||
this subscription will be triggered with an extra value of revokedBy
|
||||
in the payload.
|
||||
|
||||
Arguments:
|
||||
callback {Callable[Dict]}
|
||||
-- a function that takes the returned dict as an argument
|
||||
and executes each time a stream is removed
|
||||
|
||||
Returns:
|
||||
dict -- dict containing 'id' of stream removed and optionally 'revokedBy'
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
subscription { userStreamRemoved }
|
||||
"""
|
||||
)
|
||||
|
||||
return await self.subscribe(
|
||||
query=query,
|
||||
callback=callback,
|
||||
return_type="userStreamRemoved",
|
||||
parse_response=False,
|
||||
)
|
||||
|
||||
@check_wsclient
|
||||
async def subscribe(
|
||||
self,
|
||||
query: DocumentNode,
|
||||
params: Optional[Dict] = None,
|
||||
callback: Optional[Callable] = None,
|
||||
return_type: Optional[Union[str, List]] = None,
|
||||
schema=None,
|
||||
parse_response: bool = True,
|
||||
):
|
||||
# if self.client.transport.websocket is None:
|
||||
# TODO: add multiple subs to the same ws connection
|
||||
async with self.client as session:
|
||||
async for res in session.subscribe(query, variable_values=params):
|
||||
res = self._step_into_response(response=res, return_type=return_type)
|
||||
if parse_response:
|
||||
res = self._parse_response(response=res, schema=schema)
|
||||
if callback is not None:
|
||||
callback(res)
|
||||
else:
|
||||
return res
|
||||
@@ -0,0 +1,322 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from deprecated import deprecated
|
||||
from gql import gql
|
||||
|
||||
from specklepy.core.api.models import ActivityCollection, PendingStreamCollaborator, User
|
||||
from specklepy.core.api.resource import ResourceBase
|
||||
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
NAME = "user"
|
||||
|
||||
DEPRECATION_VERSION = "2.9.0"
|
||||
DEPRECATION_TEXT = (
|
||||
"The user resource is deprecated, please use the active_user or other_user"
|
||||
" resources"
|
||||
)
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
"""API Access class for users"""
|
||||
|
||||
def __init__(self, account, basepath, client, server_version) -> None:
|
||||
super().__init__(
|
||||
account=account,
|
||||
basepath=basepath,
|
||||
client=client,
|
||||
name=NAME,
|
||||
server_version=server_version,
|
||||
)
|
||||
self.schema = User
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def get(self, id: Optional[str] = None) -> User:
|
||||
"""
|
||||
Gets the profile of a user.
|
||||
If no id argument is provided, will return the current authenticated
|
||||
user's profile (as extracted from the authorization header).
|
||||
|
||||
Arguments:
|
||||
id {str} -- the user id
|
||||
|
||||
Returns:
|
||||
User -- the retrieved user
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
query User($id: String) {
|
||||
user(id: $id) {
|
||||
id
|
||||
email
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
profiles
|
||||
role
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"id": id}
|
||||
|
||||
return self.make_request(query=query, params=params, return_type="user")
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def search(
|
||||
self, search_query: str, limit: int = 25
|
||||
) -> Union[List[User], SpeckleException]:
|
||||
"""
|
||||
Searches for user by name or email.
|
||||
The search query must be at least 3 characters long
|
||||
|
||||
Arguments:
|
||||
search_query {str} -- a string to search for
|
||||
limit {int} -- the maximum number of results to return
|
||||
Returns:
|
||||
List[User] -- a list of User objects that match the search query
|
||||
"""
|
||||
if len(search_query) < 3:
|
||||
return SpeckleException(
|
||||
message="User search query must be at least 3 characters"
|
||||
)
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query UserSearch($search_query: String!, $limit: Int!) {
|
||||
userSearch(query: $search_query, limit: $limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"search_query": search_query, "limit": limit}
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type=["userSearch", "items"]
|
||||
)
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def update(
|
||||
self,
|
||||
name: Optional[str] = None,
|
||||
company: Optional[str] = None,
|
||||
bio: Optional[str] = None,
|
||||
avatar: Optional[str] = None,
|
||||
):
|
||||
"""Updates your user profile. All arguments are optional.
|
||||
|
||||
Arguments:
|
||||
name {str} -- your name
|
||||
company {str} -- the company you may or may not work for
|
||||
bio {str} -- tell us about yourself
|
||||
avatar {str} -- a nice photo of yourself
|
||||
|
||||
Returns:
|
||||
bool -- True if your profile was updated successfully
|
||||
"""
|
||||
query = gql(
|
||||
"""
|
||||
mutation UserUpdate($user: UserUpdateInput!) {
|
||||
userUpdate(user: $user)
|
||||
}
|
||||
"""
|
||||
)
|
||||
params = {"name": name, "company": company, "bio": bio, "avatar": avatar}
|
||||
|
||||
params = {"user": {k: v for k, v in params.items() if v is not None}}
|
||||
|
||||
if not params["user"]:
|
||||
return SpeckleException(
|
||||
message=(
|
||||
"You must provide at least one field to update your user profile"
|
||||
)
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="userUpdate", parse_response=False
|
||||
)
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def activity(
|
||||
self,
|
||||
user_id: Optional[str] = None,
|
||||
limit: int = 20,
|
||||
action_type: Optional[str] = None,
|
||||
before: Optional[datetime] = None,
|
||||
after: Optional[datetime] = None,
|
||||
cursor: Optional[datetime] = None,
|
||||
):
|
||||
"""
|
||||
Get the activity from a given stream in an Activity collection.
|
||||
Step into the activity `items` for the list of activity.
|
||||
If no id argument is provided, will return the current authenticated
|
||||
user's activity (as extracted from the authorization header).
|
||||
|
||||
Note: all timestamps arguments should be `datetime` of any tz as
|
||||
they will be converted to UTC ISO format strings
|
||||
|
||||
user_id {str} -- the id of the user to get the activity from
|
||||
action_type {str} -- filter results to a single action type
|
||||
(eg: `commit_create` or `commit_receive`)
|
||||
limit {int} -- max number of Activity items to return
|
||||
before {datetime}
|
||||
-- latest cutoff for activity (ie: return all activity _before_ this time)
|
||||
after {datetime}
|
||||
-- oldest cutoff for activity (ie: return all activity _after_ this time)
|
||||
cursor {datetime} -- timestamp cursor for pagination
|
||||
"""
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query UserActivity(
|
||||
$user_id: String,
|
||||
$action_type: String,
|
||||
$before:DateTime,
|
||||
$after: DateTime,
|
||||
$cursor: DateTime,
|
||||
$limit: Int
|
||||
){
|
||||
user(id: $user_id) {
|
||||
activity(
|
||||
actionType: $action_type,
|
||||
before: $before,
|
||||
after: $after,
|
||||
cursor: $cursor,
|
||||
limit: $limit
|
||||
) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
actionType
|
||||
info
|
||||
userId
|
||||
streamId
|
||||
resourceId
|
||||
resourceType
|
||||
message
|
||||
time
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {
|
||||
"user_id": user_id,
|
||||
"limit": limit,
|
||||
"action_type": action_type,
|
||||
"before": before.astimezone(timezone.utc).isoformat() if before else before,
|
||||
"after": after.astimezone(timezone.utc).isoformat() if after else after,
|
||||
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
|
||||
}
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type=["user", "activity"],
|
||||
schema=ActivityCollection,
|
||||
)
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
|
||||
"""Get all of the active user's pending stream invites
|
||||
|
||||
Requires Speckle Server version >= 2.6.4
|
||||
|
||||
Returns:
|
||||
List[PendingStreamCollaborator]
|
||||
-- a list of pending invites for the current user
|
||||
"""
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvites {
|
||||
streamInvites{
|
||||
id
|
||||
token
|
||||
inviteId
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
return_type="streamInvites",
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
|
||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||
def get_pending_invite(
|
||||
self, stream_id: str, token: Optional[str] = None
|
||||
) -> Optional[PendingStreamCollaborator]:
|
||||
"""Get a particular pending invite for the active user on a given stream.
|
||||
If no invite_id is provided, any valid invite will be returned.
|
||||
|
||||
Requires Speckle Server version >= 2.6.4
|
||||
|
||||
Arguments:
|
||||
stream_id {str} -- the id of the stream to look for invites on
|
||||
token {str} -- the token of the invite to look for (optional)
|
||||
|
||||
Returns:
|
||||
PendingStreamCollaborator
|
||||
-- the invite for the given stream (or None if it isn't found)
|
||||
"""
|
||||
self._check_invites_supported()
|
||||
|
||||
query = gql(
|
||||
"""
|
||||
query StreamInvite($streamId: String!, $token: String) {
|
||||
streamInvite(streamId: $streamId, token: $token) {
|
||||
id
|
||||
token
|
||||
streamId
|
||||
streamName
|
||||
title
|
||||
role
|
||||
invitedBy {
|
||||
id
|
||||
name
|
||||
company
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"streamId": stream_id}
|
||||
if token:
|
||||
params["token"] = token
|
||||
|
||||
return self.make_request(
|
||||
query=query,
|
||||
params=params,
|
||||
return_type="streamInvite",
|
||||
schema=PendingStreamCollaborator,
|
||||
)
|
||||
@@ -0,0 +1,186 @@
|
||||
from urllib.parse import unquote, urlparse
|
||||
from warnings import warn
|
||||
|
||||
from specklepy.core.api.client import SpeckleClient
|
||||
from specklepy.core.api.credentials import (
|
||||
Account,
|
||||
get_account_from_token,
|
||||
get_local_accounts,
|
||||
)
|
||||
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
||||
from specklepy.transports.server.server import ServerTransport
|
||||
|
||||
|
||||
class StreamWrapper:
|
||||
"""
|
||||
The `StreamWrapper` gives you some handy helpers to deal with urls and
|
||||
get authenticated clients and transports.
|
||||
|
||||
Construct a `StreamWrapper` with a stream, branch, commit, or object URL.
|
||||
The corresponding ids will be stored
|
||||
in the wrapper. If you have local accounts on the machine,
|
||||
you can use the `get_account` and `get_client` methods
|
||||
to get a local account for the server. You can also pass a token into `get_client`
|
||||
if you don't have a corresponding
|
||||
local account for the server.
|
||||
|
||||
```py
|
||||
from specklepy.api.wrapper import StreamWrapper
|
||||
|
||||
# provide any stream, branch, commit, object, or globals url
|
||||
wrapper = StreamWrapper("https://speckle.xyz/streams/3073b96e86/commits/604bea8cc6")
|
||||
|
||||
# get an authenticated SpeckleClient if you have a local account for the server
|
||||
client = wrapper.get_client()
|
||||
|
||||
# get an authenticated ServerTransport if you have a local account for the server
|
||||
transport = wrapper.get_transport()
|
||||
```
|
||||
"""
|
||||
|
||||
stream_url: str = None
|
||||
use_ssl: bool = True
|
||||
host: str = None
|
||||
stream_id: str = None
|
||||
commit_id: str = None
|
||||
object_id: str = None
|
||||
branch_name: str = None
|
||||
_client: SpeckleClient = None
|
||||
_account: Account = None
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"StreamWrapper( server: {self.host}, stream_id: {self.stream_id}, type:"
|
||||
f" {self.type} )"
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
if self.object_id:
|
||||
return "object"
|
||||
elif self.commit_id:
|
||||
return "commit"
|
||||
elif self.branch_name:
|
||||
return "branch"
|
||||
else:
|
||||
return "stream" if self.stream_id else "invalid"
|
||||
|
||||
def __init__(self, url: str) -> None:
|
||||
self.stream_url = url
|
||||
parsed = urlparse(url)
|
||||
self.host = parsed.netloc
|
||||
self.use_ssl = parsed.scheme == "https"
|
||||
segments = parsed.path.strip("/").split("/", 3)
|
||||
|
||||
if not segments or len(segments) < 2:
|
||||
raise SpeckleException(
|
||||
f"Cannot parse {url} into a stream wrapper class - invalid URL"
|
||||
" provided."
|
||||
)
|
||||
|
||||
while segments:
|
||||
segment = segments.pop(0)
|
||||
if segments and segment.lower() == "streams":
|
||||
self.stream_id = segments.pop(0)
|
||||
elif segments and segment.lower() == "commits":
|
||||
self.commit_id = segments.pop(0)
|
||||
elif segments and segment.lower() == "branches":
|
||||
self.branch_name = unquote(segments.pop(0))
|
||||
elif segments and segment.lower() == "objects":
|
||||
self.object_id = segments.pop(0)
|
||||
elif segment.lower() == "globals":
|
||||
self.branch_name = "globals"
|
||||
if segments:
|
||||
self.commit_id = segments.pop(0)
|
||||
else:
|
||||
raise SpeckleException(
|
||||
f"Cannot parse {url} into a stream wrapper class - invalid URL"
|
||||
" provided."
|
||||
)
|
||||
|
||||
if not self.stream_id:
|
||||
raise SpeckleException(
|
||||
f"Cannot parse {url} into a stream wrapper class - no stream id found."
|
||||
)
|
||||
|
||||
@property
|
||||
def server_url(self):
|
||||
return f"{'https' if self.use_ssl else 'http'}://{self.host}"
|
||||
|
||||
def get_account(self, token: str = None) -> Account:
|
||||
"""
|
||||
Gets an account object for this server from the local accounts db
|
||||
(added via Speckle Manager or a json file)
|
||||
"""
|
||||
if self._account and self._account.token:
|
||||
return self._account
|
||||
|
||||
self._account = next(
|
||||
(
|
||||
a
|
||||
for a in get_local_accounts()
|
||||
if self.host == urlparse(a.serverInfo.url).netloc
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
if not self._account:
|
||||
self._account = get_account_from_token(token, self.server_url)
|
||||
|
||||
if self._client:
|
||||
self._client.authenticate_with_account(self._account)
|
||||
|
||||
return self._account
|
||||
|
||||
def get_client(self, token: str = None) -> SpeckleClient:
|
||||
"""
|
||||
Gets an authenticated client for this server.
|
||||
You may provide a token if there aren't any local accounts on this
|
||||
machine. If no account is found and no token is provided,
|
||||
an unauthenticated client is returned.
|
||||
|
||||
Arguments:
|
||||
token {str}
|
||||
-- optional token if no local account is available (defaults to None)
|
||||
|
||||
Returns:
|
||||
SpeckleClient
|
||||
-- authenticated with a corresponding local account or the provided token
|
||||
"""
|
||||
if self._client and token is None:
|
||||
return self._client
|
||||
|
||||
if not self._account or not self._account.token:
|
||||
self.get_account(token)
|
||||
|
||||
if not self._client:
|
||||
self._client = SpeckleClient(host=self.host, use_ssl=self.use_ssl)
|
||||
|
||||
if self._account.token is None and token is None:
|
||||
warn(f"No local account found for server {self.host}", SpeckleWarning)
|
||||
return self._client
|
||||
|
||||
if self._account.token:
|
||||
self._client.authenticate_with_account(self._account)
|
||||
else:
|
||||
self._client.authenticate_with_token(token)
|
||||
|
||||
return self._client
|
||||
|
||||
def get_transport(self, token: str = None) -> ServerTransport:
|
||||
"""
|
||||
Gets a server transport for this stream using an authenticated client.
|
||||
If there is no local account for this
|
||||
server and the client was not authenticated with a token,
|
||||
this will throw an exception.
|
||||
|
||||
Returns:
|
||||
ServerTransport -- constructed for this stream
|
||||
with a pre-authenticated client
|
||||
"""
|
||||
if not self._account or not self._account.token:
|
||||
self.get_account(token)
|
||||
return ServerTransport(self.stream_id, account=self._account)
|
||||
@@ -106,6 +106,18 @@ def user_speckle_folder_path() -> Path:
|
||||
return _ensure_folder_exists(user_application_data_path(), _application_name)
|
||||
|
||||
|
||||
def user_speckle_connector_installation_path(host_application: str) -> Path:
|
||||
"""
|
||||
Gets a connector specific installation folder.
|
||||
|
||||
In this folder we can put our connector installation and all python packages.
|
||||
"""
|
||||
return _ensure_folder_exists(
|
||||
_ensure_folder_exists(user_speckle_folder_path(), "connector_installations"),
|
||||
host_application,
|
||||
)
|
||||
|
||||
|
||||
def accounts_folder_path() -> Path:
|
||||
"""Get the folder where the Speckle accounts data should be stored."""
|
||||
return _ensure_folder_exists(user_speckle_folder_path(), _accounts_folder_name)
|
||||
|
||||
@@ -23,23 +23,24 @@ LOG = logging.getLogger(__name__)
|
||||
METRICS_TRACKER = None
|
||||
|
||||
# actions
|
||||
SDK = "SDK Actions"
|
||||
RECEIVE = "Receive"
|
||||
SEND = "Send"
|
||||
STREAM = "Stream Action"
|
||||
PERMISSION = "Permission Action"
|
||||
INVITE = "Invite Action"
|
||||
COMMIT = "Commit Action"
|
||||
BRANCH = "Branch Action"
|
||||
USER = "User Action"
|
||||
OTHER_USER = "Other User Action"
|
||||
SERVER = "Server Action"
|
||||
CLIENT = "Speckle Client"
|
||||
STREAM_WRAPPER = "Stream Wrapper"
|
||||
|
||||
# not in use since 2.15
|
||||
ACCOUNTS = "Get Local Accounts"
|
||||
|
||||
SERIALIZE = "serialization/serialize"
|
||||
BRANCH = "Branch Action"
|
||||
CLIENT = "Speckle Client"
|
||||
COMMIT = "Commit Action"
|
||||
DESERIALIZE = "serialization/deserialize"
|
||||
INVITE = "Invite Action"
|
||||
OTHER_USER = "Other User Action"
|
||||
PERMISSION = "Permission Action"
|
||||
SERIALIZE = "serialization/serialize"
|
||||
SERVER = "Server Action"
|
||||
STREAM = "Stream Action"
|
||||
STREAM_WRAPPER = "Stream Wrapper"
|
||||
USER = "User Action"
|
||||
|
||||
|
||||
def disable():
|
||||
@@ -96,7 +97,7 @@ def initialise_tracker(account=None):
|
||||
if account and account.userInfo.email:
|
||||
METRICS_TRACKER.set_last_user(account.userInfo.email)
|
||||
if account and account.serverInfo.url:
|
||||
METRICS_TRACKER.set_last_server(account.userInfo.email)
|
||||
METRICS_TRACKER.set_last_server(account.serverInfo.url)
|
||||
|
||||
|
||||
class Singleton(type):
|
||||
@@ -139,7 +140,9 @@ class MetricsTracker(metaclass=Singleton):
|
||||
self.last_server = self.hash(server)
|
||||
|
||||
def hash(self, value: str):
|
||||
return hashlib.md5(value.lower().encode("utf-8")).hexdigest().upper()
|
||||
inputList = value.lower().split("://")
|
||||
input = inputList[len(inputList)-1].split("/")[0].split('?')[0]
|
||||
return hashlib.md5(input.encode("utf-8")).hexdigest().upper()
|
||||
|
||||
def _send_tracking_requests(self):
|
||||
session = requests.Session()
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
from typing import Optional
|
||||
from specklepy.objects import Base
|
||||
|
||||
|
||||
class CRS(Base, speckle_type="Objects.GIS.CRS"):
|
||||
"""A Coordinate Reference System stored in wkt format"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: Optional[str] = None,
|
||||
authority_id: Optional[str] = None,
|
||||
wkt: Optional[str] = None,
|
||||
units: Optional[str] = None,
|
||||
units_native: Optional[str] = None,
|
||||
offset_x: Optional[float] = None,
|
||||
offset_y: Optional[float] = None,
|
||||
rotation: Optional[float] = None,
|
||||
**kwargs
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self.name = name
|
||||
self.authority_id = authority_id
|
||||
self.wkt = wkt
|
||||
self.units = units or "m"
|
||||
self.units_native = units_native
|
||||
self.offset_x = offset_x
|
||||
self.offset_y = offset_y
|
||||
self.rotation = rotation
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
"""Builtin Speckle object kit."""
|
||||
|
||||
from specklepy.objects.GIS.layers import (
|
||||
VectorLayer,
|
||||
RasterLayer,
|
||||
)
|
||||
|
||||
from specklepy.objects.GIS.geometry import (
|
||||
GisPolygonGeometry,
|
||||
GisPolygonElement,
|
||||
GisLineElement,
|
||||
GisPointElement,
|
||||
GisRasterElement,
|
||||
)
|
||||
|
||||
from specklepy.objects.GIS.CRS import (
|
||||
CRS,
|
||||
)
|
||||
|
||||
__all__ = ["VectorLayer", "RasterLayer",
|
||||
"GisPolygonGeometry", "GisPolygonElement", "GisLineElement", "GisPointElement", "GisRasterElement",
|
||||
"CRS"]
|
||||
@@ -0,0 +1,104 @@
|
||||
|
||||
from typing import Optional, Union, List
|
||||
from specklepy.objects.geometry import Point, Line, Polyline, Circle, Arc, Polycurve, Mesh
|
||||
from specklepy.objects import Base
|
||||
from deprecated import deprecated
|
||||
|
||||
class GisPolygonGeometry(Base, speckle_type="Objects.GIS.PolygonGeometry", detachable={"displayValue"}):
|
||||
"""GIS Polygon Geometry"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
boundary: Optional[Union[Polyline, Arc, Line, Circle, Polycurve]] = None,
|
||||
voids: Optional[List[Union[Polyline, Arc, Line, Circle, Polycurve]] ] = None,
|
||||
displayValue: Optional[List[Mesh]] = None,
|
||||
units: Optional[str] = None,
|
||||
**kwargs
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self.boundary = boundary
|
||||
self.voids = voids
|
||||
self.displayValue = displayValue
|
||||
self.units = units or "m"
|
||||
|
||||
class GisPolygonElement(Base, speckle_type="Objects.GIS.PolygonElement"):
|
||||
"""GIS Polygon element"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
geometry: Optional[List[GisPolygonGeometry]] = None,
|
||||
attributes: Optional[Base] = None,
|
||||
units: Optional[str] = None,
|
||||
**kwargs
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self.geometry = geometry
|
||||
self.attributes = attributes
|
||||
self.units = units or "m"
|
||||
|
||||
class GisLineElement(Base, speckle_type="Objects.GIS.LineElement"):
|
||||
"""GIS Polyline element"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
geometry: Optional[List[Union[Polyline, Arc, Line, Circle, Polycurve]]] = None,
|
||||
attributes: Optional[Base] = None,
|
||||
units: Optional[str] = None,
|
||||
**kwargs
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self.geometry = geometry
|
||||
self.attributes = attributes
|
||||
self.units = units or "m"
|
||||
|
||||
class GisPointElement(Base, speckle_type="Objects.GIS.PointElement"):
|
||||
"""GIS Point element"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
geometry: Optional[List[Point]] = None,
|
||||
attributes: Optional[Base] = None,
|
||||
units: Optional[str] = None,
|
||||
**kwargs
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self.geometry = geometry
|
||||
self.attributes = attributes
|
||||
self.units = units or "m"
|
||||
|
||||
class GisRasterElement(Base, speckle_type="Objects.GIS.RasterElement", detachable={"displayValue"}):
|
||||
"""GIS Raster element"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
band_count: Optional[int] = None,
|
||||
band_names: Optional[List[str]] = None,
|
||||
x_origin: Optional[float] = None,
|
||||
y_origin: Optional[float] = None,
|
||||
x_size: Optional[int] = None,
|
||||
y_size: Optional[int] = None,
|
||||
x_resolution: Optional[float] = None,
|
||||
y_resolution: Optional[float] = None,
|
||||
noDataValue: Optional[List[float]] = None,
|
||||
displayValue: Optional[List[Mesh]] = None,
|
||||
units: Optional[str] = None,
|
||||
**kwargs
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self.band_count = band_count
|
||||
self.band_names = band_names
|
||||
self.x_origin = x_origin
|
||||
self.y_origin = y_origin
|
||||
self.x_size = x_size
|
||||
self.y_size = y_size
|
||||
self.x_resolution = x_resolution
|
||||
self.y_resolution = y_resolution
|
||||
self.noDataValue = noDataValue
|
||||
self.displayValue = displayValue
|
||||
self.units = units or "m"
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
from typing import Any, Dict, List, Optional
|
||||
from specklepy.objects.base import Base
|
||||
from specklepy.objects.other import Collection
|
||||
|
||||
from specklepy.objects.GIS.CRS import CRS
|
||||
from deprecated import deprecated
|
||||
|
||||
@deprecated(version="2.15", reason="Use VectorLayer or RasterLayer instead")
|
||||
class Layer(Base, detachable={"features"}):
|
||||
"""A GIS Layer"""
|
||||
def __init__(
|
||||
self,
|
||||
name:str=None,
|
||||
crs:CRS=None,
|
||||
units: str = "m",
|
||||
features: Optional[List[Base]] = None,
|
||||
layerType: str = "None",
|
||||
geomType: str = "None",
|
||||
renderer: Optional[dict[str, Any]] = None,
|
||||
**kwargs
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self.name = name
|
||||
self.crs = crs
|
||||
self.units = units
|
||||
self.type = layerType
|
||||
self.features = features or []
|
||||
self.geomType = geomType
|
||||
self.renderer = renderer or {}
|
||||
|
||||
class VectorLayer(Collection, detachable={"elements"}, speckle_type="Objects.GIS.VectorLayer", serialize_ignore={"features"}):
|
||||
"""GIS Vector Layer"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: Optional[str]=None,
|
||||
crs: Optional[CRS]=None,
|
||||
units: Optional[str] = None,
|
||||
elements: Optional[List[Base]] = None,
|
||||
attributes: Optional[Base] = None,
|
||||
geomType: Optional[str] = None,
|
||||
renderer: Optional[Dict[str, Any]] = None,
|
||||
**kwargs
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self.name = name or ""
|
||||
self.crs = crs
|
||||
self.units = units
|
||||
self.elements = elements or []
|
||||
self.attributes = attributes
|
||||
self.geomType = geomType or "None"
|
||||
self.renderer = renderer or {}
|
||||
self.collectionType = "VectorLayer"
|
||||
|
||||
@property
|
||||
@deprecated(version="2.14", reason="Use elements")
|
||||
def features(self) -> Optional[List[Base]]:
|
||||
return self.elements
|
||||
|
||||
@features.setter
|
||||
def features(self, value: Optional[List[Base]]) -> None:
|
||||
self.elements = value
|
||||
|
||||
class RasterLayer(Collection, detachable={"elements"}, speckle_type="Objects.GIS.RasterLayer", serialize_ignore={"features"}):
|
||||
"""GIS Raster Layer"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: Optional[str] = None,
|
||||
crs: Optional[CRS]=None,
|
||||
units: Optional[str] = None,
|
||||
rasterCrs: Optional[CRS]=None,
|
||||
elements: Optional[List[Base]] = None,
|
||||
geomType: Optional[str] = None,
|
||||
renderer: Optional[Dict[str, Any]] = None,
|
||||
**kwargs
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self.name = name or ""
|
||||
self.crs = crs
|
||||
self.units = units
|
||||
self.rasterCrs = rasterCrs
|
||||
self.elements = elements or []
|
||||
self.geomType = geomType or "None"
|
||||
self.renderer = renderer or {}
|
||||
self.collectionType = "RasterLayer"
|
||||
|
||||
@property
|
||||
@deprecated(version="2.14", reason="Use elements")
|
||||
def features(self) -> Optional[List[Base]]:
|
||||
return self.elements
|
||||
|
||||
@features.setter
|
||||
def features(self, value: Optional[List[Base]]) -> None:
|
||||
self.elements = value
|
||||
|
||||
@@ -5,6 +5,7 @@ from typing import (
|
||||
Any,
|
||||
ClassVar,
|
||||
Dict,
|
||||
ForwardRef,
|
||||
List,
|
||||
Optional,
|
||||
Set,
|
||||
@@ -217,6 +218,9 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
|
||||
return True, t(value)
|
||||
|
||||
if getattr(t, "__module__", None) == "typing":
|
||||
if isinstance(t, ForwardRef):
|
||||
return True, value
|
||||
|
||||
origin = getattr(t, "__origin__")
|
||||
# below is what in nicer for >= py38
|
||||
# origin = get_origin(t)
|
||||
@@ -303,7 +307,7 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
|
||||
if isinstance(value, t):
|
||||
return True, value
|
||||
|
||||
with contextlib.suppress(ValueError):
|
||||
with contextlib.suppress(ValueError, TypeError):
|
||||
if t is float and value is not None:
|
||||
return True, float(value)
|
||||
# TODO: dafuq, i had to add this not list check
|
||||
|
||||
@@ -39,7 +39,11 @@ class Point(Base, speckle_type=GEOMETRY + "Point"):
|
||||
return pt
|
||||
|
||||
|
||||
class Pointcloud(Base, speckle_type=GEOMETRY + "Pointcloud"):
|
||||
class Pointcloud(
|
||||
Base,
|
||||
speckle_type=GEOMETRY + "Pointcloud",
|
||||
chunkable={"points": 31250, "colors": 62500, "sizes": 62500},
|
||||
):
|
||||
points: Optional[List[float]] = None
|
||||
colors: Optional[List[int]] = None
|
||||
sizes: Optional[List[float]] = None
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
from typing import Any, List, Optional
|
||||
from deprecated import deprecated
|
||||
|
||||
from specklepy.objects.geometry import Point, Vector
|
||||
|
||||
from .base import Base
|
||||
|
||||
OTHER = "Objects.Other."
|
||||
OTHER_REVIT = OTHER + "Revit."
|
||||
|
||||
IDENTITY_TRANSFORM = [
|
||||
1.0,
|
||||
@@ -72,7 +74,7 @@ class DisplayStyle(Base, speckle_type=OTHER + "DisplayStyle"):
|
||||
class Transform(
|
||||
Base,
|
||||
speckle_type=OTHER + "Transform",
|
||||
serialize_ignore={"translation", "scaling", "is_identity"},
|
||||
serialize_ignore={"translation", "scaling", "is_identity", "value"},
|
||||
):
|
||||
"""The 4x4 transformation matrix
|
||||
|
||||
@@ -84,12 +86,21 @@ class Transform(
|
||||
_value: Optional[List[float]] = None
|
||||
|
||||
@property
|
||||
@deprecated(version="2.12", reason="Use matrix")
|
||||
def value(self) -> List[float]:
|
||||
"""The transform matrix represented as a flat list of 16 floats"""
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, value: List[float]) -> None:
|
||||
self.matrix = value
|
||||
|
||||
@property
|
||||
def matrix(self) -> List[float]:
|
||||
"""The transform matrix represented as a flat list of 16 floats"""
|
||||
return self._value
|
||||
|
||||
@matrix.setter
|
||||
def matrix(self, value: List[float]) -> None:
|
||||
try:
|
||||
value = [float(x) for x in value]
|
||||
except (ValueError, TypeError) as error:
|
||||
@@ -118,7 +129,7 @@ class Transform(
|
||||
|
||||
@property
|
||||
def is_identity(self) -> bool:
|
||||
return self.value == IDENTITY_TRANSFORM
|
||||
return self._value == IDENTITY_TRANSFORM
|
||||
|
||||
def apply_to_point(self, point: Point) -> Point:
|
||||
"""Transform a single speckle Point
|
||||
@@ -236,15 +247,38 @@ class BlockDefinition(
|
||||
geometry: Optional[List[Base]] = None
|
||||
|
||||
|
||||
class BlockInstance(
|
||||
Base, speckle_type=OTHER + "BlockInstance", detachable={"blockDefinition"}
|
||||
class Instance(
|
||||
Base, speckle_type=OTHER + "Instance", detachable={"definition"}
|
||||
):
|
||||
blockDefinition: Optional[BlockDefinition] = None
|
||||
transform: Optional[Transform] = None
|
||||
definition: Optional[Base] = None
|
||||
|
||||
|
||||
class BlockInstance(
|
||||
Instance, speckle_type=OTHER + "BlockInstance", serialize_ignore={"blockDefinition"}
|
||||
):
|
||||
@property
|
||||
@deprecated(version="2.13", reason="Use definition")
|
||||
def blockDefinition(self) -> Optional[BlockDefinition]:
|
||||
if isinstance(self.definition, BlockDefinition):
|
||||
return self.definition
|
||||
return None
|
||||
|
||||
@blockDefinition.setter
|
||||
def blockDefinition(self, value: Optional[BlockDefinition]) -> None:
|
||||
self.definition = value
|
||||
|
||||
class RevitInstance(Instance, speckle_type=OTHER_REVIT + "RevitInstance"):
|
||||
level: Optional[Base] = None
|
||||
facingFlipped: bool
|
||||
handFlipped: bool
|
||||
parameters: Optional[Base] = None
|
||||
elementId: Optional[str]
|
||||
|
||||
# TODO: prob move this into a built elements module, but just trialling this for now
|
||||
class RevitParameter(Base, speckle_type="Objects.BuiltElements.Revit.Parameter"):
|
||||
class RevitParameter(
|
||||
Base, speckle_type="Objects.BuiltElements.Revit.Parameter"
|
||||
):
|
||||
name: Optional[str] = None
|
||||
value: Any = None
|
||||
applicationUnitType: Optional[str] = None # eg UnitType UT_Length
|
||||
@@ -255,3 +289,10 @@ class RevitParameter(Base, speckle_type="Objects.BuiltElements.Revit.Parameter")
|
||||
isShared: bool = False
|
||||
isReadOnly: bool = False
|
||||
isTypeParameter: bool = False
|
||||
|
||||
class Collection(
|
||||
Base, speckle_type="Speckle.Core.Models.Collection", detachable={"elements"}
|
||||
):
|
||||
name: Optional[str] = None
|
||||
collectionType: Optional[str] = None
|
||||
elements: Optional[List[Base]] = None
|
||||
@@ -6,7 +6,10 @@ from specklepy.objects.structural.analysis import (
|
||||
ModelSettings,
|
||||
ModelUnits,
|
||||
)
|
||||
from specklepy.objects.structural.axis import Axis
|
||||
from specklepy.objects.structural.axis import (
|
||||
AxisType,
|
||||
Axis
|
||||
)
|
||||
from specklepy.objects.structural.geometry import (
|
||||
Element1D,
|
||||
Element2D,
|
||||
@@ -82,6 +85,7 @@ __all__ = [
|
||||
"ElementType1D",
|
||||
"ElementType2D",
|
||||
"ElementType3D",
|
||||
"AxisType",
|
||||
"Axis",
|
||||
"Node",
|
||||
"Restraint",
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
from typing import Optional
|
||||
from enum import Enum
|
||||
|
||||
from specklepy.objects.base import Base
|
||||
from specklepy.objects.geometry import Plane
|
||||
|
||||
|
||||
class AxisType(int, Enum):
|
||||
Cartesian = 0
|
||||
Cylindrical = 1
|
||||
Spherical = 2
|
||||
|
||||
|
||||
class Axis(Base, speckle_type="Objects.Structural.Geometry.Axis"):
|
||||
name: Optional[str] = None
|
||||
axisType: Optional[str] = None
|
||||
axisType: Optional[AxisType] = None
|
||||
plane: Optional[Plane] = None
|
||||
|
||||
@@ -65,6 +65,7 @@ class ShapeType(int, Enum):
|
||||
Box = 7
|
||||
Catalogue = 8
|
||||
Explicit = 9
|
||||
Undefined = 10
|
||||
|
||||
|
||||
class PropertyTypeSpring(int, Enum):
|
||||
@@ -90,7 +91,9 @@ class Property(Base, speckle_type=STRUCTURAL_PROPERTY):
|
||||
name: Optional[str] = None
|
||||
|
||||
|
||||
class SectionProfile(Base, speckle_type=STRUCTURAL_PROPERTY + ".SectionProfile"):
|
||||
class SectionProfile(
|
||||
Base, speckle_type=STRUCTURAL_PROPERTY + ".Profiles.SectionProfile"
|
||||
):
|
||||
name: Optional[str] = None
|
||||
shapeType: Optional[ShapeType] = None
|
||||
area: float = 0.0
|
||||
|
||||
@@ -7,6 +7,7 @@ from pydantic.config import Extra
|
||||
|
||||
class AbstractTransport(ABC, BaseModel):
|
||||
_name: str = "Abstract"
|
||||
model_config = {'extra': 'allow', 'arbitrary_types_allowed': True}
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@@ -87,7 +88,3 @@ class AbstractTransport(ABC, BaseModel):
|
||||
str -- the string representation of the root object
|
||||
"""
|
||||
pass
|
||||
|
||||
class Config:
|
||||
extra = Extra.allow
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
@@ -18,6 +18,7 @@ class BatchSender(object):
|
||||
stream_id,
|
||||
token,
|
||||
max_batch_size_mb=1,
|
||||
max_batch_length=20000,
|
||||
batch_buffer_length=10,
|
||||
thread_count=4,
|
||||
):
|
||||
@@ -26,6 +27,7 @@ class BatchSender(object):
|
||||
self._token = token
|
||||
|
||||
self.max_size = int(max_batch_size_mb * 1000 * 1000)
|
||||
self.max_batch_length = int(max_batch_length)
|
||||
self._batches = queue.Queue(batch_buffer_length)
|
||||
self._crt_batch = []
|
||||
self._crt_batch_size = 0
|
||||
@@ -39,7 +41,11 @@ class BatchSender(object):
|
||||
self._create_threads()
|
||||
|
||||
crt_obj_size = len(obj)
|
||||
if not self._crt_batch or self._crt_batch_size + crt_obj_size < self.max_size:
|
||||
crt_batch_length = len(self._crt_batch)
|
||||
if not self._crt_batch or (
|
||||
self._crt_batch_size + crt_obj_size < self.max_size
|
||||
and crt_batch_length < self.max_batch_length
|
||||
):
|
||||
self._crt_batch.append((id, obj))
|
||||
self._crt_batch_size += crt_obj_size
|
||||
return
|
||||
@@ -90,17 +96,18 @@ class BatchSender(object):
|
||||
self._exception = self._exception or ex
|
||||
LOG.error("ServerTransport sending thread error: " + str(ex))
|
||||
|
||||
def _bg_send_batch(self, session, batch):
|
||||
def _bg_send_batch(self, session: requests.Session, batch):
|
||||
object_ids = [obj[0] for obj in batch]
|
||||
try:
|
||||
server_has_object = session.post(
|
||||
url=f"{self.server_url}/api/diff/{self.stream_id}",
|
||||
data={"objects": json.dumps(object_ids)},
|
||||
).json()
|
||||
except Exception as ex:
|
||||
response = session.post(
|
||||
url=f"{self.server_url}/api/diff/{self.stream_id}",
|
||||
data={"objects": json.dumps(object_ids)},
|
||||
)
|
||||
if response.status_code == 403:
|
||||
raise SpeckleException(
|
||||
f"Invalid credentials - cannot send objects to server {self.server_url}"
|
||||
) from ex
|
||||
)
|
||||
response.raise_for_status()
|
||||
server_has_object = response.json()
|
||||
|
||||
new_object_ids = [x for x in object_ids if not server_has_object[x]]
|
||||
new_object_ids = set(new_object_ids)
|
||||
@@ -130,7 +137,7 @@ class BatchSender(object):
|
||||
raise SpeckleException(
|
||||
message=(
|
||||
"Could not save the object to the server - status code"
|
||||
f" {r.status_code}"
|
||||
f" {r.status_code} ({r.text[:1000]})"
|
||||
)
|
||||
)
|
||||
except json.JSONDecodeError as error:
|
||||
|
||||
@@ -5,7 +5,7 @@ from warnings import warn
|
||||
import requests
|
||||
|
||||
from specklepy.api.client import SpeckleClient
|
||||
from specklepy.api.credentials import Account, get_account_from_token
|
||||
from specklepy.core.api.credentials import Account, get_account_from_token
|
||||
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
||||
from specklepy.transports.abstract_transport import AbstractTransport
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import random
|
||||
import uuid
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
@@ -38,7 +39,8 @@ def seed_user(host):
|
||||
)
|
||||
if not r.ok:
|
||||
raise Exception(f"Cannot seed user: {r.reason}")
|
||||
access_code = r.text.split("access_code=")[1]
|
||||
redirect_url = urlparse(r.headers.get("location"))
|
||||
access_code = parse_qs(redirect_url.query)["access_code"][0] # type: ignore
|
||||
|
||||
r_tokens = requests.post(
|
||||
url=f"http://{host}/auth/token",
|
||||
|
||||
@@ -39,7 +39,7 @@ class TestUser:
|
||||
assert my_activity.totalCount
|
||||
assert isinstance(their_activity, ActivityCollection)
|
||||
|
||||
older_activity = client.user.activity(before=my_activity.items[0].time)
|
||||
older_activity = client.active_user.activity(before=my_activity.items[0].time)
|
||||
|
||||
assert isinstance(older_activity, ActivityCollection)
|
||||
assert older_activity.totalCount
|
||||
|
||||
@@ -37,7 +37,7 @@ class TestStream:
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def second_user(self, second_client: SpeckleClient):
|
||||
return second_client.user.get()
|
||||
return second_client.active_user.get()
|
||||
|
||||
def test_stream_create(self, client, stream, updated_stream):
|
||||
stream.id = updated_stream.id = client.stream.create(
|
||||
@@ -93,15 +93,6 @@ class TestStream:
|
||||
assert isinstance(favorited, Stream)
|
||||
assert unfavorited.favoritedDate is None
|
||||
|
||||
def test_stream_grant_permission(self, client, stream, second_user):
|
||||
# deprecated as of Speckle Server 2.6.4
|
||||
with pytest.raises(UnsupportedException):
|
||||
client.stream.grant_permission(
|
||||
stream_id=stream.id,
|
||||
user_id=second_user.id,
|
||||
role="stream:contributor",
|
||||
)
|
||||
|
||||
def test_stream_invite(
|
||||
self, client: SpeckleClient, stream: Stream, second_user_dict: dict
|
||||
):
|
||||
@@ -122,18 +113,18 @@ class TestStream:
|
||||
self, second_client: SpeckleClient, stream: Stream
|
||||
):
|
||||
# NOTE: these are user queries, but testing here to contain the flow
|
||||
invites = second_client.user.get_all_pending_invites()
|
||||
invites = second_client.active_user.get_all_pending_invites()
|
||||
|
||||
assert isinstance(invites, list)
|
||||
assert isinstance(invites[0], PendingStreamCollaborator)
|
||||
assert len(invites) == 1
|
||||
|
||||
invite = second_client.user.get_pending_invite(stream_id=stream.id)
|
||||
invite = second_client.active_user.get_pending_invite(stream_id=stream.id)
|
||||
assert isinstance(invite, PendingStreamCollaborator)
|
||||
|
||||
def test_stream_invite_use(self, second_client: SpeckleClient, stream: Stream):
|
||||
invite: PendingStreamCollaborator = (
|
||||
second_client.user.get_all_pending_invites()[0]
|
||||
second_client.active_user.get_all_pending_invites()[0]
|
||||
)
|
||||
|
||||
accepted = second_client.stream.invite_use(
|
||||
|
||||
@@ -44,3 +44,12 @@ def test_accounts_folder_name_override():
|
||||
speckle_path_provider.override_accounts_folder_name(new_folder_name)
|
||||
assert speckle_path_provider._accounts_folder_name == new_folder_name
|
||||
speckle_path_provider.override_accounts_folder_name(old_folder_name)
|
||||
|
||||
|
||||
def test_connector_installation_path():
|
||||
host_application = "test application"
|
||||
connector_path = speckle_path_provider.user_speckle_connector_installation_path(
|
||||
host_application
|
||||
)
|
||||
assert "connector_installations" in str(connector_path)
|
||||
assert str(connector_path).endswith(host_application)
|
||||
|
||||
@@ -106,6 +106,9 @@ fake_bases = [FakeBase("foo"), FakeBase("bar")]
|
||||
True,
|
||||
fake_bases,
|
||||
),
|
||||
(List["int"], [2, 3, 4], True, [2, 3, 4]),
|
||||
(Union[float, Dict[str, float]], {"foo": 1, "bar": 2}, True, {"foo": 1.0, "bar": 2.0}),
|
||||
(Union[float, Dict[str, float]], {"foo": "bar"}, False, {"foo": "bar"}),
|
||||
],
|
||||
)
|
||||
def test_validate_type(
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
"""
|
||||
Provides uniform and consistent path helpers for `specklepy`
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from importlib import import_module, invalidate_caches
|
||||
|
||||
_user_data_env_var = "SPECKLE_USERDATA_PATH"
|
||||
|
||||
|
||||
def _path() -> Optional[Path]:
|
||||
"""Read the user data path override setting."""
|
||||
path_override = os.environ.get(_user_data_env_var)
|
||||
if path_override:
|
||||
return Path(path_override)
|
||||
return None
|
||||
|
||||
|
||||
_application_name = "Speckle"
|
||||
|
||||
|
||||
def override_application_name(application_name: str) -> None:
|
||||
"""Override the global Speckle application name."""
|
||||
global _application_name
|
||||
_application_name = application_name
|
||||
|
||||
|
||||
def override_application_data_path(path: Optional[str]) -> None:
|
||||
"""
|
||||
Override the global Speckle application data path.
|
||||
|
||||
If the value of path is `None` the environment variable gets deleted.
|
||||
"""
|
||||
if path:
|
||||
os.environ[_user_data_env_var] = path
|
||||
else:
|
||||
os.environ.pop(_user_data_env_var, None)
|
||||
|
||||
|
||||
def _ensure_folder_exists(base_path: Path, folder_name: str) -> Path:
|
||||
path = base_path.joinpath(folder_name)
|
||||
path.mkdir(exist_ok=True, parents=True)
|
||||
return path
|
||||
|
||||
|
||||
def user_application_data_path() -> Path:
|
||||
"""Get the platform specific user configuration folder path"""
|
||||
path_override = _path()
|
||||
if path_override:
|
||||
return path_override
|
||||
|
||||
try:
|
||||
if sys.platform.startswith("win"):
|
||||
app_data_path = os.getenv("APPDATA")
|
||||
if not app_data_path:
|
||||
raise Exception(
|
||||
"Cannot get appdata path from environment."
|
||||
)
|
||||
return Path(app_data_path)
|
||||
else:
|
||||
# try getting the standard XDG_DATA_HOME value
|
||||
# as that is used as an override
|
||||
app_data_path = os.getenv("XDG_DATA_HOME")
|
||||
if app_data_path:
|
||||
return Path(app_data_path)
|
||||
else:
|
||||
return _ensure_folder_exists(Path.home(), ".config")
|
||||
except Exception as ex:
|
||||
raise Exception(
|
||||
"Failed to initialize user application data path.", ex
|
||||
)
|
||||
|
||||
|
||||
def user_speckle_folder_path() -> Path:
|
||||
"""Get the folder where the user's Speckle data should be stored."""
|
||||
return _ensure_folder_exists(user_application_data_path(), _application_name)
|
||||
|
||||
|
||||
def user_speckle_connector_installation_path(host_application: str) -> Path:
|
||||
"""
|
||||
Gets a connector specific installation folder.
|
||||
|
||||
In this folder we can put our connector installation and all python packages.
|
||||
"""
|
||||
return _ensure_folder_exists(
|
||||
_ensure_folder_exists(user_speckle_folder_path(), "connector_installations"),
|
||||
host_application,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
print("Starting module dependency installation")
|
||||
print(sys.executable)
|
||||
|
||||
PYTHON_PATH = sys.executable
|
||||
|
||||
|
||||
|
||||
def connector_installation_path(host_application: str) -> Path:
|
||||
connector_installation_path = user_speckle_connector_installation_path(host_application)
|
||||
connector_installation_path.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
# set user modules path at beginning of paths for earlier hit
|
||||
if sys.path[0] != connector_installation_path:
|
||||
sys.path.insert(0, str(connector_installation_path))
|
||||
|
||||
print(f"Using connector installation path {connector_installation_path}")
|
||||
return connector_installation_path
|
||||
|
||||
|
||||
|
||||
def is_pip_available() -> bool:
|
||||
try:
|
||||
import_module("pip") # noqa F401
|
||||
return True
|
||||
except ImportError:
|
||||
return False
|
||||
|
||||
|
||||
def ensure_pip() -> None:
|
||||
print("Installing pip... ")
|
||||
|
||||
from subprocess import run
|
||||
|
||||
completed_process = run([PYTHON_PATH, "-m", "ensurepip"])
|
||||
|
||||
if completed_process.returncode == 0:
|
||||
print("Successfully installed pip")
|
||||
else:
|
||||
raise Exception(f"Failed to install pip, got {completed_process.returncode} return code")
|
||||
|
||||
|
||||
def get_requirements_path() -> Path:
|
||||
# we assume that a requirements.txt exists next to the __init__.py file
|
||||
path = Path(Path(__file__).parent, "requirements.txt")
|
||||
assert path.exists()
|
||||
return path
|
||||
|
||||
|
||||
def install_requirements(host_application: str) -> None:
|
||||
# set up addons/modules under the user
|
||||
# script path. Here we'll install the
|
||||
# dependencies
|
||||
path = connector_installation_path(host_application)
|
||||
print(f"Installing Speckle dependencies to {path}")
|
||||
|
||||
from subprocess import run
|
||||
|
||||
completed_process = run(
|
||||
[
|
||||
PYTHON_PATH,
|
||||
"-m",
|
||||
"pip",
|
||||
"install",
|
||||
"-t",
|
||||
str(path),
|
||||
"-r",
|
||||
str(get_requirements_path()),
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
if completed_process.returncode != 0:
|
||||
m = f"Failed to install dependenices through pip, got {completed_process.returncode} return code"
|
||||
print(m)
|
||||
raise Exception(m)
|
||||
|
||||
|
||||
def install_dependencies(host_application: str) -> None:
|
||||
if not is_pip_available():
|
||||
ensure_pip()
|
||||
|
||||
install_requirements(host_application)
|
||||
|
||||
|
||||
def _import_dependencies() -> None:
|
||||
import_module("specklepy")
|
||||
# the code above doesn't work for now, it fails on importing graphql-core
|
||||
# despite that, the connector seams to be working as expected
|
||||
# But it would be nice to make this solution work
|
||||
# it would ensure that all dependencies are fully loaded
|
||||
# requirements = get_requirements_path().read_text()
|
||||
# reqs = [
|
||||
# req.split(" ; ")[0].split("==")[0].split("[")[0].replace("-", "_")
|
||||
# for req in requirements.split("\n")
|
||||
# if req and not req.startswith(" ")
|
||||
# ]
|
||||
# for req in reqs:
|
||||
# print(req)
|
||||
# import_module("specklepy")
|
||||
|
||||
def ensure_dependencies(host_application: str) -> None:
|
||||
try:
|
||||
install_dependencies(host_application)
|
||||
invalidate_caches()
|
||||
_import_dependencies()
|
||||
print("Successfully found dependencies")
|
||||
except ImportError:
|
||||
raise Exception(f"Cannot automatically ensure Speckle dependencies. Please try restarting the host application {host_application}!")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user