Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0330c6cf83 | |||
| e98a80d45a | |||
| 2d7e0e9e92 |
+35
-63
@@ -1,76 +1,53 @@
|
|||||||
version: 2.1
|
version: 2.1
|
||||||
|
|
||||||
orbs:
|
orbs:
|
||||||
codecov: codecov/codecov@3.3.0
|
python: circleci/python@2.0.3
|
||||||
|
codecov: codecov/codecov@3.2.2
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
pre-commit:
|
|
||||||
parameters:
|
|
||||||
config_file:
|
|
||||||
default: ./.pre-commit-config.yaml
|
|
||||||
description: Optional, path to pre-commit config file.
|
|
||||||
type: string
|
|
||||||
cache_prefix:
|
|
||||||
default: ''
|
|
||||||
description: |
|
|
||||||
Optional cache prefix to be used on CircleCI. Can be used for cache busting or to ensure multiple jobs use different caches.
|
|
||||||
type: string
|
|
||||||
docker:
|
|
||||||
- image: speckle/pre-commit-runner:latest
|
|
||||||
resource_class: medium
|
|
||||||
steps:
|
|
||||||
- checkout
|
|
||||||
- restore_cache:
|
|
||||||
keys:
|
|
||||||
- cache-pre-commit-<<parameters.cache_prefix>>-{{ checksum "<<parameters.config_file>>" }}
|
|
||||||
- run:
|
|
||||||
name: Install pre-commit hooks
|
|
||||||
command: pre-commit install-hooks --config <<parameters.config_file>>
|
|
||||||
- save_cache:
|
|
||||||
key: cache-pre-commit-<<parameters.cache_prefix>>-{{ checksum "<<parameters.config_file>>" }}
|
|
||||||
paths:
|
|
||||||
- ~/.cache/pre-commit
|
|
||||||
- run:
|
|
||||||
name: Run pre-commit
|
|
||||||
command: pre-commit run --all-files
|
|
||||||
- run:
|
|
||||||
command: git --no-pager diff
|
|
||||||
name: git diff
|
|
||||||
when: on_fail
|
|
||||||
|
|
||||||
test:
|
test:
|
||||||
machine:
|
docker:
|
||||||
image: ubuntu-2204:2023.02.1
|
- image: "cimg/python:<<parameters.tag>>"
|
||||||
docker_layer_caching: false
|
- image: "cimg/node:16.15"
|
||||||
resource_class: medium
|
- 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"
|
||||||
parameters:
|
parameters:
|
||||||
tag:
|
tag:
|
||||||
default: "3.11"
|
default: "3.8"
|
||||||
type: string
|
type: string
|
||||||
steps:
|
steps:
|
||||||
- checkout
|
- checkout
|
||||||
|
- run: python --version
|
||||||
- run:
|
- run:
|
||||||
name: Install python
|
command: python -m pip install --upgrade pip
|
||||||
command: |
|
name: upgrade pip
|
||||||
pyenv install -s << parameters.tag >>
|
- python/install-packages:
|
||||||
pyenv global << parameters.tag >>
|
pkg-manager: poetry
|
||||||
- run:
|
- run: poetry run pytest --cov --cov-report xml:reports/coverage.xml --junitxml=reports/test-results.xml
|
||||||
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:
|
- store_test_results:
|
||||||
path: reports
|
path: reports
|
||||||
|
|
||||||
- store_artifacts:
|
- store_artifacts:
|
||||||
path: reports
|
path: reports
|
||||||
|
|
||||||
- codecov/upload
|
- codecov/upload
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
@@ -85,21 +62,16 @@ jobs:
|
|||||||
workflows:
|
workflows:
|
||||||
main:
|
main:
|
||||||
jobs:
|
jobs:
|
||||||
- pre-commit:
|
|
||||||
filters:
|
|
||||||
tags:
|
|
||||||
only: /.*/
|
|
||||||
- test:
|
- test:
|
||||||
matrix:
|
matrix:
|
||||||
parameters:
|
parameters:
|
||||||
tag: ["3.11"]
|
tag: ["3.7", "3.8", "3.9", "3.10", "3.11"]
|
||||||
filters:
|
filters:
|
||||||
tags:
|
tags:
|
||||||
only: /.*/
|
only: /.*/
|
||||||
- deploy:
|
- deploy:
|
||||||
context: pypi
|
context: pypi
|
||||||
requires:
|
requires:
|
||||||
- pre-commit
|
|
||||||
- test
|
- test
|
||||||
filters:
|
filters:
|
||||||
tags:
|
tags:
|
||||||
|
|||||||
@@ -22,6 +22,6 @@ RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/
|
|||||||
|
|
||||||
USER vscode
|
USER vscode
|
||||||
|
|
||||||
RUN curl -sSL https://install.python-poetry.org | python3 -
|
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
|
||||||
|
|
||||||
ENV PATH=$PATH:$HOME/.poetry/env
|
ENV PATH=$PATH:$HOME/.poetry/env
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
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 }}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
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 }}
|
||||||
@@ -2,23 +2,23 @@ repos:
|
|||||||
- repo: https://github.com/charliermarsh/ruff-pre-commit
|
- repo: https://github.com/charliermarsh/ruff-pre-commit
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
rev: v0.1.6
|
rev: v0.0.186
|
||||||
|
|
||||||
- repo: https://github.com/commitizen-tools/commitizen
|
- repo: https://github.com/commitizen-tools/commitizen
|
||||||
hooks:
|
hooks:
|
||||||
- id: commitizen
|
- id: commitizen
|
||||||
- id: commitizen-branch
|
- id: commitizen-branch
|
||||||
stages:
|
stages:
|
||||||
- push
|
- push
|
||||||
rev: v3.13.0
|
rev: v2.38.0
|
||||||
|
|
||||||
- repo: https://github.com/pycqa/isort
|
- repo: https://github.com/pycqa/isort
|
||||||
rev: 5.12.0
|
rev: v5.11.3
|
||||||
hooks:
|
hooks:
|
||||||
- id: isort
|
- id: isort
|
||||||
|
|
||||||
- repo: https://github.com/psf/black
|
- repo: https://github.com/psf/black
|
||||||
rev: 23.11.0
|
rev: 22.12.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: black
|
- id: black
|
||||||
# It is recommended to specify the latest version of Python
|
# It is recommended to specify the latest version of Python
|
||||||
@@ -27,7 +27,7 @@ repos:
|
|||||||
# https://pre-commit.com/#top_level-default_language_version
|
# https://pre-commit.com/#top_level-default_language_version
|
||||||
# language_version: python3.11
|
# language_version: python3.11
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v4.5.0
|
rev: v4.4.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ What is Speckle? Check our ](https://app.speckle.systems) ⇒ creating an account at our public server
|
- [](https://speckle.xyz) ⇒ creating an account at our public server
|
||||||
- [](https://marketplace.digitalocean.com/apps/speckle-server?refcode=947a2b5d7dc1) ⇒ deploying an instance in 1 click
|
- [](https://marketplace.digitalocean.com/apps/speckle-server?refcode=947a2b5d7dc1) ⇒ deploying an instance in 1 click
|
||||||
|
|
||||||
### Resources
|
### Resources
|
||||||
|
|||||||
@@ -1,162 +0,0 @@
|
|||||||
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:RELEASE.2023-10-25T06-33-25Z"
|
|
||||||
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:latest
|
|
||||||
restart: always
|
|
||||||
ports:
|
|
||||||
- "0.0.0.0:8080:8080"
|
|
||||||
environment:
|
|
||||||
FILE_SIZE_LIMIT_MB: 100
|
|
||||||
|
|
||||||
speckle-server:
|
|
||||||
image: speckle/speckle-server:latest
|
|
||||||
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:latest
|
|
||||||
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:latest
|
|
||||||
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:latest
|
|
||||||
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
+980
-1080
File diff suppressed because it is too large
Load Diff
+6
-10
@@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "specklepy"
|
name = "specklepy"
|
||||||
version = "2.17.14"
|
version = "2.9.1"
|
||||||
description = "The Python SDK for Speckle 2.0"
|
description = "The Python SDK for Speckle 2.0"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
authors = ["Speckle Systems <devops@speckle.systems>"]
|
authors = ["Speckle Systems <devops@speckle.systems>"]
|
||||||
@@ -10,34 +10,30 @@ documentation = "https://speckle.guide/dev/py-examples.html"
|
|||||||
homepage = "https://speckle.systems/"
|
homepage = "https://speckle.systems/"
|
||||||
packages = [
|
packages = [
|
||||||
{ include = "specklepy", from = "src" },
|
{ include = "specklepy", from = "src" },
|
||||||
{ include = "speckle_automate", from = "src" },
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = ">=3.8.0, <4.0"
|
python = ">=3.7.2, <4.0"
|
||||||
pydantic = "^2.5"
|
pydantic = "^1.9"
|
||||||
appdirs = "^1.4.4"
|
appdirs = "^1.4.4"
|
||||||
gql = { extras = ["requests", "websockets"], version = "^3.3.0" }
|
gql = {extras = ["requests", "websockets"], version = "^3.3.0"}
|
||||||
ujson = "^5.3.0"
|
ujson = "^5.3.0"
|
||||||
Deprecated = "^1.2.13"
|
Deprecated = "^1.2.13"
|
||||||
stringcase = "^1.2.0"
|
stringcase = "^1.2.0"
|
||||||
attrs = "^23.1.0"
|
|
||||||
httpx = "^0.25.0"
|
|
||||||
|
|
||||||
[tool.poetry.group.dev.dependencies]
|
[tool.poetry.group.dev.dependencies]
|
||||||
black = "23.11.0"
|
black = "^22.8.0"
|
||||||
isort = "^5.7.0"
|
isort = "^5.7.0"
|
||||||
pytest = "^7.1.3"
|
pytest = "^7.1.3"
|
||||||
pytest-ordering = "^0.6"
|
pytest-ordering = "^0.6"
|
||||||
pytest-cov = "^3.0.0"
|
pytest-cov = "^3.0.0"
|
||||||
devtools = "^0.8.0"
|
devtools = "^0.8.0"
|
||||||
pylint = "^2.14.4"
|
pylint = "^2.14.4"
|
||||||
pydantic-settings = "^2.3.0"
|
|
||||||
mypy = "^0.982"
|
mypy = "^0.982"
|
||||||
pre-commit = "^2.20.0"
|
pre-commit = "^2.20.0"
|
||||||
commitizen = "^2.38.0"
|
commitizen = "^2.38.0"
|
||||||
ruff = "^0.4.4"
|
ruff = "^0.0.187"
|
||||||
types-deprecated = "^1.2.9"
|
types-deprecated = "^1.2.9"
|
||||||
types-ujson = "^5.6.0.0"
|
types-ujson = "^5.6.0.0"
|
||||||
types-requests = "^2.28.11.5"
|
types-requests = "^2.28.11.5"
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
"""This module contains an SDK for working with Speckle Automate."""
|
|
||||||
|
|
||||||
from speckle_automate.automation_context import AutomationContext
|
|
||||||
from speckle_automate.runner import execute_automate_function, run_function
|
|
||||||
from speckle_automate.schema import (
|
|
||||||
AutomateBase,
|
|
||||||
AutomationResult,
|
|
||||||
AutomationRunData,
|
|
||||||
AutomationStatus,
|
|
||||||
ObjectResultLevel,
|
|
||||||
ResultCase,
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"AutomationContext",
|
|
||||||
"AutomateBase",
|
|
||||||
"AutomationStatus",
|
|
||||||
"AutomationResult",
|
|
||||||
"AutomationRunData",
|
|
||||||
"ResultCase",
|
|
||||||
"ObjectResultLevel",
|
|
||||||
"run_function",
|
|
||||||
"execute_automate_function",
|
|
||||||
]
|
|
||||||
@@ -1,406 +0,0 @@
|
|||||||
"""This module provides an abstraction layer above the Speckle Automate runtime."""
|
|
||||||
|
|
||||||
import time
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any, Dict, List, Optional, Tuple, Union
|
|
||||||
|
|
||||||
import httpx
|
|
||||||
from gql import gql
|
|
||||||
|
|
||||||
from speckle_automate.schema import (
|
|
||||||
AutomateBase,
|
|
||||||
AutomationResult,
|
|
||||||
AutomationRunData,
|
|
||||||
AutomationStatus,
|
|
||||||
ObjectResultLevel,
|
|
||||||
ResultCase,
|
|
||||||
)
|
|
||||||
from specklepy.api import operations
|
|
||||||
from specklepy.api.client import SpeckleClient
|
|
||||||
from specklepy.core.api.models import Branch
|
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
|
||||||
from specklepy.objects.base import Base
|
|
||||||
from specklepy.transports.memory import MemoryTransport
|
|
||||||
from specklepy.transports.server import ServerTransport
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class AutomationContext:
|
|
||||||
"""A context helper class.
|
|
||||||
|
|
||||||
This class exposes methods to work with the Speckle Automate context inside
|
|
||||||
Speckle Automate functions.
|
|
||||||
|
|
||||||
An instance of AutomationContext is injected into every run of a function.
|
|
||||||
"""
|
|
||||||
|
|
||||||
automation_run_data: AutomationRunData
|
|
||||||
speckle_client: SpeckleClient
|
|
||||||
_server_transport: ServerTransport
|
|
||||||
_speckle_token: str
|
|
||||||
|
|
||||||
#: keep a memory transponrt at hand, to speed up things if needed
|
|
||||||
_memory_transport: MemoryTransport = field(default_factory=MemoryTransport)
|
|
||||||
|
|
||||||
#: added for performance measuring
|
|
||||||
_init_time: float = field(default_factory=time.perf_counter)
|
|
||||||
_automation_result: AutomationResult = field(default_factory=AutomationResult)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def initialize(
|
|
||||||
cls, automation_run_data: Union[str, AutomationRunData], speckle_token: str
|
|
||||||
) -> "AutomationContext":
|
|
||||||
"""Bootstrap the AutomateSDK from raw data.
|
|
||||||
|
|
||||||
Todo:
|
|
||||||
----
|
|
||||||
* bootstrap a structlog logger instance
|
|
||||||
* expose a logger, that ppl can use instead of print
|
|
||||||
"""
|
|
||||||
# parse the json value if its not an initialized project data instance
|
|
||||||
automation_run_data = (
|
|
||||||
automation_run_data
|
|
||||||
if isinstance(automation_run_data, AutomationRunData)
|
|
||||||
else AutomationRunData.model_validate_json(automation_run_data)
|
|
||||||
)
|
|
||||||
speckle_client = SpeckleClient(
|
|
||||||
automation_run_data.speckle_server_url,
|
|
||||||
automation_run_data.speckle_server_url.startswith("https"),
|
|
||||||
)
|
|
||||||
speckle_client.authenticate_with_token(speckle_token)
|
|
||||||
if not speckle_client.account:
|
|
||||||
msg = (
|
|
||||||
f"Could not autenticate to {automation_run_data.speckle_server_url}",
|
|
||||||
"with the provided token",
|
|
||||||
)
|
|
||||||
raise ValueError(msg)
|
|
||||||
server_transport = ServerTransport(
|
|
||||||
automation_run_data.project_id, speckle_client
|
|
||||||
)
|
|
||||||
return cls(automation_run_data, speckle_client, server_transport, speckle_token)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def run_status(self) -> AutomationStatus:
|
|
||||||
"""Get the status of the automation run."""
|
|
||||||
return self._automation_result.run_status
|
|
||||||
|
|
||||||
@property
|
|
||||||
def status_message(self) -> Optional[str]:
|
|
||||||
"""Get the current status message."""
|
|
||||||
return self._automation_result.status_message
|
|
||||||
|
|
||||||
def elapsed(self) -> float:
|
|
||||||
"""Return the elapsed time in seconds since the initialization time."""
|
|
||||||
return time.perf_counter() - self._init_time
|
|
||||||
|
|
||||||
def receive_version(self) -> Base:
|
|
||||||
"""Receive the Speckle project version that triggered this automation run."""
|
|
||||||
# TODO: this is a quick hack to keep implementation consistency. Move to proper receive many versions
|
|
||||||
version_id = self.automation_run_data.triggers[0].payload.version_id
|
|
||||||
commit = self.speckle_client.commit.get(
|
|
||||||
self.automation_run_data.project_id, version_id
|
|
||||||
)
|
|
||||||
if not commit.referencedObject:
|
|
||||||
raise ValueError("The commit has no referencedObject, cannot receive it.")
|
|
||||||
base = operations.receive(
|
|
||||||
commit.referencedObject, self._server_transport, self._memory_transport
|
|
||||||
)
|
|
||||||
print(
|
|
||||||
f"It took {self.elapsed():.2f} seconds to receive",
|
|
||||||
f" the speckle version {version_id}",
|
|
||||||
)
|
|
||||||
return base
|
|
||||||
|
|
||||||
def create_new_version_in_project(
|
|
||||||
self, root_object: Base, model_name: str, version_message: str = ""
|
|
||||||
) -> Tuple[str, str]:
|
|
||||||
"""Save a base model to a new version on the project.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
root_object (Base): The Speckle base object for the new version.
|
|
||||||
model_id (str): For now please use a `branchName`!
|
|
||||||
version_message (str): The message for the new version.
|
|
||||||
"""
|
|
||||||
|
|
||||||
branch = self.speckle_client.branch.get(
|
|
||||||
self.automation_run_data.project_id, model_name, 1
|
|
||||||
)
|
|
||||||
if isinstance(branch, Branch):
|
|
||||||
if not branch.id:
|
|
||||||
raise ValueError("Cannot use the branch without its id")
|
|
||||||
matching_trigger = [
|
|
||||||
t
|
|
||||||
for t in self.automation_run_data.triggers
|
|
||||||
if t.payload.model_id == branch.id
|
|
||||||
]
|
|
||||||
if matching_trigger:
|
|
||||||
raise ValueError(
|
|
||||||
f"The target model: {model_name} cannot match the model"
|
|
||||||
f" that triggered this automation:"
|
|
||||||
f" {matching_trigger[0].payload.model_id}"
|
|
||||||
)
|
|
||||||
model_id = branch.id
|
|
||||||
|
|
||||||
else:
|
|
||||||
# we just check if it exists
|
|
||||||
branch_create = self.speckle_client.branch.create(
|
|
||||||
self.automation_run_data.project_id,
|
|
||||||
model_name,
|
|
||||||
)
|
|
||||||
if isinstance(branch_create, Exception):
|
|
||||||
raise branch_create
|
|
||||||
model_id = branch_create
|
|
||||||
|
|
||||||
root_object_id = operations.send(
|
|
||||||
root_object,
|
|
||||||
[self._server_transport, self._memory_transport],
|
|
||||||
use_default_cache=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
version_id = self.speckle_client.commit.create(
|
|
||||||
stream_id=self.automation_run_data.project_id,
|
|
||||||
object_id=root_object_id,
|
|
||||||
branch_name=model_name,
|
|
||||||
message=version_message,
|
|
||||||
source_application="SpeckleAutomate",
|
|
||||||
)
|
|
||||||
|
|
||||||
if isinstance(version_id, SpeckleException):
|
|
||||||
raise version_id
|
|
||||||
|
|
||||||
self._automation_result.result_versions.append(version_id)
|
|
||||||
return model_id, version_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def context_view(self) -> Optional[str]:
|
|
||||||
return self._automation_result.result_view
|
|
||||||
|
|
||||||
def set_context_view(
|
|
||||||
self,
|
|
||||||
# f"{model_id}@{version_id} or {model_id} "
|
|
||||||
resource_ids: Optional[List[str]] = None,
|
|
||||||
include_source_model_version: bool = True,
|
|
||||||
) -> None:
|
|
||||||
link_resources = (
|
|
||||||
[
|
|
||||||
f"{t.payload.model_id}@{t.payload.version_id}"
|
|
||||||
for t in self.automation_run_data.triggers
|
|
||||||
]
|
|
||||||
if include_source_model_version
|
|
||||||
else []
|
|
||||||
)
|
|
||||||
if resource_ids:
|
|
||||||
link_resources.extend(resource_ids)
|
|
||||||
if not link_resources:
|
|
||||||
raise Exception(
|
|
||||||
"We do not have enough resource ids to compose a context view"
|
|
||||||
)
|
|
||||||
self._automation_result.result_view = (
|
|
||||||
f"/projects/{self.automation_run_data.project_id}"
|
|
||||||
f"/models/{','.join(link_resources)}"
|
|
||||||
)
|
|
||||||
|
|
||||||
def report_run_status(self) -> None:
|
|
||||||
"""Report the current run status to the project of this automation."""
|
|
||||||
query = gql(
|
|
||||||
"""
|
|
||||||
mutation AutomateFunctionRunStatusReport(
|
|
||||||
$functionRunId: String!
|
|
||||||
$status: AutomateRunStatus!
|
|
||||||
$statusMessage: String
|
|
||||||
$results: JSONObject
|
|
||||||
$contextView: String
|
|
||||||
){
|
|
||||||
automateFunctionRunStatusReport(input: {
|
|
||||||
functionRunId: $functionRunId
|
|
||||||
status: $status
|
|
||||||
statusMessage: $statusMessage
|
|
||||||
contextView: $contextView
|
|
||||||
results: $results
|
|
||||||
})
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
if self.run_status in [AutomationStatus.SUCCEEDED, AutomationStatus.FAILED]:
|
|
||||||
object_results = {
|
|
||||||
"version": 1,
|
|
||||||
"values": {
|
|
||||||
"objectResults": self._automation_result.model_dump(by_alias=True)[
|
|
||||||
"objectResults"
|
|
||||||
],
|
|
||||||
"blobIds": self._automation_result.blobs,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
object_results = None
|
|
||||||
|
|
||||||
params = {
|
|
||||||
"functionRunId": self.automation_run_data.function_run_id,
|
|
||||||
"status": self.run_status.value,
|
|
||||||
"statusMessage": self._automation_result.status_message,
|
|
||||||
"results": object_results,
|
|
||||||
"contextView": self._automation_result.result_view,
|
|
||||||
}
|
|
||||||
print(f"Reporting run status with content: {params}")
|
|
||||||
self.speckle_client.httpclient.execute(query, params)
|
|
||||||
|
|
||||||
def store_file_result(self, file_path: Union[Path, str]) -> None:
|
|
||||||
"""Save a file attached to the project of this automation."""
|
|
||||||
path_obj = (
|
|
||||||
Path(file_path).resolve() if isinstance(file_path, str) else file_path
|
|
||||||
)
|
|
||||||
|
|
||||||
class UploadResult(AutomateBase):
|
|
||||||
blob_id: str
|
|
||||||
file_name: str
|
|
||||||
upload_status: int
|
|
||||||
|
|
||||||
class BlobUploadResponse(AutomateBase):
|
|
||||||
upload_results: list[UploadResult]
|
|
||||||
|
|
||||||
if not path_obj.exists():
|
|
||||||
raise ValueError("The given file path doesn't exist")
|
|
||||||
files = {path_obj.name: open(str(path_obj), "rb")}
|
|
||||||
|
|
||||||
url = (
|
|
||||||
f"{self.automation_run_data.speckle_server_url}/api/stream/"
|
|
||||||
f"{self.automation_run_data.project_id}/blob"
|
|
||||||
)
|
|
||||||
data = (
|
|
||||||
httpx.post(
|
|
||||||
url,
|
|
||||||
files=files,
|
|
||||||
headers={"authorization": f"Bearer {self._speckle_token}"},
|
|
||||||
)
|
|
||||||
.raise_for_status()
|
|
||||||
.json()
|
|
||||||
)
|
|
||||||
|
|
||||||
upload_response = BlobUploadResponse.model_validate(data)
|
|
||||||
|
|
||||||
if len(upload_response.upload_results) != 1:
|
|
||||||
raise ValueError("Expecting one upload result.")
|
|
||||||
|
|
||||||
self._automation_result.blobs.extend(
|
|
||||||
[upload_result.blob_id for upload_result in upload_response.upload_results]
|
|
||||||
)
|
|
||||||
|
|
||||||
def mark_run_failed(self, status_message: str) -> None:
|
|
||||||
"""Mark the current run a failure."""
|
|
||||||
self._mark_run(AutomationStatus.FAILED, status_message)
|
|
||||||
|
|
||||||
def mark_run_exception(self, status_message: str) -> None:
|
|
||||||
"""Mark the current run a failure."""
|
|
||||||
self._mark_run(AutomationStatus.EXCEPTION, status_message)
|
|
||||||
|
|
||||||
def mark_run_success(self, status_message: Optional[str]) -> None:
|
|
||||||
"""Mark the current run a success with an optional message."""
|
|
||||||
self._mark_run(AutomationStatus.SUCCEEDED, status_message)
|
|
||||||
|
|
||||||
def _mark_run(
|
|
||||||
self, status: AutomationStatus, status_message: Optional[str]
|
|
||||||
) -> None:
|
|
||||||
duration = self.elapsed()
|
|
||||||
self._automation_result.status_message = status_message
|
|
||||||
self._automation_result.run_status = status
|
|
||||||
self._automation_result.elapsed = duration
|
|
||||||
|
|
||||||
msg = f"Automation run {status.value} after {duration:.2f} seconds."
|
|
||||||
print("\n".join([msg, status_message]) if status_message else msg)
|
|
||||||
|
|
||||||
def attach_error_to_objects(
|
|
||||||
self,
|
|
||||||
category: str,
|
|
||||||
object_ids: Union[str, List[str]],
|
|
||||||
message: Optional[str] = None,
|
|
||||||
metadata: Optional[Dict[str, Any]] = None,
|
|
||||||
visual_overrides: Optional[Dict[str, Any]] = None,
|
|
||||||
) -> None:
|
|
||||||
"""Add a new error case to the run results.
|
|
||||||
|
|
||||||
If the error cause has already created an error case,
|
|
||||||
the error will be extended with a new case refering to the causing objects.
|
|
||||||
Args:
|
|
||||||
error_tag (str): A short tag for the error type.
|
|
||||||
causing_object_ids (str[]): A list of object_id-s that are causing the error
|
|
||||||
error_messagge (Optional[str]): Optional error message.
|
|
||||||
metadata: User provided metadata key value pairs
|
|
||||||
visual_overrides: Case specific 3D visual overrides.
|
|
||||||
"""
|
|
||||||
self.attach_result_to_objects(
|
|
||||||
ObjectResultLevel.ERROR,
|
|
||||||
category,
|
|
||||||
object_ids,
|
|
||||||
message,
|
|
||||||
metadata,
|
|
||||||
visual_overrides,
|
|
||||||
)
|
|
||||||
|
|
||||||
def attach_warning_to_objects(
|
|
||||||
self,
|
|
||||||
category: str,
|
|
||||||
object_ids: Union[str, List[str]],
|
|
||||||
message: Optional[str] = None,
|
|
||||||
metadata: Optional[Dict[str, Any]] = None,
|
|
||||||
visual_overrides: Optional[Dict[str, Any]] = None,
|
|
||||||
) -> None:
|
|
||||||
"""Add a new warning case to the run results."""
|
|
||||||
self.attach_result_to_objects(
|
|
||||||
ObjectResultLevel.WARNING,
|
|
||||||
category,
|
|
||||||
object_ids,
|
|
||||||
message,
|
|
||||||
metadata,
|
|
||||||
visual_overrides,
|
|
||||||
)
|
|
||||||
|
|
||||||
def attach_info_to_objects(
|
|
||||||
self,
|
|
||||||
category: str,
|
|
||||||
object_ids: Union[str, List[str]],
|
|
||||||
message: Optional[str] = None,
|
|
||||||
metadata: Optional[Dict[str, Any]] = None,
|
|
||||||
visual_overrides: Optional[Dict[str, Any]] = None,
|
|
||||||
) -> None:
|
|
||||||
"""Add a new info case to the run results."""
|
|
||||||
self.attach_result_to_objects(
|
|
||||||
ObjectResultLevel.INFO,
|
|
||||||
category,
|
|
||||||
object_ids,
|
|
||||||
message,
|
|
||||||
metadata,
|
|
||||||
visual_overrides,
|
|
||||||
)
|
|
||||||
|
|
||||||
def attach_result_to_objects(
|
|
||||||
self,
|
|
||||||
level: ObjectResultLevel,
|
|
||||||
category: str,
|
|
||||||
object_ids: Union[str, List[str]],
|
|
||||||
message: Optional[str] = None,
|
|
||||||
metadata: Optional[Dict[str, Any]] = None,
|
|
||||||
visual_overrides: Optional[Dict[str, Any]] = None,
|
|
||||||
) -> None:
|
|
||||||
if isinstance(object_ids, list):
|
|
||||||
if len(object_ids) < 1:
|
|
||||||
raise ValueError(
|
|
||||||
f"Need atleast one object_id to report a(n) {level.value.upper()}"
|
|
||||||
)
|
|
||||||
id_list = object_ids
|
|
||||||
else:
|
|
||||||
id_list = [object_ids]
|
|
||||||
print(
|
|
||||||
f"Created new {level.value.upper()}"
|
|
||||||
f" category: {category} caused by: {message}"
|
|
||||||
)
|
|
||||||
self._automation_result.object_results.append(
|
|
||||||
ResultCase(
|
|
||||||
category=category,
|
|
||||||
level=level,
|
|
||||||
object_ids=id_list,
|
|
||||||
message=message,
|
|
||||||
metadata=metadata,
|
|
||||||
visual_overrides=visual_overrides,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
@@ -1,154 +0,0 @@
|
|||||||
"""Some useful helpers for working with automation data."""
|
|
||||||
import secrets
|
|
||||||
import string
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
from gql import gql
|
|
||||||
from pydantic import Field
|
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
||||||
|
|
||||||
from speckle_automate.schema import AutomationRunData, TestAutomationRunData
|
|
||||||
from specklepy.api.client import SpeckleClient
|
|
||||||
|
|
||||||
|
|
||||||
class TestAutomationEnvironment(BaseSettings):
|
|
||||||
"""Get known environment variables from local `.env` file"""
|
|
||||||
|
|
||||||
model_config = SettingsConfigDict(
|
|
||||||
env_file=".env",
|
|
||||||
env_file_encoding="utf-8",
|
|
||||||
env_prefix="speckle_",
|
|
||||||
extra="ignore",
|
|
||||||
)
|
|
||||||
|
|
||||||
token: str = Field()
|
|
||||||
server_url: str = Field()
|
|
||||||
project_id: str = Field()
|
|
||||||
automation_id: str = Field()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def test_automation_environment() -> TestAutomationEnvironment:
|
|
||||||
return TestAutomationEnvironment()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def test_automation_token(
|
|
||||||
test_automation_environment: TestAutomationEnvironment,
|
|
||||||
) -> str:
|
|
||||||
"""Provide a speckle token for the test suite."""
|
|
||||||
|
|
||||||
return test_automation_environment.token
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def speckle_client(
|
|
||||||
test_automation_environment: TestAutomationEnvironment,
|
|
||||||
) -> SpeckleClient:
|
|
||||||
"""Initialize a SpeckleClient for testing."""
|
|
||||||
speckle_client = SpeckleClient(
|
|
||||||
test_automation_environment.server_url,
|
|
||||||
test_automation_environment.server_url.startswith("https"),
|
|
||||||
)
|
|
||||||
speckle_client.authenticate_with_token(test_automation_environment.token)
|
|
||||||
return speckle_client
|
|
||||||
|
|
||||||
|
|
||||||
def create_test_automation_run(
|
|
||||||
speckle_client: SpeckleClient, project_id: str, test_automation_id: str
|
|
||||||
) -> TestAutomationRunData:
|
|
||||||
"""Create test run to report local test results to"""
|
|
||||||
|
|
||||||
query = gql(
|
|
||||||
"""
|
|
||||||
mutation CreateTestRun(
|
|
||||||
$projectId: ID!,
|
|
||||||
$automationId: ID!
|
|
||||||
) {
|
|
||||||
projectMutations {
|
|
||||||
automationMutations(projectId: $projectId) {
|
|
||||||
createTestAutomationRun(automationId: $automationId) {
|
|
||||||
automationRunId
|
|
||||||
functionRunId
|
|
||||||
triggers {
|
|
||||||
payload {
|
|
||||||
modelId
|
|
||||||
versionId
|
|
||||||
}
|
|
||||||
triggerType
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
params = {"automationId": test_automation_id, "projectId": project_id}
|
|
||||||
|
|
||||||
result = speckle_client.httpclient.execute(query, params)
|
|
||||||
|
|
||||||
print(result)
|
|
||||||
|
|
||||||
return (
|
|
||||||
result.get("projectMutations")
|
|
||||||
.get("automationMutations")
|
|
||||||
.get("createTestAutomationRun")
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def test_automation_run(
|
|
||||||
speckle_client: SpeckleClient,
|
|
||||||
test_automation_environment: TestAutomationEnvironment,
|
|
||||||
) -> TestAutomationRunData:
|
|
||||||
return create_test_automation_run(
|
|
||||||
speckle_client,
|
|
||||||
test_automation_environment.project_id,
|
|
||||||
test_automation_environment.automation_id,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def create_test_automation_run_data(
|
|
||||||
speckle_client: SpeckleClient,
|
|
||||||
test_automation_environment: TestAutomationEnvironment,
|
|
||||||
) -> AutomationRunData:
|
|
||||||
"""Create automation run data for a new run for a given test automation"""
|
|
||||||
|
|
||||||
test_automation_run_data = create_test_automation_run(
|
|
||||||
speckle_client,
|
|
||||||
test_automation_environment.project_id,
|
|
||||||
test_automation_environment.automation_id,
|
|
||||||
)
|
|
||||||
|
|
||||||
return AutomationRunData(
|
|
||||||
project_id=test_automation_environment.project_id,
|
|
||||||
speckle_server_url=test_automation_environment.server_url,
|
|
||||||
automation_id=test_automation_environment.automation_id,
|
|
||||||
automation_run_id=test_automation_run_data["automationRunId"],
|
|
||||||
function_run_id=test_automation_run_data["functionRunId"],
|
|
||||||
triggers=test_automation_run_data["triggers"],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def test_automation_run_data(
|
|
||||||
speckle_client: SpeckleClient,
|
|
||||||
test_automation_environment: TestAutomationEnvironment,
|
|
||||||
) -> AutomationRunData:
|
|
||||||
return create_test_automation_run_data(speckle_client, test_automation_environment)
|
|
||||||
|
|
||||||
|
|
||||||
def crypto_random_string(length: int) -> str:
|
|
||||||
"""Generate a semi crypto random string of a given length."""
|
|
||||||
alphabet = string.ascii_letters + string.digits
|
|
||||||
return "".join(secrets.choice(alphabet) for _ in range(length)).lower()
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"test_automation_environment",
|
|
||||||
"test_automation_token",
|
|
||||||
"speckle_client",
|
|
||||||
"test_automation_run",
|
|
||||||
"test_automation_run_data",
|
|
||||||
]
|
|
||||||
@@ -1,197 +0,0 @@
|
|||||||
"""Function execution module.
|
|
||||||
|
|
||||||
Provides mechanisms to execute any function,
|
|
||||||
that conforms to the AutomateFunction "interface"
|
|
||||||
"""
|
|
||||||
|
|
||||||
import json
|
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Callable, Optional, Tuple, TypeVar, Union, overload
|
|
||||||
|
|
||||||
from pydantic import create_model
|
|
||||||
from pydantic.json_schema import GenerateJsonSchema
|
|
||||||
|
|
||||||
from speckle_automate.automation_context import AutomationContext
|
|
||||||
from speckle_automate.schema import AutomateBase, AutomationRunData, AutomationStatus
|
|
||||||
|
|
||||||
T = TypeVar("T", bound=AutomateBase)
|
|
||||||
|
|
||||||
AutomateFunction = Callable[[AutomationContext, T], None]
|
|
||||||
AutomateFunctionWithoutInputs = Callable[[AutomationContext], None]
|
|
||||||
|
|
||||||
|
|
||||||
def _read_input_data(inputs_location: str) -> str:
|
|
||||||
input_path = Path(inputs_location)
|
|
||||||
if not input_path.exists():
|
|
||||||
raise ValueError(f"Cannot find the function inputs file at {input_path}")
|
|
||||||
|
|
||||||
return input_path.read_text()
|
|
||||||
|
|
||||||
|
|
||||||
def _parse_input_data(
|
|
||||||
input_location: str, input_schema: Optional[type[T]]
|
|
||||||
) -> Tuple[AutomationRunData, Optional[T], str]:
|
|
||||||
input_json_string = _read_input_data(input_location)
|
|
||||||
|
|
||||||
class FunctionRunData(AutomateBase):
|
|
||||||
speckle_token: str
|
|
||||||
automation_run_data: AutomationRunData
|
|
||||||
function_inputs: None = None
|
|
||||||
|
|
||||||
parser_model = FunctionRunData
|
|
||||||
|
|
||||||
if input_schema:
|
|
||||||
parser_model = create_model(
|
|
||||||
"FunctionRunDataWithInputs",
|
|
||||||
function_inputs=(input_schema, ...),
|
|
||||||
__base__=FunctionRunData,
|
|
||||||
)
|
|
||||||
|
|
||||||
input_data = parser_model.model_validate_json(input_json_string)
|
|
||||||
return (
|
|
||||||
input_data.automation_run_data,
|
|
||||||
input_data.function_inputs,
|
|
||||||
input_data.speckle_token,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@overload
|
|
||||||
def execute_automate_function(
|
|
||||||
automate_function: AutomateFunction[T],
|
|
||||||
input_schema: type[T],
|
|
||||||
) -> None:
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
@overload
|
|
||||||
def execute_automate_function(
|
|
||||||
automate_function: AutomateFunctionWithoutInputs,
|
|
||||||
) -> None:
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
class AutomateGenerateJsonSchema(GenerateJsonSchema):
|
|
||||||
def generate(self, schema, mode="validation"):
|
|
||||||
json_schema = super().generate(schema, mode=mode)
|
|
||||||
json_schema["$schema"] = self.schema_dialect
|
|
||||||
return json_schema
|
|
||||||
|
|
||||||
|
|
||||||
def execute_automate_function(
|
|
||||||
automate_function: Union[AutomateFunction[T], AutomateFunctionWithoutInputs],
|
|
||||||
input_schema: Optional[type[T]] = None,
|
|
||||||
):
|
|
||||||
"""Runs the provided automate function with the input schema."""
|
|
||||||
# first arg is the python file name, we do not need that
|
|
||||||
args = sys.argv[1:]
|
|
||||||
|
|
||||||
if len(args) != 2:
|
|
||||||
raise ValueError("Incorrect number of arguments specified need 2")
|
|
||||||
|
|
||||||
# we rely on a command name convention to decide what to do.
|
|
||||||
# this is here, so that the function authors do not see any of this
|
|
||||||
command, argument = args
|
|
||||||
|
|
||||||
if command == "generate_schema":
|
|
||||||
path = Path(argument)
|
|
||||||
schema = json.dumps(
|
|
||||||
input_schema.model_json_schema(
|
|
||||||
by_alias=True, schema_generator=AutomateGenerateJsonSchema
|
|
||||||
)
|
|
||||||
if input_schema
|
|
||||||
else {}
|
|
||||||
)
|
|
||||||
path.write_text(schema)
|
|
||||||
|
|
||||||
elif command == "run":
|
|
||||||
automation_run_data, function_inputs, speckle_token = _parse_input_data(
|
|
||||||
argument, input_schema
|
|
||||||
)
|
|
||||||
|
|
||||||
automation_context = AutomationContext.initialize(
|
|
||||||
automation_run_data, speckle_token
|
|
||||||
)
|
|
||||||
|
|
||||||
if function_inputs:
|
|
||||||
automation_context = run_function(
|
|
||||||
automation_context,
|
|
||||||
automate_function, # type: ignore
|
|
||||||
function_inputs, # type: ignore
|
|
||||||
)
|
|
||||||
|
|
||||||
else:
|
|
||||||
automation_context = AutomationContext.initialize(
|
|
||||||
automation_run_data, speckle_token
|
|
||||||
)
|
|
||||||
automation_context = run_function(
|
|
||||||
automation_context,
|
|
||||||
automate_function, # type: ignore
|
|
||||||
)
|
|
||||||
|
|
||||||
# if we've gotten this far, the execution should technically be completed as expected
|
|
||||||
# thus exiting with 0 is the schemantically correct thing to do
|
|
||||||
exit_code = (
|
|
||||||
1 if automation_context.run_status == AutomationStatus.EXCEPTION else 0
|
|
||||||
)
|
|
||||||
exit(exit_code)
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(f"Command: '{command}' is not supported.")
|
|
||||||
|
|
||||||
|
|
||||||
@overload
|
|
||||||
def run_function(
|
|
||||||
automation_context: AutomationContext,
|
|
||||||
automate_function: AutomateFunction[T],
|
|
||||||
inputs: T,
|
|
||||||
) -> AutomationContext:
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
@overload
|
|
||||||
def run_function(
|
|
||||||
automation_context: AutomationContext,
|
|
||||||
automate_function: AutomateFunctionWithoutInputs,
|
|
||||||
) -> AutomationContext:
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
def run_function(
|
|
||||||
automation_context: AutomationContext,
|
|
||||||
automate_function: Union[AutomateFunction[T], AutomateFunctionWithoutInputs],
|
|
||||||
inputs: Optional[T] = None,
|
|
||||||
) -> AutomationContext:
|
|
||||||
"""Run the provided function with the automate sdk context."""
|
|
||||||
automation_context.report_run_status()
|
|
||||||
|
|
||||||
try:
|
|
||||||
# avoiding complex type gymnastics here on the internals.
|
|
||||||
# the external type overloads make this correct
|
|
||||||
if inputs:
|
|
||||||
automate_function(automation_context, inputs) # type: ignore
|
|
||||||
else:
|
|
||||||
automate_function(automation_context) # type: ignore
|
|
||||||
|
|
||||||
# the function author forgot to mark the function success
|
|
||||||
if automation_context.run_status not in [
|
|
||||||
AutomationStatus.FAILED,
|
|
||||||
AutomationStatus.SUCCEEDED,
|
|
||||||
AutomationStatus.EXCEPTION,
|
|
||||||
]:
|
|
||||||
automation_context.mark_run_success(
|
|
||||||
"WARNING: Automate assumed a success status,"
|
|
||||||
" but it was not marked as so by the function."
|
|
||||||
)
|
|
||||||
except Exception:
|
|
||||||
trace = traceback.format_exc()
|
|
||||||
print(trace)
|
|
||||||
automation_context.mark_run_exception(
|
|
||||||
"Function error. Check the automation run logs for details."
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
if not automation_context.context_view:
|
|
||||||
automation_context.set_context_view()
|
|
||||||
automation_context.report_run_status()
|
|
||||||
return automation_context
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
""""""
|
|
||||||
|
|
||||||
from enum import Enum
|
|
||||||
from typing import Any, Dict, List, Literal, Optional
|
|
||||||
|
|
||||||
from pydantic import BaseModel, ConfigDict, Field
|
|
||||||
from stringcase import camelcase
|
|
||||||
|
|
||||||
|
|
||||||
class AutomateBase(BaseModel):
|
|
||||||
"""Use this class as a base model for automate related DTO."""
|
|
||||||
|
|
||||||
model_config = ConfigDict(alias_generator=camelcase, populate_by_name=True)
|
|
||||||
|
|
||||||
|
|
||||||
class VersionCreationTriggerPayload(AutomateBase):
|
|
||||||
"""Represents the version creation trigger payload."""
|
|
||||||
|
|
||||||
model_id: str
|
|
||||||
version_id: str
|
|
||||||
|
|
||||||
|
|
||||||
class VersionCreationTrigger(AutomateBase):
|
|
||||||
"""Represents a single version creation trigger for the automation run."""
|
|
||||||
|
|
||||||
trigger_type: Literal["versionCreation"]
|
|
||||||
payload: VersionCreationTriggerPayload
|
|
||||||
|
|
||||||
|
|
||||||
class AutomationRunData(BaseModel):
|
|
||||||
"""Values of the project / model that triggered the run of this function."""
|
|
||||||
|
|
||||||
project_id: str
|
|
||||||
speckle_server_url: str
|
|
||||||
automation_id: str
|
|
||||||
automation_run_id: str
|
|
||||||
function_run_id: str
|
|
||||||
|
|
||||||
triggers: List[VersionCreationTrigger]
|
|
||||||
|
|
||||||
model_config = ConfigDict(
|
|
||||||
alias_generator=camelcase, populate_by_name=True, protected_namespaces=()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestAutomationRunData(BaseModel):
|
|
||||||
"""Values of the run created in the test automation for local test results."""
|
|
||||||
|
|
||||||
automation_run_id: str
|
|
||||||
function_run_id: str
|
|
||||||
|
|
||||||
triggers: List[VersionCreationTrigger]
|
|
||||||
|
|
||||||
model_config = ConfigDict(
|
|
||||||
alias_generator=camelcase, populate_by_name=True, protected_namespaces=()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class AutomationStatus(str, Enum):
|
|
||||||
"""Set the status of the automation."""
|
|
||||||
|
|
||||||
INITIALIZING = "INITIALIZING"
|
|
||||||
RUNNING = "RUNNING"
|
|
||||||
FAILED = "FAILED"
|
|
||||||
SUCCEEDED = "SUCCEEDED"
|
|
||||||
EXCEPTION = "EXCEPTION"
|
|
||||||
|
|
||||||
|
|
||||||
class ObjectResultLevel(str, Enum):
|
|
||||||
"""Possible status message levels for object reports."""
|
|
||||||
|
|
||||||
INFO = "INFO"
|
|
||||||
WARNING = "WARNING"
|
|
||||||
ERROR = "ERROR"
|
|
||||||
|
|
||||||
|
|
||||||
class ResultCase(AutomateBase):
|
|
||||||
"""A result case."""
|
|
||||||
|
|
||||||
category: str
|
|
||||||
level: ObjectResultLevel
|
|
||||||
object_ids: List[str]
|
|
||||||
message: Optional[str]
|
|
||||||
metadata: Optional[Dict[str, Any]]
|
|
||||||
visual_overrides: Optional[Dict[str, Any]]
|
|
||||||
|
|
||||||
|
|
||||||
class AutomationResult(AutomateBase):
|
|
||||||
"""Schema accepted by the Speckle server as a result for an automation run."""
|
|
||||||
|
|
||||||
elapsed: float = 0
|
|
||||||
result_view: Optional[str] = None
|
|
||||||
result_versions: List[str] = Field(default_factory=list)
|
|
||||||
blobs: List[str] = Field(default_factory=list)
|
|
||||||
run_status: AutomationStatus = AutomationStatus.RUNNING
|
|
||||||
status_message: Optional[str] = None
|
|
||||||
object_results: list[ResultCase] = Field(default_factory=list)
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
from specklepy import objects
|
|
||||||
|
|
||||||
__all__ = ["objects"]
|
|
||||||
|
|||||||
+151
-65
@@ -1,6 +1,15 @@
|
|||||||
from deprecated import deprecated
|
import re
|
||||||
|
from typing import Dict
|
||||||
|
from warnings import warn
|
||||||
|
|
||||||
from specklepy.api.credentials import Account
|
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 (
|
from specklepy.api.resources import (
|
||||||
active_user,
|
active_user,
|
||||||
branch,
|
branch,
|
||||||
@@ -12,16 +21,16 @@ from specklepy.api.resources import (
|
|||||||
subscriptions,
|
subscriptions,
|
||||||
user,
|
user,
|
||||||
)
|
)
|
||||||
from specklepy.core.api.client import SpeckleClient as CoreSpeckleClient
|
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
|
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
||||||
|
|
||||||
|
|
||||||
class SpeckleClient(CoreSpeckleClient):
|
class SpeckleClient:
|
||||||
"""
|
"""
|
||||||
The `SpeckleClient` is your entry point for interacting with
|
The `SpeckleClient` is your entry point for interacting with
|
||||||
your Speckle Server's GraphQL API.
|
your Speckle Server's GraphQL API.
|
||||||
You'll need to have access to a server to use it,
|
You'll need to have access to a server to use it,
|
||||||
or you can use our public server `app.speckle.systems`.
|
or you can use our public server `speckle.xyz`.
|
||||||
|
|
||||||
To authenticate the client, you'll need to have downloaded
|
To authenticate the client, you'll need to have downloaded
|
||||||
the [Speckle Manager](https://speckle.guide/#speckle-manager)
|
the [Speckle Manager](https://speckle.guide/#speckle-manager)
|
||||||
@@ -32,7 +41,7 @@ class SpeckleClient(CoreSpeckleClient):
|
|||||||
from specklepy.api.credentials import get_default_account
|
from specklepy.api.credentials import get_default_account
|
||||||
|
|
||||||
# initialise the client
|
# initialise the client
|
||||||
client = SpeckleClient(host="app.speckle.systems") # or whatever your host is
|
client = SpeckleClient(host="speckle.xyz") # or whatever your host is
|
||||||
# client = SpeckleClient(host="localhost:3000", use_ssl=False) or use local server
|
# 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)
|
# authenticate the client with an account (account has been added in Speckle Manager)
|
||||||
@@ -47,22 +56,136 @@ class SpeckleClient(CoreSpeckleClient):
|
|||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DEFAULT_HOST = "app.speckle.systems"
|
DEFAULT_HOST = "speckle.xyz"
|
||||||
USE_SSL = True
|
USE_SSL = True
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, host: str = DEFAULT_HOST, use_ssl: bool = USE_SSL) -> None:
|
||||||
self,
|
metrics.track(metrics.CLIENT, custom_props={"name": "create"})
|
||||||
host: str = DEFAULT_HOST,
|
ws_protocol = "ws"
|
||||||
use_ssl: bool = USE_SSL,
|
http_protocol = "http"
|
||||||
verify_certificate: bool = True,
|
|
||||||
) -> None:
|
if use_ssl:
|
||||||
super().__init__(
|
ws_protocol = "wss"
|
||||||
host=host,
|
http_protocol = "https"
|
||||||
use_ssl=use_ssl,
|
|
||||||
verify_certificate=verify_certificate,
|
# 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.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()
|
||||||
|
|
||||||
|
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:
|
def _init_resources(self) -> None:
|
||||||
self.server = server.Resource(
|
self.server = server.Resource(
|
||||||
account=self.account, basepath=self.url, client=self.httpclient
|
account=self.account, basepath=self.url, client=self.httpclient
|
||||||
@@ -111,50 +234,13 @@ class SpeckleClient(CoreSpeckleClient):
|
|||||||
client=self.wsclient,
|
client=self.wsclient,
|
||||||
)
|
)
|
||||||
|
|
||||||
@deprecated(
|
def __getattr__(self, name):
|
||||||
version="2.6.0",
|
try:
|
||||||
reason=(
|
attr = getattr(resources, name)
|
||||||
"Renamed: please use `authenticate_with_account` or"
|
return attr.Resource(
|
||||||
" `authenticate_with_token` instead."
|
account=self.account, basepath=self.url, client=self.httpclient
|
||||||
),
|
)
|
||||||
)
|
except AttributeError:
|
||||||
def authenticate(self, token: str) -> None:
|
raise SpeckleException(
|
||||||
"""Authenticate the client using a personal access token
|
f"Method {name} is not supported by the SpeckleClient class"
|
||||||
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)
|
|
||||||
|
|||||||
@@ -1,14 +1,44 @@
|
|||||||
|
import os
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
# following imports seem to be unnecessary, but they need to stay
|
from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
|
||||||
# to not break the scripts using these functions as non-core
|
|
||||||
from specklepy.core.api.credentials import StreamWrapper # noqa: F401
|
from specklepy.api.models import ServerInfo
|
||||||
from specklepy.core.api.credentials import Account, UserInfo # noqa: F401
|
from specklepy.core.helpers import speckle_path_provider
|
||||||
from specklepy.core.api.credentials import (
|
|
||||||
get_account_from_token as core_get_account_from_token,
|
|
||||||
)
|
|
||||||
from specklepy.core.api.credentials import get_local_accounts as core_get_local_accounts
|
|
||||||
from specklepy.logging import metrics
|
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]:
|
def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
|
||||||
@@ -21,15 +51,48 @@ def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
|
|||||||
List[Account] -- list of all local accounts or an empty list if
|
List[Account] -- list of all local accounts or an empty list if
|
||||||
no accounts were found
|
no accounts were found
|
||||||
"""
|
"""
|
||||||
accounts = core_get_local_accounts(base_path)
|
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
|
||||||
|
|
||||||
metrics.track(
|
metrics.track(
|
||||||
metrics.SDK,
|
metrics.ACCOUNTS,
|
||||||
next(
|
next(
|
||||||
(acc for acc in accounts if acc.isDefault),
|
(acc for acc in accounts if acc.isDefault),
|
||||||
accounts[0] if accounts else None,
|
accounts[0] if accounts else None,
|
||||||
),
|
),
|
||||||
{"name": "Get Local Accounts"},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return accounts
|
return accounts
|
||||||
@@ -45,7 +108,7 @@ def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
|
|||||||
Returns:
|
Returns:
|
||||||
Account -- the default account or None if no local accounts were found
|
Account -- the default account or None if no local accounts were found
|
||||||
"""
|
"""
|
||||||
accounts = core_get_local_accounts(base_path=base_path)
|
accounts = get_local_accounts(base_path=base_path)
|
||||||
if not accounts:
|
if not accounts:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -67,7 +130,31 @@ def get_account_from_token(token: str, server_url: str = None) -> Account:
|
|||||||
Account -- the local account with this token or a shell account containing
|
Account -- the local account with this token or a shell account containing
|
||||||
just the token and url if no local account is found
|
just the token and url if no local account is found
|
||||||
"""
|
"""
|
||||||
account = core_get_account_from_token(token, server_url)
|
accounts = get_local_accounts()
|
||||||
|
if not accounts:
|
||||||
|
return Account.from_token(token, server_url)
|
||||||
|
|
||||||
metrics.track(metrics.SDK, account, {"name": "Get Account From Token"})
|
acct = next((acc for acc in accounts if acc.token == token), None)
|
||||||
return account
|
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(),
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,74 +1,116 @@
|
|||||||
from specklepy.core.api.host_applications import (
|
from dataclasses import dataclass
|
||||||
ARCGIS,
|
from enum import Enum
|
||||||
ARCHICAD,
|
from unicodedata import name
|
||||||
AUTOCAD,
|
|
||||||
BLENDER,
|
|
||||||
CIVIL,
|
|
||||||
CSIBRIDGE,
|
|
||||||
DXF,
|
|
||||||
DYNAMO,
|
|
||||||
ETABS,
|
|
||||||
EXCEL,
|
|
||||||
GRASSHOPPER,
|
|
||||||
GSA,
|
|
||||||
MICROSTATION,
|
|
||||||
NET,
|
|
||||||
OPENBUILDINGS,
|
|
||||||
OPENRAIL,
|
|
||||||
OPENROADS,
|
|
||||||
OTHER,
|
|
||||||
POWERBI,
|
|
||||||
PYTHON,
|
|
||||||
QGIS,
|
|
||||||
REVIT,
|
|
||||||
RHINO,
|
|
||||||
SAFE,
|
|
||||||
SAP2000,
|
|
||||||
SKETCHUP,
|
|
||||||
TEKLASTRUCTURES,
|
|
||||||
TOPSOLID,
|
|
||||||
UNITY,
|
|
||||||
UNREAL,
|
|
||||||
HostApplication,
|
|
||||||
HostAppVersion,
|
|
||||||
_app_name_host_app_mapping,
|
|
||||||
get_host_app_from_string,
|
|
||||||
)
|
|
||||||
|
|
||||||
# re-exporting stuff from the moved api module
|
|
||||||
__all__ = [
|
class HostAppVersion(Enum):
|
||||||
"ARCGIS",
|
v = "v"
|
||||||
"ARCHICAD",
|
v6 = "v6"
|
||||||
"AUTOCAD",
|
v7 = "v7"
|
||||||
"BLENDER",
|
v2019 = "v2019"
|
||||||
"CIVIL",
|
v2020 = "v2020"
|
||||||
"CSIBRIDGE",
|
v2021 = "v2021"
|
||||||
"DXF",
|
v2022 = "v2022"
|
||||||
"DYNAMO",
|
v2023 = "v2023"
|
||||||
"ETABS",
|
v2024 = "v2024"
|
||||||
"EXCEL",
|
v2025 = "v2025"
|
||||||
"GRASSHOPPER",
|
vSandbox = "vSandbox"
|
||||||
"GSA",
|
vRevit = "vRevit"
|
||||||
"MICROSTATION",
|
vRevit2021 = "vRevit2021"
|
||||||
"NET",
|
vRevit2022 = "vRevit2022"
|
||||||
"OPENBUILDINGS",
|
vRevit2023 = "vRevit2023"
|
||||||
"OPENRAIL",
|
vRevit2024 = "vRevit2024"
|
||||||
"OPENROADS",
|
vRevit2025 = "vRevit2025"
|
||||||
"OTHER",
|
v25 = "v25"
|
||||||
"POWERBI",
|
v26 = "v26"
|
||||||
"PYTHON",
|
|
||||||
"QGIS",
|
def __repr__(self) -> str:
|
||||||
"REVIT",
|
return self.value
|
||||||
"RHINO",
|
|
||||||
"SAFE",
|
def __str__(self) -> str:
|
||||||
"SAP2000",
|
return self.value
|
||||||
"SKETCHUP",
|
|
||||||
"TEKLASTRUCTURES",
|
|
||||||
"TOPSOLID",
|
@dataclass
|
||||||
"UNITY",
|
class HostApplication:
|
||||||
"UNREAL",
|
name: str
|
||||||
"HostApplication",
|
slug: str
|
||||||
"HostAppVersion",
|
|
||||||
"_app_name_host_app_mapping",
|
def get_version(self, version: HostAppVersion) -> str:
|
||||||
"get_host_app_from_string",
|
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)
|
||||||
|
|||||||
+197
-34
@@ -1,35 +1,198 @@
|
|||||||
# following imports seem to be unnecessary, but they need to stay
|
from datetime import datetime
|
||||||
# to not break the scripts using these functions as non-core
|
from typing import List, Optional
|
||||||
from specklepy.core.api.models import (
|
|
||||||
Activity,
|
|
||||||
ActivityCollection,
|
|
||||||
Branch,
|
|
||||||
Branches,
|
|
||||||
Collaborator,
|
|
||||||
Commit,
|
|
||||||
Commits,
|
|
||||||
LimitedUser,
|
|
||||||
Object,
|
|
||||||
PendingStreamCollaborator,
|
|
||||||
ServerInfo,
|
|
||||||
Stream,
|
|
||||||
Streams,
|
|
||||||
User,
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
from pydantic import BaseModel, Field
|
||||||
"Activity",
|
|
||||||
"ActivityCollection",
|
|
||||||
"Branch",
|
class Collaborator(BaseModel):
|
||||||
"Branches",
|
id: Optional[str]
|
||||||
"Collaborator",
|
name: Optional[str]
|
||||||
"Commit",
|
role: Optional[str]
|
||||||
"Commits",
|
avatar: Optional[str]
|
||||||
"LimitedUser",
|
|
||||||
"Object",
|
|
||||||
"PendingStreamCollaborator",
|
class Commit(BaseModel):
|
||||||
"ServerInfo",
|
id: Optional[str]
|
||||||
"Stream",
|
message: Optional[str]
|
||||||
"Streams",
|
authorName: Optional[str]
|
||||||
"User",
|
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
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
from specklepy.core.api.operations import deserialize as core_deserialize
|
|
||||||
from specklepy.core.api.operations import receive as _untracked_receive
|
|
||||||
from specklepy.core.api.operations import send as core_send
|
|
||||||
from specklepy.core.api.operations import serialize as core_serialize
|
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
|
from specklepy.logging.exceptions import SpeckleException
|
||||||
from specklepy.objects.base import Base
|
from specklepy.objects.base import Base
|
||||||
|
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
|
||||||
from specklepy.transports.abstract_transport import AbstractTransport
|
from specklepy.transports.abstract_transport import AbstractTransport
|
||||||
|
from specklepy.transports.sqlite import SQLiteTransport
|
||||||
|
|
||||||
|
|
||||||
def send(
|
def send(
|
||||||
@@ -25,18 +24,47 @@ def send(
|
|||||||
Returns:
|
Returns:
|
||||||
str -- the object id of the sent object
|
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:
|
if transports is None:
|
||||||
metrics.track(metrics.SEND)
|
metrics.track(metrics.SEND)
|
||||||
|
transports = []
|
||||||
else:
|
else:
|
||||||
metrics.track(metrics.SEND, getattr(transports[0], "account", None))
|
metrics.track(metrics.SEND, getattr(transports[0], "account", None))
|
||||||
|
|
||||||
return core_send(base, transports, use_default_cache)
|
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(
|
def receive(
|
||||||
obj_id: str,
|
obj_id: str,
|
||||||
remote_transport: Optional[AbstractTransport] = None,
|
remote_transport: Optional[AbstractTransport] = None,
|
||||||
local_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:
|
) -> Base:
|
||||||
"""Receives an object from a transport.
|
"""Receives an object from a transport.
|
||||||
|
|
||||||
@@ -49,8 +77,29 @@ def receive(
|
|||||||
Returns:
|
Returns:
|
||||||
Base -- the base object
|
Base -- the base object
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.RECEIVE, getattr(remote_transport, "account", None))
|
if not local_transport:
|
||||||
return _untracked_receive(obj_id, remote_transport, 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:
|
def serialize(base: Base, write_transports: List[AbstractTransport] = []) -> str:
|
||||||
@@ -67,8 +116,10 @@ def serialize(base: Base, write_transports: List[AbstractTransport] = []) -> str
|
|||||||
Returns:
|
Returns:
|
||||||
str -- the serialized object
|
str -- the serialized object
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, custom_props={"name": "Serialize"})
|
metrics.track(metrics.SERIALIZE)
|
||||||
return core_serialize(base, write_transports)
|
serializer = BaseObjectSerializer(write_transports=write_transports)
|
||||||
|
|
||||||
|
return serializer.write_json(base)[1]
|
||||||
|
|
||||||
|
|
||||||
def deserialize(
|
def deserialize(
|
||||||
@@ -90,8 +141,13 @@ def deserialize(
|
|||||||
Returns:
|
Returns:
|
||||||
Base -- the deserialized object
|
Base -- the deserialized object
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, custom_props={"name": "Deserialize"})
|
metrics.track(metrics.DESERIALIZE)
|
||||||
return core_deserialize(obj_string, read_transport)
|
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"]
|
__all__ = ["receive", "send", "serialize", "deserialize"]
|
||||||
|
|||||||
@@ -1,12 +1,20 @@
|
|||||||
from typing import Any, Optional, Tuple
|
from typing import Any, Dict, List, Optional, Tuple, Type, Union
|
||||||
|
|
||||||
from gql.client import Client
|
from gql.client import Client
|
||||||
|
from gql.transport.exceptions import TransportQueryError
|
||||||
|
from graphql import DocumentNode
|
||||||
|
|
||||||
from specklepy.api.credentials import Account
|
from specklepy.api.credentials import Account
|
||||||
from specklepy.core.api.resource import ResourceBase as CoreResourceBase
|
from specklepy.logging.exceptions import (
|
||||||
|
GraphQLException,
|
||||||
|
SpeckleException,
|
||||||
|
UnsupportedException,
|
||||||
|
)
|
||||||
|
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
|
||||||
|
from specklepy.transports.sqlite import SQLiteTransport
|
||||||
|
|
||||||
|
|
||||||
class ResourceBase(CoreResourceBase):
|
class ResourceBase(object):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
account: Account,
|
account: Account,
|
||||||
@@ -15,10 +23,106 @@ class ResourceBase(CoreResourceBase):
|
|||||||
name: str,
|
name: str,
|
||||||
server_version: Optional[Tuple[Any, ...]] = None,
|
server_version: Optional[Tuple[Any, ...]] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(
|
self.account = account
|
||||||
account=account,
|
self.basepath = basepath
|
||||||
basepath=basepath,
|
self.client = client
|
||||||
client=client,
|
self.name = name
|
||||||
name=name,
|
self.server_version = server_version
|
||||||
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.",
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,33 +1,62 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
from specklepy.api.models import PendingStreamCollaborator, User
|
from gql import gql
|
||||||
from specklepy.core.api.resources.active_user import Resource as CoreResource
|
|
||||||
|
from specklepy.api.models import ActivityCollection, PendingStreamCollaborator, User
|
||||||
|
from specklepy.api.resource import ResourceBase
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
|
from specklepy.logging.exceptions import SpeckleException
|
||||||
|
|
||||||
|
NAME = "active_user"
|
||||||
|
|
||||||
|
|
||||||
class Resource(CoreResource):
|
class Resource(ResourceBase):
|
||||||
"""API Access class for users. This class provides methods to get and update
|
"""API Access class for users"""
|
||||||
the user profile, fetch user activity, and manage pending stream invitations."""
|
|
||||||
|
|
||||||
def __init__(self, account, basepath, client, server_version) -> None:
|
def __init__(self, account, basepath, client, server_version) -> None:
|
||||||
super().__init__(
|
super().__init__(
|
||||||
account=account,
|
account=account,
|
||||||
basepath=basepath,
|
basepath=basepath,
|
||||||
client=client,
|
client=client,
|
||||||
|
name=NAME,
|
||||||
server_version=server_version,
|
server_version=server_version,
|
||||||
)
|
)
|
||||||
self.schema = User
|
self.schema = User
|
||||||
|
|
||||||
def get(self) -> User:
|
def get(self) -> User:
|
||||||
"""Gets the profile of the current authenticated user's profile
|
"""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).
|
(as extracted from the authorization header).
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
id {str} -- the user id
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
User -- the retrieved user
|
User -- the retrieved user
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, custom_props={"name": "User Active Get"})
|
metrics.track(metrics.USER, self.account, {"name": "get"})
|
||||||
return super().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")
|
||||||
|
|
||||||
def update(
|
def update(
|
||||||
self,
|
self,
|
||||||
@@ -38,17 +67,37 @@ class Resource(CoreResource):
|
|||||||
):
|
):
|
||||||
"""Updates your user profile. All arguments are optional.
|
"""Updates your user profile. All arguments are optional.
|
||||||
|
|
||||||
Args:
|
Arguments:
|
||||||
name (Optional[str]): The user's name.
|
name {str} -- your name
|
||||||
company (Optional[str]): The company the user works for.
|
company {str} -- the company you may or may not work for
|
||||||
bio (Optional[str]): A brief user biography.
|
bio {str} -- tell us about yourself
|
||||||
avatar (Optional[str]): A URL to an avatar image for the user.
|
avatar {str} -- a nice photo of yourself
|
||||||
|
|
||||||
Returns @deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT):
|
Returns @deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT):
|
||||||
bool -- True if your profile was updated successfully
|
bool -- True if your profile was updated successfully
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "User Active Update"})
|
metrics.track(metrics.USER, self.account, {"name": "update"})
|
||||||
return super().update(name, company, bio, avatar)
|
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(
|
def activity(
|
||||||
self,
|
self,
|
||||||
@@ -59,47 +108,162 @@ class Resource(CoreResource):
|
|||||||
cursor: Optional[datetime] = None,
|
cursor: Optional[datetime] = None,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Fetches collection the current authenticated user's activity
|
Get the activity from a given stream in an Activity collection.
|
||||||
as filtered by given parameters
|
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
|
Note: all timestamps arguments should be `datetime` of any tz as they will be
|
||||||
converted to UTC ISO format strings
|
converted to UTC ISO format strings
|
||||||
|
|
||||||
Args:
|
user_id {str} -- the id of the user to get the activity from
|
||||||
limit (int): The maximum number of activity items to return.
|
action_type {str} -- filter results to a single action type
|
||||||
action_type (Optional[str]): Filter results to a single action type.
|
(eg: `commit_create` or `commit_receive`)
|
||||||
before (Optional[datetime]): Latest cutoff for activity to include.
|
limit {int} -- max number of Activity items to return
|
||||||
after (Optional[datetime]): Oldest cutoff for an activity to include.
|
before {datetime} -- latest cutoff for activity
|
||||||
cursor (Optional[datetime]): Timestamp cursor for pagination.
|
(ie: return all activity _before_ this time)
|
||||||
|
after {datetime} -- oldest cutoff for activity
|
||||||
Returns:
|
(ie: return all activity _after_ this time)
|
||||||
Activity collection, filtered according to the provided parameters.
|
cursor {datetime} -- timestamp cursor for pagination
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "User Active Activity"})
|
|
||||||
return super().activity(limit, action_type, before, after, cursor)
|
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]:
|
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
|
||||||
"""Fetches all of the current user's pending stream invitations.
|
"""Get all of the active user's pending stream invites
|
||||||
|
|
||||||
|
Requires Speckle Server version >= 2.6.4
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List[PendingStreamCollaborator]: A list of pending stream invitations.
|
List[PendingStreamCollaborator]
|
||||||
|
-- a list of pending invites for the current user
|
||||||
"""
|
"""
|
||||||
metrics.track(
|
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||||
metrics.SDK, self.account, {"name": "User Active Invites All 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,
|
||||||
)
|
)
|
||||||
return super().get_all_pending_invites()
|
|
||||||
|
|
||||||
def get_pending_invite(
|
def get_pending_invite(
|
||||||
self, stream_id: str, token: Optional[str] = None
|
self, stream_id: str, token: Optional[str] = None
|
||||||
) -> Optional[PendingStreamCollaborator]:
|
) -> Optional[PendingStreamCollaborator]:
|
||||||
"""Fetches a specific pending invite for the current user on a given stream.
|
"""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.
|
||||||
|
|
||||||
Args:
|
Requires Speckle Server version >= 2.6.4
|
||||||
stream_id (str): The ID of the stream to look for invites on.
|
|
||||||
token (Optional[str]): The token of the invite to look for (optional).
|
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:
|
Returns:
|
||||||
Optional[PendingStreamCollaborator]: The invite for the given stream, or None if not found.
|
PendingStreamCollaborator
|
||||||
|
-- the invite for the given stream (or None if it isn't found)
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "User Active Invite Get"})
|
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||||
return super().get_pending_invite(stream_id, token)
|
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,
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
from typing import Optional, Union
|
from typing import Optional
|
||||||
|
|
||||||
|
from gql import gql
|
||||||
|
|
||||||
from specklepy.api.models import Branch
|
from specklepy.api.models import Branch
|
||||||
from specklepy.core.api.resources.branch import Resource as CoreResource
|
from specklepy.api.resource import ResourceBase
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
|
||||||
|
NAME = "branch"
|
||||||
|
|
||||||
|
|
||||||
class Resource(CoreResource):
|
class Resource(ResourceBase):
|
||||||
"""API Access class for branches"""
|
"""API Access class for branches"""
|
||||||
|
|
||||||
def __init__(self, account, basepath, client) -> None:
|
def __init__(self, account, basepath, client) -> None:
|
||||||
@@ -14,6 +17,7 @@ class Resource(CoreResource):
|
|||||||
account=account,
|
account=account,
|
||||||
basepath=basepath,
|
basepath=basepath,
|
||||||
client=client,
|
client=client,
|
||||||
|
name=NAME,
|
||||||
)
|
)
|
||||||
self.schema = Branch
|
self.schema = Branch
|
||||||
|
|
||||||
@@ -29,12 +33,27 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
id {str} -- the newly created branch's id
|
id {str} -- the newly created branch's id
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Branch Create"})
|
metrics.track(metrics.BRANCH, self.account, {"name": "create"})
|
||||||
return super().create(stream_id, name, description)
|
query = gql(
|
||||||
|
"""
|
||||||
|
mutation BranchCreate($branch: BranchCreateInput!) {
|
||||||
|
branchCreate(branch: $branch)
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
params = {
|
||||||
|
"branch": {
|
||||||
|
"streamId": stream_id,
|
||||||
|
"name": name,
|
||||||
|
"description": description,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def get(
|
return self.make_request(
|
||||||
self, stream_id: str, name: str, commits_limit: int = 10
|
query=query, params=params, return_type="branchCreate", parse_response=False
|
||||||
) -> Union[Branch, None, SpeckleException]:
|
)
|
||||||
|
|
||||||
|
def get(self, stream_id: str, name: str, commits_limit: int = 10):
|
||||||
"""Get a branch by name from a stream
|
"""Get a branch by name from a stream
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@@ -45,8 +64,42 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
Branch -- the fetched branch with its latest commits
|
Branch -- the fetched branch with its latest commits
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Branch Get"})
|
metrics.track(metrics.BRANCH, self.account, {"name": "get"})
|
||||||
return super().get(stream_id, name, commits_limit)
|
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):
|
def list(self, stream_id: str, branches_limit: int = 10, commits_limit: int = 10):
|
||||||
"""Get a list of branches from a given stream
|
"""Get a list of branches from a given stream
|
||||||
@@ -59,8 +112,50 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
List[Branch] -- the branches on the stream
|
List[Branch] -- the branches on the stream
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Branch List"})
|
metrics.track(metrics.BRANCH, self.account, {"name": "get"})
|
||||||
return super().list(stream_id, branches_limit, commits_limit)
|
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(
|
def update(
|
||||||
self,
|
self,
|
||||||
@@ -80,8 +175,29 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- True if update is successful
|
bool -- True if update is successful
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Branch Update"})
|
metrics.track(metrics.BRANCH, self.account, {"name": "update"})
|
||||||
return super().update(stream_id, branch_id, name, description)
|
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):
|
def delete(self, stream_id: str, branch_id: str):
|
||||||
"""Delete a branch
|
"""Delete a branch
|
||||||
@@ -93,5 +209,17 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- True if deletion is successful
|
bool -- True if deletion is successful
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Branch Delete"})
|
metrics.track(metrics.BRANCH, self.account, {"name": "delete"})
|
||||||
return super().delete(stream_id, branch_id)
|
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
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
from typing import List, Optional, Union
|
from typing import List, Optional
|
||||||
|
|
||||||
|
from gql import gql
|
||||||
|
|
||||||
from specklepy.api.models import Commit
|
from specklepy.api.models import Commit
|
||||||
from specklepy.core.api.resources.commit import Resource as CoreResource
|
from specklepy.api.resource import ResourceBase
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
|
||||||
|
NAME = "commit"
|
||||||
|
|
||||||
|
|
||||||
class Resource(CoreResource):
|
class Resource(ResourceBase):
|
||||||
"""API Access class for commits"""
|
"""API Access class for commits"""
|
||||||
|
|
||||||
def __init__(self, account, basepath, client) -> None:
|
def __init__(self, account, basepath, client) -> None:
|
||||||
@@ -14,6 +17,7 @@ class Resource(CoreResource):
|
|||||||
account=account,
|
account=account,
|
||||||
basepath=basepath,
|
basepath=basepath,
|
||||||
client=client,
|
client=client,
|
||||||
|
name=NAME,
|
||||||
)
|
)
|
||||||
self.schema = Commit
|
self.schema = Commit
|
||||||
|
|
||||||
@@ -28,8 +32,32 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
Commit -- the retrieved commit object
|
Commit -- the retrieved commit object
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Get"})
|
query = gql(
|
||||||
return super().get(stream_id, commit_id)
|
"""
|
||||||
|
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]:
|
def list(self, stream_id: str, limit: int = 10) -> List[Commit]:
|
||||||
"""
|
"""
|
||||||
@@ -42,8 +70,36 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
List[Commit] -- a list of the most recent commit objects
|
List[Commit] -- a list of the most recent commit objects
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Commit List"})
|
metrics.track(metrics.COMMIT, self.account, {"name": "get"})
|
||||||
return super().list(stream_id, limit)
|
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(
|
def create(
|
||||||
self,
|
self,
|
||||||
@@ -52,8 +108,8 @@ class Resource(CoreResource):
|
|||||||
branch_name: str = "main",
|
branch_name: str = "main",
|
||||||
message: str = "",
|
message: str = "",
|
||||||
source_application: str = "python",
|
source_application: str = "python",
|
||||||
parents: Optional[List[str]] = None,
|
parents: List[str] = None,
|
||||||
) -> Union[str, SpeckleException]:
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Creates a commit on a branch
|
Creates a commit on a branch
|
||||||
|
|
||||||
@@ -72,9 +128,27 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
str -- the id of the created commit
|
str -- the id of the created commit
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Create"})
|
metrics.track(metrics.COMMIT, self.account, {"name": "create"})
|
||||||
return super().create(
|
query = gql(
|
||||||
stream_id, object_id, branch_name, message, source_application, parents
|
"""
|
||||||
|
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:
|
def update(self, stream_id: str, commit_id: str, message: str) -> bool:
|
||||||
@@ -90,8 +164,20 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- True if the operation succeeded
|
bool -- True if the operation succeeded
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Update"})
|
metrics.track(metrics.COMMIT, self.account, {"name": "update"})
|
||||||
return super().update(stream_id, commit_id, message)
|
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:
|
def delete(self, stream_id: str, commit_id: str) -> bool:
|
||||||
"""
|
"""
|
||||||
@@ -105,8 +191,18 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- True if the operation succeeded
|
bool -- True if the operation succeeded
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Delete"})
|
metrics.track(metrics.COMMIT, self.account, {"name": "delete"})
|
||||||
return super().delete(stream_id, commit_id)
|
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(
|
def received(
|
||||||
self,
|
self,
|
||||||
@@ -118,5 +214,30 @@ class Resource(CoreResource):
|
|||||||
"""
|
"""
|
||||||
Mark a commit object a received by the source application.
|
Mark a commit object a received by the source application.
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Commit Received"})
|
metrics.track(metrics.COMMIT, self.account, {"name": "received"})
|
||||||
return super().received(stream_id, commit_id, source_application, message)
|
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
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
|
||||||
from specklepy.core.api.resources.object import Resource as CoreResource
|
from gql import gql
|
||||||
from specklepy.logging import metrics
|
|
||||||
|
from specklepy.api.resource import ResourceBase
|
||||||
from specklepy.objects.base import Base
|
from specklepy.objects.base import Base
|
||||||
|
|
||||||
|
NAME = "object"
|
||||||
|
|
||||||
class Resource(CoreResource):
|
|
||||||
|
class Resource(ResourceBase):
|
||||||
"""API Access class for objects"""
|
"""API Access class for objects"""
|
||||||
|
|
||||||
def __init__(self, account, basepath, client) -> None:
|
def __init__(self, account, basepath, client) -> None:
|
||||||
@@ -13,6 +16,7 @@ class Resource(CoreResource):
|
|||||||
account=account,
|
account=account,
|
||||||
basepath=basepath,
|
basepath=basepath,
|
||||||
client=client,
|
client=client,
|
||||||
|
name=NAME,
|
||||||
)
|
)
|
||||||
self.schema = Base
|
self.schema = Base
|
||||||
|
|
||||||
@@ -27,8 +31,31 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
Base -- the returned Base object
|
Base -- the returned Base object
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Object Get"})
|
query = gql(
|
||||||
return super().get(stream_id, object_id)
|
"""
|
||||||
|
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:
|
def create(self, stream_id: str, objects: List[Dict]) -> str:
|
||||||
"""
|
"""
|
||||||
@@ -51,5 +78,15 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
str -- the id of the object
|
str -- the id of the object
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Object Create"})
|
query = gql(
|
||||||
return super().create(stream_id, objects)
|
"""
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,63 +1,99 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
from typing import List, Optional, Union
|
from typing import List, Optional, Union
|
||||||
|
|
||||||
|
from gql import gql
|
||||||
|
|
||||||
from specklepy.api.models import ActivityCollection, LimitedUser
|
from specklepy.api.models import ActivityCollection, LimitedUser
|
||||||
from specklepy.core.api.resources.other_user import Resource as CoreResource
|
from specklepy.api.resource import ResourceBase
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
from specklepy.logging.exceptions import SpeckleException
|
||||||
|
|
||||||
|
NAME = "other_user"
|
||||||
|
|
||||||
class Resource(CoreResource):
|
|
||||||
"""
|
class Resource(ResourceBase):
|
||||||
Provides API access to other users' profiles and activities on the platform.
|
"""API Access class for other users, that are not the currently active user."""
|
||||||
This class enables fetching limited information about users, searching for users by name or email,
|
|
||||||
and accessing user activity logs with appropriate privacy and access control measures in place.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, account, basepath, client, server_version) -> None:
|
def __init__(self, account, basepath, client, server_version) -> None:
|
||||||
super().__init__(
|
super().__init__(
|
||||||
account=account,
|
account=account,
|
||||||
basepath=basepath,
|
basepath=basepath,
|
||||||
client=client,
|
client=client,
|
||||||
|
name=NAME,
|
||||||
server_version=server_version,
|
server_version=server_version,
|
||||||
)
|
)
|
||||||
self.schema = LimitedUser
|
self.schema = LimitedUser
|
||||||
|
|
||||||
def get(self, id: str) -> LimitedUser:
|
def get(self, id: str) -> LimitedUser:
|
||||||
"""
|
"""
|
||||||
Retrieves the profile of a user specified by their user ID.
|
Gets the profile of another user.
|
||||||
|
|
||||||
Args:
|
Arguments:
|
||||||
id (str): The unique identifier of the user.
|
id {str} -- the user id
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
LimitedUser: The profile of the user with limited information.
|
LimitedUser -- the retrieved profile of another user
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Other User Get"})
|
metrics.track(metrics.OTHER_USER, self.account, {"name": "get"})
|
||||||
return super().get(id)
|
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(
|
def search(
|
||||||
self, search_query: str, limit: int = 25
|
self, search_query: str, limit: int = 25
|
||||||
) -> Union[List[LimitedUser], SpeckleException]:
|
) -> Union[List[LimitedUser], SpeckleException]:
|
||||||
"""
|
"""Searches for user by name or email. The search query must be at least
|
||||||
Searches for users by name or email.
|
3 characters long
|
||||||
The search requires a minimum query length of 3 characters.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
search_query (str): The search string.
|
|
||||||
limit (int): Maximum number of search results to return.
|
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
search_query {str} -- a string to search for
|
||||||
|
limit {int} -- the maximum number of results to return
|
||||||
Returns:
|
Returns:
|
||||||
Union[List[LimitedUser], SpeckleException]: A list of users matching the search
|
List[LimitedUser] -- a list of User objects that match the search query
|
||||||
query or an exception if the query is too short.
|
|
||||||
"""
|
"""
|
||||||
if len(search_query) < 3:
|
if len(search_query) < 3:
|
||||||
return SpeckleException(
|
return SpeckleException(
|
||||||
message="User search query must be at least 3 characters."
|
message="User search query must be at least 3 characters"
|
||||||
)
|
)
|
||||||
|
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Other User Search"})
|
metrics.track(metrics.OTHER_USER, self.account, {"name": "search"})
|
||||||
return super().search(search_query, limit)
|
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(
|
def activity(
|
||||||
self,
|
self,
|
||||||
@@ -69,19 +105,71 @@ class Resource(CoreResource):
|
|||||||
cursor: Optional[datetime] = None,
|
cursor: Optional[datetime] = None,
|
||||||
) -> ActivityCollection:
|
) -> ActivityCollection:
|
||||||
"""
|
"""
|
||||||
Retrieves a collection of activities for a specified user, with optional filters for activity type,
|
Get the activity from a given stream in an Activity collection.
|
||||||
time frame, and pagination.
|
Step into the activity `items` for the list of activity.
|
||||||
|
|
||||||
Args:
|
Note: all timestamps arguments should be `datetime` of
|
||||||
user_id (str): The ID of the user whose activities are being requested.
|
any tz as they will be converted to UTC ISO format strings
|
||||||
limit (int): The maximum number of activity items to return.
|
|
||||||
action_type (Optional[str]): A specific type of activity to filter.
|
|
||||||
before (Optional[datetime]): Latest timestamp to include activities before.
|
|
||||||
after (Optional[datetime]): Earliest timestamp to include activities after.
|
|
||||||
cursor (Optional[datetime]): Timestamp for pagination cursor.
|
|
||||||
|
|
||||||
Returns:
|
user_id {str} -- the id of the user to get the activity from
|
||||||
ActivityCollection: A collection of user activities filtered according to specified criteria.
|
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
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Other User Activity"})
|
|
||||||
return super().activity(user_id, limit, action_type, before, after, cursor)
|
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,
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
|
import re
|
||||||
from typing import Any, Dict, List, Tuple
|
from typing import Any, Dict, List, Tuple
|
||||||
|
|
||||||
|
from gql import gql
|
||||||
|
|
||||||
from specklepy.api.models import ServerInfo
|
from specklepy.api.models import ServerInfo
|
||||||
from specklepy.core.api.resources.server import Resource as CoreResource
|
from specklepy.api.resource import ResourceBase
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
|
from specklepy.logging.exceptions import GraphQLException
|
||||||
|
|
||||||
|
NAME = "server"
|
||||||
|
|
||||||
|
|
||||||
class Resource(CoreResource):
|
class Resource(ResourceBase):
|
||||||
"""API Access class for the server"""
|
"""API Access class for the server"""
|
||||||
|
|
||||||
def __init__(self, account, basepath, client) -> None:
|
def __init__(self, account, basepath, client) -> None:
|
||||||
@@ -13,6 +19,7 @@ class Resource(CoreResource):
|
|||||||
account=account,
|
account=account,
|
||||||
basepath=basepath,
|
basepath=basepath,
|
||||||
client=client,
|
client=client,
|
||||||
|
name=NAME,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get(self) -> ServerInfo:
|
def get(self) -> ServerInfo:
|
||||||
@@ -21,8 +28,39 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
dict -- the server info in dictionary form
|
dict -- the server info in dictionary form
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Server Get"})
|
metrics.track(metrics.SERVER, self.account, {"name": "get"})
|
||||||
return super().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
|
||||||
|
)
|
||||||
|
|
||||||
def version(self) -> Tuple[Any, ...]:
|
def version(self) -> Tuple[Any, ...]:
|
||||||
"""Get the server version
|
"""Get the server version
|
||||||
@@ -32,7 +70,30 @@ class Resource(CoreResource):
|
|||||||
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
|
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
|
# not tracking as it will be called along with other mutations / queries as a check
|
||||||
return super().version()
|
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:
|
def apps(self) -> Dict:
|
||||||
"""Get the apps registered on the server
|
"""Get the apps registered on the server
|
||||||
@@ -40,8 +101,28 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
dict -- a dictionary of apps registered on the server
|
dict -- a dictionary of apps registered on the server
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Server Apps"})
|
metrics.track(metrics.SERVER, self.account, {"name": "apps"})
|
||||||
return super().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)
|
||||||
|
|
||||||
def create_token(self, name: str, scopes: List[str], lifespan: int) -> str:
|
def create_token(self, name: str, scopes: List[str], lifespan: int) -> str:
|
||||||
"""Create a personal API token
|
"""Create a personal API token
|
||||||
@@ -54,8 +135,22 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
str -- the new API token. note: this is the only time you'll see the token!
|
str -- the new API token. note: this is the only time you'll see the token!
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Server Create Token"})
|
metrics.track(metrics.SERVER, self.account, {"name": "create_token"})
|
||||||
return super().create_token(name, scopes, lifespan)
|
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:
|
def revoke_token(self, token: str) -> bool:
|
||||||
"""Revokes (deletes) a personal API token
|
"""Revokes (deletes) a personal API token
|
||||||
@@ -66,5 +161,19 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- True if the token was successfully deleted
|
bool -- True if the token was successfully deleted
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Server Revoke Token"})
|
metrics.track(metrics.SERVER, self.account, {"name": "revoke_token"})
|
||||||
return super().revoke_token(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,
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,12 +1,18 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
from specklepy.api.models import PendingStreamCollaborator, Stream
|
from deprecated import deprecated
|
||||||
from specklepy.core.api.resources.stream import Resource as CoreResource
|
from gql import gql
|
||||||
|
|
||||||
|
from specklepy.api.models import ActivityCollection, PendingStreamCollaborator, Stream
|
||||||
|
from specklepy.api.resource import ResourceBase
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
|
from specklepy.logging.exceptions import SpeckleException, UnsupportedException
|
||||||
|
|
||||||
|
NAME = "stream"
|
||||||
|
|
||||||
|
|
||||||
class Resource(CoreResource):
|
class Resource(ResourceBase):
|
||||||
"""API Access class for streams"""
|
"""API Access class for streams"""
|
||||||
|
|
||||||
def __init__(self, account, basepath, client, server_version) -> None:
|
def __init__(self, account, basepath, client, server_version) -> None:
|
||||||
@@ -14,6 +20,7 @@ class Resource(CoreResource):
|
|||||||
account=account,
|
account=account,
|
||||||
basepath=basepath,
|
basepath=basepath,
|
||||||
client=client,
|
client=client,
|
||||||
|
name=NAME,
|
||||||
server_version=server_version,
|
server_version=server_version,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -30,8 +37,56 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
Stream -- the retrieved stream
|
Stream -- the retrieved stream
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Get"})
|
metrics.track(metrics.STREAM, self.account, {"name": "get"})
|
||||||
return super().get(id, branch_limit, commit_limit)
|
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]:
|
def list(self, stream_limit: int = 10) -> List[Stream]:
|
||||||
"""Get a list of the user's streams
|
"""Get a list of the user's streams
|
||||||
@@ -42,8 +97,50 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
List[Stream] -- A list of Stream objects
|
List[Stream] -- A list of Stream objects
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream List"})
|
metrics.track(metrics.STREAM, self.account, {"name": "get"})
|
||||||
return super().list(stream_limit)
|
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(
|
def create(
|
||||||
self,
|
self,
|
||||||
@@ -62,8 +159,22 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
id {str} -- the id of the newly created stream
|
id {str} -- the id of the newly created stream
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Create"})
|
metrics.track(metrics.STREAM, self.account, {"name": "create"})
|
||||||
return super().create(name, description, is_public)
|
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(
|
def update(
|
||||||
self,
|
self,
|
||||||
@@ -84,8 +195,27 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- whether the stream update was successful
|
bool -- whether the stream update was successful
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Update"})
|
metrics.track(metrics.STREAM, self.account, {"name": "update"})
|
||||||
return super().update(id, name, description, is_public)
|
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:
|
def delete(self, id: str) -> bool:
|
||||||
"""Delete a stream given its id
|
"""Delete a stream given its id
|
||||||
@@ -96,8 +226,20 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- whether the deletion was successful
|
bool -- whether the deletion was successful
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Delete"})
|
metrics.track(metrics.STREAM, self.account, {"name": "delete"})
|
||||||
return super().delete(id)
|
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(
|
def search(
|
||||||
self,
|
self,
|
||||||
@@ -117,8 +259,67 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
List[Stream] -- a list of Streams that match the search query
|
List[Stream] -- a list of Streams that match the search query
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Search"})
|
metrics.track(metrics.STREAM, self.account, {"name": "search"})
|
||||||
return super().search(search_query, limit, branch_limit, commit_limit)
|
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):
|
def favorite(self, stream_id: str, favorited: bool = True):
|
||||||
"""Favorite or unfavorite the given stream.
|
"""Favorite or unfavorite the given stream.
|
||||||
@@ -131,8 +332,86 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
Stream -- the stream with its `id`, `name`, and `favoritedDate`
|
Stream -- the stream with its `id`, `name`, and `favoritedDate`
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Favorite"})
|
metrics.track(metrics.STREAM, self.account, {"name": "favorite"})
|
||||||
return super().favorite(stream_id, favorited)
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
def get_all_pending_invites(
|
def get_all_pending_invites(
|
||||||
self, stream_id: str
|
self, stream_id: str
|
||||||
@@ -149,8 +428,46 @@ class Resource(CoreResource):
|
|||||||
List[PendingStreamCollaborator]
|
List[PendingStreamCollaborator]
|
||||||
-- a list of pending invites for the specified stream
|
-- a list of pending invites for the specified stream
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Get"})
|
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||||
return super().get_all_pending_invites(stream_id)
|
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(
|
def invite(
|
||||||
self,
|
self,
|
||||||
@@ -176,8 +493,38 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- True if the operation was successful
|
bool -- True if the operation was successful
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Create"})
|
metrics.track(metrics.INVITE, self.account, {"name": "create"})
|
||||||
return super().invite(stream_id, email, user_id, role, message)
|
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(
|
def invite_batch(
|
||||||
self,
|
self,
|
||||||
@@ -202,8 +549,43 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- True if the operation was successful
|
bool -- True if the operation was successful
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Batch Create"})
|
metrics.track(metrics.INVITE, self.account, {"name": "batch create"})
|
||||||
return super().invite_batch(stream_id, emails, user_ids, message)
|
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:
|
def invite_cancel(self, stream_id: str, invite_id: str) -> bool:
|
||||||
"""Cancel an existing stream invite
|
"""Cancel an existing stream invite
|
||||||
@@ -217,8 +599,25 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- true if the operation was successful
|
bool -- true if the operation was successful
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Cancel"})
|
metrics.track(metrics.INVITE, self.account, {"name": "cancel"})
|
||||||
return super().invite_cancel(stream_id, invite_id)
|
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:
|
def invite_use(self, stream_id: str, token: str, accept: bool = True) -> bool:
|
||||||
"""Accept or decline a stream invite
|
"""Accept or decline a stream invite
|
||||||
@@ -234,8 +633,29 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- true if the operation was successful
|
bool -- true if the operation was successful
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Invite Use"})
|
metrics.track(metrics.INVITE, self.account, {"name": "use"})
|
||||||
return super().invite_use(stream_id, token, accept)
|
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):
|
def update_permission(self, stream_id: str, user_id: str, role: str):
|
||||||
"""Updates permissions for a user on a given stream
|
"""Updates permissions for a user on a given stream
|
||||||
@@ -251,11 +671,40 @@ class Resource(CoreResource):
|
|||||||
bool -- True if the operation was successful
|
bool -- True if the operation was successful
|
||||||
"""
|
"""
|
||||||
metrics.track(
|
metrics.track(
|
||||||
metrics.SDK,
|
metrics.PERMISSION, self.account, {"name": "update", "role": role}
|
||||||
self.account,
|
)
|
||||||
{"name": "Stream Permission 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,
|
||||||
)
|
)
|
||||||
return super().update_permission(stream_id, user_id, role)
|
|
||||||
|
|
||||||
def revoke_permission(self, stream_id: str, user_id: str):
|
def revoke_permission(self, stream_id: str, user_id: str):
|
||||||
"""Revoke permissions from a user on a given stream
|
"""Revoke permissions from a user on a given stream
|
||||||
@@ -267,8 +716,25 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- True if the operation was successful
|
bool -- True if the operation was successful
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Permission Revoke"})
|
metrics.track(metrics.PERMISSION, self.account, {"name": "revoke"})
|
||||||
return super().revoke_permission(stream_id, user_id)
|
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(
|
def activity(
|
||||||
self,
|
self,
|
||||||
@@ -297,5 +763,64 @@ class Resource(CoreResource):
|
|||||||
-- oldest cutoff for activity (ie: return all activity _after_ this time)
|
-- oldest cutoff for activity (ie: return all activity _after_ this time)
|
||||||
cursor {datetime} -- timestamp cursor for pagination
|
cursor {datetime} -- timestamp cursor for pagination
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Stream Activity"})
|
query = gql(
|
||||||
return super().activity(stream_id, action_type, limit, before, after, cursor)
|
"""
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import Callable, Dict, List, Optional, Union
|
from typing import Callable, Dict, List, Optional, Union
|
||||||
|
|
||||||
|
from gql import gql
|
||||||
from graphql import DocumentNode
|
from graphql import DocumentNode
|
||||||
|
|
||||||
from specklepy.core.api.resources.subscriptions import Resource as CoreResource
|
from specklepy.api.resource import ResourceBase
|
||||||
from specklepy.logging import metrics
|
from specklepy.api.resources.stream import Stream
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
from specklepy.logging.exceptions import SpeckleException
|
||||||
|
|
||||||
|
NAME = "subscribe"
|
||||||
|
|
||||||
|
|
||||||
def check_wsclient(function):
|
def check_wsclient(function):
|
||||||
@wraps(function)
|
@wraps(function)
|
||||||
@@ -21,7 +24,7 @@ def check_wsclient(function):
|
|||||||
return check_wsclient_wrapper
|
return check_wsclient_wrapper
|
||||||
|
|
||||||
|
|
||||||
class Resource(CoreResource):
|
class Resource(ResourceBase):
|
||||||
"""API Access class for subscriptions"""
|
"""API Access class for subscriptions"""
|
||||||
|
|
||||||
def __init__(self, account, basepath, client) -> None:
|
def __init__(self, account, basepath, client) -> None:
|
||||||
@@ -29,6 +32,7 @@ class Resource(CoreResource):
|
|||||||
account=account,
|
account=account,
|
||||||
basepath=basepath,
|
basepath=basepath,
|
||||||
client=client,
|
client=client,
|
||||||
|
name=NAME,
|
||||||
)
|
)
|
||||||
|
|
||||||
@check_wsclient
|
@check_wsclient
|
||||||
@@ -43,8 +47,14 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
Stream -- the update stream
|
Stream -- the update stream
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "Subscription Stream Added"})
|
query = gql(
|
||||||
return super().stream_added(callback)
|
"""
|
||||||
|
subscription { userStreamAdded }
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
return await self.subscribe(
|
||||||
|
query=query, callback=callback, return_type="userStreamAdded", schema=Stream
|
||||||
|
)
|
||||||
|
|
||||||
@check_wsclient
|
@check_wsclient
|
||||||
async def stream_updated(self, id: str, callback: Optional[Callable] = None):
|
async def stream_updated(self, id: str, callback: Optional[Callable] = None):
|
||||||
@@ -61,10 +71,20 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
Stream -- the update stream
|
Stream -- the update stream
|
||||||
"""
|
"""
|
||||||
metrics.track(
|
query = gql(
|
||||||
metrics.SDK, self.account, {"name": "Subscription Stream Updated"}
|
"""
|
||||||
|
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,
|
||||||
)
|
)
|
||||||
return super().stream_updated(id, callback)
|
|
||||||
|
|
||||||
@check_wsclient
|
@check_wsclient
|
||||||
async def stream_removed(self, callback: Optional[Callable] = None):
|
async def stream_removed(self, callback: Optional[Callable] = None):
|
||||||
@@ -82,10 +102,18 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
dict -- dict containing 'id' of stream removed and optionally 'revokedBy'
|
dict -- dict containing 'id' of stream removed and optionally 'revokedBy'
|
||||||
"""
|
"""
|
||||||
metrics.track(
|
query = gql(
|
||||||
metrics.SDK, self.account, {"name": "Subscription Stream Removed"}
|
"""
|
||||||
|
subscription { userStreamRemoved }
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
return await self.subscribe(
|
||||||
|
query=query,
|
||||||
|
callback=callback,
|
||||||
|
return_type="userStreamRemoved",
|
||||||
|
parse_response=False,
|
||||||
)
|
)
|
||||||
return super().stream_removed(callback)
|
|
||||||
|
|
||||||
@check_wsclient
|
@check_wsclient
|
||||||
async def subscribe(
|
async def subscribe(
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
from typing import List, Optional, Union
|
from typing import List, Optional, Union
|
||||||
|
|
||||||
from deprecated import deprecated
|
from deprecated import deprecated
|
||||||
|
from gql import gql
|
||||||
|
|
||||||
from specklepy.api.models import PendingStreamCollaborator, User
|
from specklepy.api.models import ActivityCollection, PendingStreamCollaborator, User
|
||||||
from specklepy.core.api.resources.user import Resource as CoreResource
|
from specklepy.api.resource import ResourceBase
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
from specklepy.logging.exceptions import SpeckleException
|
||||||
|
|
||||||
|
NAME = "user"
|
||||||
|
|
||||||
DEPRECATION_VERSION = "2.9.0"
|
DEPRECATION_VERSION = "2.9.0"
|
||||||
DEPRECATION_TEXT = (
|
DEPRECATION_TEXT = (
|
||||||
"The user resource is deprecated, please use the active_user or other_user"
|
"The user resource is deprecated, please use the active_user or other_user"
|
||||||
@@ -15,7 +18,7 @@ DEPRECATION_TEXT = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Resource(CoreResource):
|
class Resource(ResourceBase):
|
||||||
"""API Access class for users"""
|
"""API Access class for users"""
|
||||||
|
|
||||||
def __init__(self, account, basepath, client, server_version) -> None:
|
def __init__(self, account, basepath, client, server_version) -> None:
|
||||||
@@ -23,6 +26,7 @@ class Resource(CoreResource):
|
|||||||
account=account,
|
account=account,
|
||||||
basepath=basepath,
|
basepath=basepath,
|
||||||
client=client,
|
client=client,
|
||||||
|
name=NAME,
|
||||||
server_version=server_version,
|
server_version=server_version,
|
||||||
)
|
)
|
||||||
self.schema = User
|
self.schema = User
|
||||||
@@ -40,8 +44,28 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
User -- the retrieved user
|
User -- the retrieved user
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "User Get_deprecated"})
|
metrics.track(metrics.USER, self.account, {"name": "get"})
|
||||||
return super().get(id)
|
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)
|
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||||
def search(
|
def search(
|
||||||
@@ -57,8 +81,33 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
List[User] -- a list of User objects that match the search query
|
List[User] -- a list of User objects that match the search query
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "User Search_deprecated"})
|
if len(search_query) < 3:
|
||||||
return super().search(search_query, limit)
|
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"]
|
||||||
|
)
|
||||||
|
|
||||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||||
def update(
|
def update(
|
||||||
@@ -79,9 +128,28 @@ class Resource(CoreResource):
|
|||||||
Returns:
|
Returns:
|
||||||
bool -- True if your profile was updated successfully
|
bool -- True if your profile was updated successfully
|
||||||
"""
|
"""
|
||||||
# metrics.track(metrics.USER, self.account, {"name": "update"})
|
metrics.track(metrics.USER, self.account, {"name": "update"})
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "User Update_deprecated"})
|
query = gql(
|
||||||
return super().update(name, company, bio, avatar)
|
"""
|
||||||
|
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)
|
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||||
def activity(
|
def activity(
|
||||||
@@ -112,8 +180,58 @@ class Resource(CoreResource):
|
|||||||
-- oldest cutoff for activity (ie: return all activity _after_ this time)
|
-- oldest cutoff for activity (ie: return all activity _after_ this time)
|
||||||
cursor {datetime} -- timestamp cursor for pagination
|
cursor {datetime} -- timestamp cursor for pagination
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "User Activity_deprecated"})
|
|
||||||
return super().activity(user_id, limit, action_type, before, after, cursor)
|
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)
|
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||||
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
|
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
|
||||||
@@ -125,11 +243,36 @@ class Resource(CoreResource):
|
|||||||
List[PendingStreamCollaborator]
|
List[PendingStreamCollaborator]
|
||||||
-- a list of pending invites for the current user
|
-- a list of pending invites for the current user
|
||||||
"""
|
"""
|
||||||
# metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||||
metrics.track(
|
self._check_invites_supported()
|
||||||
metrics.SDK, self.account, {"name": "User GetAllInvites_deprecated"}
|
|
||||||
|
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,
|
||||||
)
|
)
|
||||||
return super().get_all_pending_invites()
|
|
||||||
|
|
||||||
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
|
||||||
def get_pending_invite(
|
def get_pending_invite(
|
||||||
@@ -148,6 +291,37 @@ class Resource(CoreResource):
|
|||||||
PendingStreamCollaborator
|
PendingStreamCollaborator
|
||||||
-- the invite for the given stream (or None if it isn't found)
|
-- the invite for the given stream (or None if it isn't found)
|
||||||
"""
|
"""
|
||||||
# metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
metrics.track(metrics.INVITE, self.account, {"name": "get"})
|
||||||
metrics.track(metrics.SDK, self.account, {"name": "User GetInvite_deprecated"})
|
self._check_invites_supported()
|
||||||
return super().get_pending_invite(stream_id, token)
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|||||||
+113
-13
@@ -1,11 +1,18 @@
|
|||||||
|
from urllib.parse import unquote, urlparse
|
||||||
|
from warnings import warn
|
||||||
|
|
||||||
from specklepy.api.client import SpeckleClient
|
from specklepy.api.client import SpeckleClient
|
||||||
from specklepy.api.credentials import Account
|
from specklepy.api.credentials import (
|
||||||
from specklepy.core.api.wrapper import StreamWrapper as CoreStreamWrapper
|
Account,
|
||||||
|
get_account_from_token,
|
||||||
|
get_local_accounts,
|
||||||
|
)
|
||||||
from specklepy.logging import metrics
|
from specklepy.logging import metrics
|
||||||
|
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
||||||
from specklepy.transports.server.server import ServerTransport
|
from specklepy.transports.server.server import ServerTransport
|
||||||
|
|
||||||
|
|
||||||
class StreamWrapper(CoreStreamWrapper):
|
class StreamWrapper:
|
||||||
"""
|
"""
|
||||||
The `StreamWrapper` gives you some handy helpers to deal with urls and
|
The `StreamWrapper` gives you some handy helpers to deal with urls and
|
||||||
get authenticated clients and transports.
|
get authenticated clients and transports.
|
||||||
@@ -22,7 +29,7 @@ class StreamWrapper(CoreStreamWrapper):
|
|||||||
from specklepy.api.wrapper import StreamWrapper
|
from specklepy.api.wrapper import StreamWrapper
|
||||||
|
|
||||||
# provide any stream, branch, commit, object, or globals url
|
# provide any stream, branch, commit, object, or globals url
|
||||||
wrapper = StreamWrapper("https://app.speckle.systems/streams/3073b96e86/commits/604bea8cc6")
|
wrapper = StreamWrapper("https://speckle.xyz/streams/3073b96e86/commits/604bea8cc6")
|
||||||
|
|
||||||
# get an authenticated SpeckleClient if you have a local account for the server
|
# get an authenticated SpeckleClient if you have a local account for the server
|
||||||
client = wrapper.get_client()
|
client = wrapper.get_client()
|
||||||
@@ -42,16 +49,93 @@ class StreamWrapper(CoreStreamWrapper):
|
|||||||
_client: SpeckleClient = None
|
_client: SpeckleClient = None
|
||||||
_account: Account = 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:
|
def __init__(self, url: str) -> None:
|
||||||
super().__init__(url=url)
|
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}"
|
||||||
|
|
||||||
def get_account(self, token: str = None) -> Account:
|
def get_account(self, token: str = None) -> Account:
|
||||||
"""
|
"""
|
||||||
Gets an account object for this server from the local accounts db
|
Gets an account object for this server from the local accounts db
|
||||||
(added via Speckle Manager or a json file)
|
(added via Speckle Manager or a json file)
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, custom_props={"name": "Stream Wrapper Get Account"})
|
if self._account and self._account.token:
|
||||||
return super().get_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:
|
def get_client(self, token: str = None) -> SpeckleClient:
|
||||||
"""
|
"""
|
||||||
@@ -68,8 +152,25 @@ class StreamWrapper(CoreStreamWrapper):
|
|||||||
SpeckleClient
|
SpeckleClient
|
||||||
-- authenticated with a corresponding local account or the provided token
|
-- authenticated with a corresponding local account or the provided token
|
||||||
"""
|
"""
|
||||||
metrics.track(metrics.SDK, custom_props={"name": "Stream Wrapper Get Client"})
|
if self._client and token is None:
|
||||||
return super().get_client(token)
|
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:
|
def get_transport(self, token: str = None) -> ServerTransport:
|
||||||
"""
|
"""
|
||||||
@@ -82,7 +183,6 @@ class StreamWrapper(CoreStreamWrapper):
|
|||||||
ServerTransport -- constructed for this stream
|
ServerTransport -- constructed for this stream
|
||||||
with a pre-authenticated client
|
with a pre-authenticated client
|
||||||
"""
|
"""
|
||||||
metrics.track(
|
if not self._account or not self._account.token:
|
||||||
metrics.SDK, custom_props={"name": "Stream Wrapper Get Transport"}
|
self.get_account(token)
|
||||||
)
|
return ServerTransport(self.stream_id, account=self._account)
|
||||||
return super().get_transport(token)
|
|
||||||
|
|||||||
@@ -1,256 +0,0 @@
|
|||||||
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 (
|
|
||||||
active_user,
|
|
||||||
branch,
|
|
||||||
commit,
|
|
||||||
object,
|
|
||||||
other_user,
|
|
||||||
server,
|
|
||||||
stream,
|
|
||||||
subscriptions,
|
|
||||||
user,
|
|
||||||
)
|
|
||||||
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 `app.speckle.systems`.
|
|
||||||
|
|
||||||
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="app.speckle.systems") # 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 = "app.speckle.systems"
|
|
||||||
USE_SSL = True
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
host: str = DEFAULT_HOST,
|
|
||||||
use_ssl: bool = USE_SSL,
|
|
||||||
verify_certificate: bool = True,
|
|
||||||
connection_retries: int = 3,
|
|
||||||
connection_timeout: int = 10,
|
|
||||||
) -> 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.verify_certificate = verify_certificate
|
|
||||||
self.connection_retries = connection_retries
|
|
||||||
self.connection_timeout = connection_timeout
|
|
||||||
|
|
||||||
self.httpclient = Client(
|
|
||||||
transport=RequestsHTTPTransport(
|
|
||||||
url=self.graphql,
|
|
||||||
verify=self.verify_certificate,
|
|
||||||
retries=self.connection_retries,
|
|
||||||
timeout=self.connection_timeout,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
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_account(get_account_from_token(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
|
|
||||||
"""
|
|
||||||
self.account = 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=self.verify_certificate, 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"
|
|
||||||
)
|
|
||||||
@@ -1,176 +0,0 @@
|
|||||||
import os
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import List, Optional
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
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.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.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_raw(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)
|
|
||||||
|
|
||||||
|
|
||||||
def get_accounts_for_server(host: str) -> List[Account]:
|
|
||||||
all_accounts = get_local_accounts()
|
|
||||||
filtered: List[Account] = []
|
|
||||||
|
|
||||||
for acc in all_accounts:
|
|
||||||
moved_from = (
|
|
||||||
acc.serverInfo.migration.movedFrom if acc.serverInfo.migration else None
|
|
||||||
)
|
|
||||||
|
|
||||||
if moved_from and host == urlparse(moved_from).netloc:
|
|
||||||
filtered.append(acc)
|
|
||||||
|
|
||||||
for acc in all_accounts:
|
|
||||||
if any([x for x in filtered if x.userInfo.id == acc.userInfo.id]):
|
|
||||||
continue
|
|
||||||
|
|
||||||
if host == urlparse(acc.serverInfo.url).netloc:
|
|
||||||
filtered.append(acc)
|
|
||||||
|
|
||||||
return filtered
|
|
||||||
|
|
||||||
|
|
||||||
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(),
|
|
||||||
)
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
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)
|
|
||||||
@@ -1,205 +0,0 @@
|
|||||||
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 ServerMigration(BaseModel):
|
|
||||||
movedTo: Optional[str] = None
|
|
||||||
movedFrom: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
frontend2: Optional[bool] = None
|
|
||||||
migration: Optional[ServerMigration] = None
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
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"]
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
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.",
|
|
||||||
)
|
|
||||||
@@ -1,268 +0,0 @@
|
|||||||
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,
|
|
||||||
)
|
|
||||||
@@ -1,222 +0,0 @@
|
|||||||
from typing import Optional
|
|
||||||
|
|
||||||
from gql import gql
|
|
||||||
|
|
||||||
from specklepy.core.api.models import Branch
|
|
||||||
from specklepy.core.api.resource import ResourceBase
|
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
if len(name) < 3:
|
|
||||||
return SpeckleException(message="Branch Name must be at least 3 characters")
|
|
||||||
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
|
|
||||||
)
|
|
||||||
@@ -1,238 +0,0 @@
|
|||||||
from typing import List, Optional, Union
|
|
||||||
|
|
||||||
from gql import gql
|
|
||||||
|
|
||||||
from specklepy.core.api.models import Commit
|
|
||||||
from specklepy.core.api.resource import ResourceBase
|
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
|
||||||
|
|
||||||
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: Optional[List[str]] = None,
|
|
||||||
) -> Union[str, SpeckleException]:
|
|
||||||
"""
|
|
||||||
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
|
|
||||||
@@ -1,92 +0,0 @@
|
|||||||
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
|
|
||||||
)
|
|
||||||
@@ -1,172 +0,0 @@
|
|||||||
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,
|
|
||||||
)
|
|
||||||
@@ -1,187 +0,0 @@
|
|||||||
import re
|
|
||||||
from typing import Any, Dict, List, Tuple
|
|
||||||
|
|
||||||
import requests
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
server_info = self.make_request(
|
|
||||||
query=query, return_type="serverInfo", schema=ServerInfo
|
|
||||||
)
|
|
||||||
if isinstance(server_info, ServerInfo) and isinstance(
|
|
||||||
server_info.canonicalUrl, str
|
|
||||||
):
|
|
||||||
r = requests.get(
|
|
||||||
server_info.canonicalUrl, headers={"User-Agent": "specklepy SDK"}
|
|
||||||
)
|
|
||||||
if "x-speckle-frontend-2" in r.headers:
|
|
||||||
server_info.frontend2 = True
|
|
||||||
else:
|
|
||||||
server_info.frontend2 = False
|
|
||||||
|
|
||||||
return server_info
|
|
||||||
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
@@ -1,754 +0,0 @@
|
|||||||
from datetime import datetime, timezone
|
|
||||||
from typing import List, Optional
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
if len(name) < 3 and len(name) != 0:
|
|
||||||
return SpeckleException(message="Stream Name must be at least 3 characters")
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
@@ -1,138 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,325 +0,0 @@
|
|||||||
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,
|
|
||||||
)
|
|
||||||
@@ -1,282 +0,0 @@
|
|||||||
from urllib.parse import quote, unquote, urlparse
|
|
||||||
from warnings import warn
|
|
||||||
|
|
||||||
from gql import gql
|
|
||||||
|
|
||||||
from specklepy.core.api.client import SpeckleClient
|
|
||||||
from specklepy.core.api.credentials import (
|
|
||||||
Account,
|
|
||||||
get_account_from_token,
|
|
||||||
get_accounts_for_server,
|
|
||||||
)
|
|
||||||
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://app.speckle.systems/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
|
|
||||||
model_id: 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."
|
|
||||||
)
|
|
||||||
|
|
||||||
# check for fe2 URL
|
|
||||||
if "/projects/" in parsed.path:
|
|
||||||
use_fe2 = True
|
|
||||||
key_stream = "project"
|
|
||||||
else:
|
|
||||||
use_fe2 = False
|
|
||||||
key_stream = "stream"
|
|
||||||
|
|
||||||
while segments:
|
|
||||||
segment = segments.pop(0)
|
|
||||||
|
|
||||||
if use_fe2 is False:
|
|
||||||
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."
|
|
||||||
)
|
|
||||||
elif segments and use_fe2 is True:
|
|
||||||
if segment.lower() == "projects":
|
|
||||||
self.stream_id = segments.pop(0)
|
|
||||||
elif segment.lower() == "models":
|
|
||||||
next_segment = segments.pop(0)
|
|
||||||
if "," in next_segment:
|
|
||||||
raise SpeckleException("Multi-model urls are not supported yet")
|
|
||||||
elif unquote(next_segment).startswith("$"):
|
|
||||||
raise SpeckleException(
|
|
||||||
"Federation model urls are not supported"
|
|
||||||
)
|
|
||||||
elif len(next_segment) == 32:
|
|
||||||
self.object_id = next_segment
|
|
||||||
else:
|
|
||||||
self.branch_name = unquote(next_segment).split("@")[0]
|
|
||||||
if "@" in unquote(next_segment):
|
|
||||||
self.commit_id = unquote(next_segment).split("@")[1]
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise SpeckleException(
|
|
||||||
f"Cannot parse {url} into a stream wrapper class - invalid URL"
|
|
||||||
" provided."
|
|
||||||
)
|
|
||||||
|
|
||||||
if use_fe2 is True and self.branch_name is not None:
|
|
||||||
self.model_id = self.branch_name
|
|
||||||
# get branch name
|
|
||||||
query = gql(
|
|
||||||
"""
|
|
||||||
query Project($project_id: String!, $model_id: String!) {
|
|
||||||
project(id: $project_id) {
|
|
||||||
id
|
|
||||||
model(id: $model_id) {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
self._client = self.get_client()
|
|
||||||
params = {"project_id": self.stream_id, "model_id": self.model_id}
|
|
||||||
project = self._client.httpclient.execute(query, params)
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.branch_name = project["project"]["model"]["name"]
|
|
||||||
except KeyError as ke:
|
|
||||||
raise SpeckleException("Project model name is not found", ke)
|
|
||||||
|
|
||||||
if not self.stream_id:
|
|
||||||
raise SpeckleException(
|
|
||||||
f"Cannot parse {url} into a stream wrapper class - no {key_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(iter(get_accounts_for_server(self.host)), 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)
|
|
||||||
|
|
||||||
def to_string(self) -> str:
|
|
||||||
"""
|
|
||||||
Constructs a URL depending on the StreamWrapper type and FE version.
|
|
||||||
"""
|
|
||||||
use_fe2 = False
|
|
||||||
key_streams = "/streams/"
|
|
||||||
key_branches = "/branches/"
|
|
||||||
if isinstance(self.branch_name, str):
|
|
||||||
value_branch = quote(self.branch_name)
|
|
||||||
if self.branch_name == "globals":
|
|
||||||
key_branches = "/"
|
|
||||||
key_commits = "/commits/"
|
|
||||||
if isinstance(self.commit_id, str) and self.branch_name == "globals":
|
|
||||||
key_commits = "/globals/"
|
|
||||||
key_objects = "/objects/"
|
|
||||||
|
|
||||||
if "/projects/" in self.stream_url:
|
|
||||||
use_fe2 = True
|
|
||||||
key_streams = "/projects/"
|
|
||||||
key_branches = "/models/"
|
|
||||||
value_branch = self.model_id
|
|
||||||
key_commits = "@"
|
|
||||||
key_objects = "/models/"
|
|
||||||
|
|
||||||
wrapper_type = self.type
|
|
||||||
if use_fe2 is False or (use_fe2 is True and not self.model_id):
|
|
||||||
base_url = f"{self.server_url}{key_streams}{self.stream_id}"
|
|
||||||
else: # fe2 is True and model_id available
|
|
||||||
base_url = f"{self.server_url}{key_streams}{self.stream_id}{key_branches}{value_branch}"
|
|
||||||
|
|
||||||
if wrapper_type == "object":
|
|
||||||
return f"{base_url}{key_objects}{self.object_id}"
|
|
||||||
elif wrapper_type == "commit":
|
|
||||||
return f"{base_url}{key_commits}{self.commit_id}"
|
|
||||||
elif wrapper_type == "branch":
|
|
||||||
return f"{self.server_url}{key_streams}{self.stream_id}{key_branches}{value_branch}"
|
|
||||||
elif wrapper_type == "stream":
|
|
||||||
return f"{self.server_url}{key_streams}{self.stream_id}"
|
|
||||||
else:
|
|
||||||
raise SpeckleException(
|
|
||||||
f"Cannot parse StreamWrapper of type '{wrapper_type}'"
|
|
||||||
)
|
|
||||||
@@ -23,25 +23,23 @@ LOG = logging.getLogger(__name__)
|
|||||||
METRICS_TRACKER = None
|
METRICS_TRACKER = None
|
||||||
|
|
||||||
# actions
|
# actions
|
||||||
SDK = "SDK Action"
|
|
||||||
CONNECTOR = "Connector Action"
|
|
||||||
RECEIVE = "Receive"
|
RECEIVE = "Receive"
|
||||||
SEND = "Send"
|
SEND = "Send"
|
||||||
|
|
||||||
# not in use since 2.15
|
|
||||||
ACCOUNTS = "Get Local Accounts"
|
|
||||||
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 = "Stream Action"
|
||||||
STREAM_WRAPPER = "Stream Wrapper"
|
PERMISSION = "Permission Action"
|
||||||
|
INVITE = "Invite Action"
|
||||||
|
COMMIT = "Commit Action"
|
||||||
|
BRANCH = "Branch Action"
|
||||||
USER = "User Action"
|
USER = "User Action"
|
||||||
|
OTHER_USER = "Other User Action"
|
||||||
|
SERVER = "Server Action"
|
||||||
|
CLIENT = "Speckle Client"
|
||||||
|
STREAM_WRAPPER = "Stream Wrapper"
|
||||||
|
|
||||||
|
ACCOUNTS = "Get Local Accounts"
|
||||||
|
|
||||||
|
SERIALIZE = "serialization/serialize"
|
||||||
|
DESERIALIZE = "serialization/deserialize"
|
||||||
|
|
||||||
|
|
||||||
def disable():
|
def disable():
|
||||||
@@ -98,7 +96,7 @@ def initialise_tracker(account=None):
|
|||||||
if account and account.userInfo.email:
|
if account and account.userInfo.email:
|
||||||
METRICS_TRACKER.set_last_user(account.userInfo.email)
|
METRICS_TRACKER.set_last_user(account.userInfo.email)
|
||||||
if account and account.serverInfo.url:
|
if account and account.serverInfo.url:
|
||||||
METRICS_TRACKER.set_last_server(account.serverInfo.url)
|
METRICS_TRACKER.set_last_server(account.userInfo.email)
|
||||||
|
|
||||||
|
|
||||||
class Singleton(type):
|
class Singleton(type):
|
||||||
@@ -141,9 +139,7 @@ class MetricsTracker(metaclass=Singleton):
|
|||||||
self.last_server = self.hash(server)
|
self.last_server = self.hash(server)
|
||||||
|
|
||||||
def hash(self, value: str):
|
def hash(self, value: str):
|
||||||
inputList = value.lower().split("://")
|
return hashlib.md5(value.lower().encode("utf-8")).hexdigest().upper()
|
||||||
input = inputList[len(inputList) - 1].split("/")[0].split("?")[0]
|
|
||||||
return hashlib.md5(input.encode("utf-8")).hexdigest().upper()
|
|
||||||
|
|
||||||
def _send_tracking_requests(self):
|
def _send_tracking_requests(self):
|
||||||
session = requests.Session()
|
session = requests.Session()
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
from typing import Optional
|
|
||||||
|
|
||||||
from specklepy.objects.base import Base
|
|
||||||
|
|
||||||
|
|
||||||
class CRS(Base, speckle_type="Objects.GIS.CRS"):
|
|
||||||
"""A Coordinate Reference System stored in wkt format"""
|
|
||||||
|
|
||||||
name: Optional[str] = None
|
|
||||||
authority_id: Optional[str] = None
|
|
||||||
wkt: Optional[str] = None
|
|
||||||
units_native: Optional[str] = None
|
|
||||||
offset_x: Optional[float] = None
|
|
||||||
offset_y: Optional[float] = None
|
|
||||||
rotation: Optional[float] = None
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
from typing import List, Optional
|
|
||||||
|
|
||||||
from specklepy.objects.base import Base
|
|
||||||
|
|
||||||
|
|
||||||
class GisFeature(
|
|
||||||
Base, speckle_type="Objects.GIS.GisFeature", detachable={"displayValue"}
|
|
||||||
):
|
|
||||||
"""GIS Feature"""
|
|
||||||
|
|
||||||
geometry: Optional[List[Base]] = None
|
|
||||||
attributes: Base
|
|
||||||
displayValue: Optional[List[Base]] = None
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
"""Builtin Speckle object kit."""
|
|
||||||
|
|
||||||
from specklepy.objects.GIS.CRS import CRS
|
|
||||||
from specklepy.objects.GIS.geometry import (
|
|
||||||
GisLineElement,
|
|
||||||
GisPointElement,
|
|
||||||
GisPolygonElement,
|
|
||||||
GisPolygonGeometry,
|
|
||||||
GisRasterElement,
|
|
||||||
)
|
|
||||||
from specklepy.objects.GIS.layers import RasterLayer, VectorLayer
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"VectorLayer",
|
|
||||||
"RasterLayer",
|
|
||||||
"GisPolygonGeometry",
|
|
||||||
"GisPolygonElement",
|
|
||||||
"GisLineElement",
|
|
||||||
"GisPointElement",
|
|
||||||
"GisRasterElement",
|
|
||||||
"CRS",
|
|
||||||
]
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
from typing import List, Optional, Union
|
|
||||||
|
|
||||||
from specklepy.objects.base import Base
|
|
||||||
from specklepy.objects.geometry import (
|
|
||||||
Arc,
|
|
||||||
Circle,
|
|
||||||
Line,
|
|
||||||
Mesh,
|
|
||||||
Point,
|
|
||||||
Polycurve,
|
|
||||||
Polyline,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class GisPolygonGeometry(
|
|
||||||
Base, speckle_type="Objects.GIS.PolygonGeometry", detachable={"displayValue"}
|
|
||||||
):
|
|
||||||
"""GIS Polygon Geometry"""
|
|
||||||
|
|
||||||
boundary: Optional[Union[Polyline, Arc, Line, Circle, Polycurve]] = None
|
|
||||||
voids: Optional[List[Union[Polyline, Arc, Line, Circle, Polycurve]]] = None
|
|
||||||
displayValue: Optional[List[Mesh]] = None
|
|
||||||
|
|
||||||
|
|
||||||
class GisPolygonElement(Base, speckle_type="Objects.GIS.PolygonElement"):
|
|
||||||
"""GIS Polygon element"""
|
|
||||||
|
|
||||||
geometry: Optional[List[GisPolygonGeometry]] = None
|
|
||||||
attributes: Optional[Base] = None
|
|
||||||
|
|
||||||
|
|
||||||
class GisLineElement(Base, speckle_type="Objects.GIS.LineElement"):
|
|
||||||
"""GIS Polyline element"""
|
|
||||||
|
|
||||||
geometry: Optional[List[Union[Polyline, Arc, Line, Circle, Polycurve]]] = None
|
|
||||||
attributes: Optional[Base] = None
|
|
||||||
|
|
||||||
|
|
||||||
class GisPointElement(Base, speckle_type="Objects.GIS.PointElement"):
|
|
||||||
"""GIS Point element"""
|
|
||||||
|
|
||||||
geometry: Optional[List[Point]] = None
|
|
||||||
attributes: Optional[Base] = None
|
|
||||||
|
|
||||||
|
|
||||||
class GisRasterElement(
|
|
||||||
Base, speckle_type="Objects.GIS.RasterElement", detachable={"displayValue"}
|
|
||||||
):
|
|
||||||
"""GIS Raster element"""
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class GisTopography(
|
|
||||||
GisRasterElement,
|
|
||||||
speckle_type="Objects.GIS.GisTopography",
|
|
||||||
detachable={"displayValue"},
|
|
||||||
):
|
|
||||||
"""GIS Raster element with 3d Topography representation"""
|
|
||||||
|
|
||||||
|
|
||||||
class GisNonGeometryElement(Base, speckle_type="Objects.GIS.NonGeometryElement"):
|
|
||||||
"""GIS Table feature"""
|
|
||||||
|
|
||||||
attributes: Optional[Base] = None
|
|
||||||
@@ -1,142 +0,0 @@
|
|||||||
from typing import Any, Dict, List, Optional, Union
|
|
||||||
|
|
||||||
from deprecated import deprecated
|
|
||||||
|
|
||||||
from specklepy.objects.base import Base
|
|
||||||
from specklepy.objects.GIS.CRS import CRS
|
|
||||||
from specklepy.objects.other import Collection
|
|
||||||
|
|
||||||
|
|
||||||
@deprecated(version="2.15", reason="Use VectorLayer or RasterLayer instead")
|
|
||||||
class Layer(Base, detachable={"features"}):
|
|
||||||
"""A GIS Layer"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
name: Optional[str] = None,
|
|
||||||
crs: Optional[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 {}
|
|
||||||
|
|
||||||
|
|
||||||
@deprecated(version="2.16", reason="Use VectorLayer or RasterLayer instead")
|
|
||||||
class VectorLayer(
|
|
||||||
Collection,
|
|
||||||
detachable={"elements"},
|
|
||||||
speckle_type="VectorLayer",
|
|
||||||
serialize_ignore={"features"},
|
|
||||||
):
|
|
||||||
"""GIS Vector Layer"""
|
|
||||||
|
|
||||||
name: Optional[str] = None
|
|
||||||
crs: Optional[Union[CRS, Base]] = None
|
|
||||||
units: Optional[str] = None
|
|
||||||
elements: Optional[List[Base]] = None
|
|
||||||
attributes: Optional[Base] = None
|
|
||||||
geomType: Optional[str] = "None"
|
|
||||||
renderer: Optional[Dict[str, Any]] = None
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
@deprecated(version="2.16", reason="Use VectorLayer or RasterLayer instead")
|
|
||||||
class RasterLayer(
|
|
||||||
Collection,
|
|
||||||
detachable={"elements"},
|
|
||||||
speckle_type="RasterLayer",
|
|
||||||
serialize_ignore={"features"},
|
|
||||||
):
|
|
||||||
"""GIS Raster Layer"""
|
|
||||||
|
|
||||||
name: Optional[str] = None
|
|
||||||
crs: Optional[Union[CRS, Base]] = None
|
|
||||||
units: Optional[str] = None
|
|
||||||
rasterCrs: Optional[Union[CRS, Base]] = None
|
|
||||||
elements: Optional[List[Base]] = None
|
|
||||||
geomType: Optional[str] = "None"
|
|
||||||
renderer: Optional[Dict[str, Any]] = None
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class VectorLayer( # noqa: F811
|
|
||||||
Collection,
|
|
||||||
detachable={"elements"},
|
|
||||||
speckle_type="Objects.GIS.VectorLayer",
|
|
||||||
serialize_ignore={"features"},
|
|
||||||
):
|
|
||||||
"""GIS Vector Layer"""
|
|
||||||
|
|
||||||
name: Optional[str] = None
|
|
||||||
crs: Optional[Union[CRS, Base]] = None
|
|
||||||
units: Optional[str] = None
|
|
||||||
elements: Optional[List[Base]] = None
|
|
||||||
attributes: Optional[Base] = None
|
|
||||||
geomType: Optional[str] = "None"
|
|
||||||
renderer: Optional[Dict[str, Any]] = None
|
|
||||||
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( # noqa: F811
|
|
||||||
Collection,
|
|
||||||
detachable={"elements"},
|
|
||||||
speckle_type="Objects.GIS.RasterLayer",
|
|
||||||
serialize_ignore={"features"},
|
|
||||||
):
|
|
||||||
"""GIS Raster Layer"""
|
|
||||||
|
|
||||||
name: Optional[str] = None
|
|
||||||
crs: Optional[Union[CRS, Base]] = None
|
|
||||||
units: Optional[str] = None
|
|
||||||
rasterCrs: Optional[Union[CRS, Base]] = None
|
|
||||||
elements: Optional[List[Base]] = None
|
|
||||||
geomType: Optional[str] = "None"
|
|
||||||
renderer: Optional[Dict[str, Any]] = None
|
|
||||||
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
|
|
||||||
@@ -1,23 +1,6 @@
|
|||||||
"""Builtin Speckle object kit."""
|
"""Builtin Speckle object kit."""
|
||||||
|
|
||||||
from specklepy.objects import (
|
from specklepy.objects import encoding, geometry, other, primitive, structural, units
|
||||||
GIS,
|
|
||||||
encoding,
|
|
||||||
geometry,
|
|
||||||
other,
|
|
||||||
primitive,
|
|
||||||
structural,
|
|
||||||
units,
|
|
||||||
)
|
|
||||||
from specklepy.objects.base import Base
|
from specklepy.objects.base import Base
|
||||||
|
|
||||||
__all__ = [
|
__all__ = ["Base", "encoding", "geometry", "other", "units", "structural", "primitive"]
|
||||||
"Base",
|
|
||||||
"encoding",
|
|
||||||
"geometry",
|
|
||||||
"other",
|
|
||||||
"units",
|
|
||||||
"structural",
|
|
||||||
"primitive",
|
|
||||||
"GIS",
|
|
||||||
]
|
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ from warnings import warn
|
|||||||
|
|
||||||
from stringcase import pascalcase
|
from stringcase import pascalcase
|
||||||
|
|
||||||
from specklepy.logging.exceptions import SpeckleException, SpeckleInvalidUnitException
|
from specklepy.logging.exceptions import SpeckleException
|
||||||
from specklepy.objects.units import Units
|
from specklepy.objects.units import Units, get_units_from_string
|
||||||
from specklepy.transports.memory import MemoryTransport
|
from specklepy.transports.memory import MemoryTransport
|
||||||
|
|
||||||
PRIMITIVES = (int, float, str, bool)
|
PRIMITIVES = (int, float, str, bool)
|
||||||
@@ -188,8 +188,7 @@ class _RegisteringBase:
|
|||||||
cls._detachable = cls._detachable.union(detachable)
|
cls._detachable = cls._detachable.union(detachable)
|
||||||
if serialize_ignore:
|
if serialize_ignore:
|
||||||
cls._serialize_ignore = cls._serialize_ignore.union(serialize_ignore)
|
cls._serialize_ignore = cls._serialize_ignore.union(serialize_ignore)
|
||||||
# we know, that the super here is object, that takes no args on init subclass
|
super().__init_subclass__(**kwargs)
|
||||||
return super().__init_subclass__()
|
|
||||||
|
|
||||||
|
|
||||||
# T = TypeVar("T")
|
# T = TypeVar("T")
|
||||||
@@ -323,7 +322,7 @@ class Base(_RegisteringBase):
|
|||||||
id: Union[str, None] = None
|
id: Union[str, None] = None
|
||||||
totalChildrenCount: Union[int, None] = None
|
totalChildrenCount: Union[int, None] = None
|
||||||
applicationId: Union[str, None] = None
|
applicationId: Union[str, None] = None
|
||||||
_units: Union[None, str] = None
|
_units: Union[Units, None] = None
|
||||||
|
|
||||||
def __init__(self, **kwargs) -> None:
|
def __init__(self, **kwargs) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@@ -464,19 +463,22 @@ class Base(_RegisteringBase):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def units(self) -> Union[str, None]:
|
def units(self) -> Union[str, None]:
|
||||||
return self._units
|
if self._units:
|
||||||
|
return self._units.value
|
||||||
|
return None
|
||||||
|
|
||||||
@units.setter
|
@units.setter
|
||||||
def units(self, value: Union[str, Units, None]):
|
def units(self, value: Union[str, Units, None]):
|
||||||
"""While this property accepts any string value, geometry expects units to be specific strings (see Units enum)"""
|
if value is None:
|
||||||
if isinstance(value, str) or value is None:
|
units = value
|
||||||
self._units = value
|
|
||||||
elif isinstance(value, Units):
|
elif isinstance(value, Units):
|
||||||
self._units = value.value
|
units: Units = value
|
||||||
else:
|
else:
|
||||||
raise SpeckleInvalidUnitException(
|
units = get_units_from_string(value)
|
||||||
f"Unknown type {type(value)} received for units"
|
self._units = units
|
||||||
)
|
# except SpeckleInvalidUnitException as ex:
|
||||||
|
# warn(f"Units are reset to None. Reason {ex.message}")
|
||||||
|
# self._units = None
|
||||||
|
|
||||||
def get_member_names(self) -> List[str]:
|
def get_member_names(self) -> List[str]:
|
||||||
"""Get all of the property names on this object, dynamic or not"""
|
"""Get all of the property names on this object, dynamic or not"""
|
||||||
|
|||||||
@@ -39,11 +39,7 @@ class Point(Base, speckle_type=GEOMETRY + "Point"):
|
|||||||
return pt
|
return pt
|
||||||
|
|
||||||
|
|
||||||
class Pointcloud(
|
class Pointcloud(Base, speckle_type=GEOMETRY + "Pointcloud"):
|
||||||
Base,
|
|
||||||
speckle_type=GEOMETRY + "Pointcloud",
|
|
||||||
chunkable={"points": 31250, "colors": 62500, "sizes": 62500},
|
|
||||||
):
|
|
||||||
points: Optional[List[float]] = None
|
points: Optional[List[float]] = None
|
||||||
colors: Optional[List[int]] = None
|
colors: Optional[List[int]] = None
|
||||||
sizes: Optional[List[float]] = None
|
sizes: Optional[List[float]] = None
|
||||||
@@ -303,15 +299,15 @@ class Polyline(Base, speckle_type=GEOMETRY + "Polyline", chunkable={"value": 200
|
|||||||
|
|
||||||
|
|
||||||
class SpiralType(Enum):
|
class SpiralType(Enum):
|
||||||
Biquadratic = 0
|
Biquadratic = (0,)
|
||||||
BiquadraticParabola = 1
|
BiquadraticParabola = (1,)
|
||||||
Bloss = 2
|
Bloss = (2,)
|
||||||
Clothoid = 3
|
Clothoid = (3,)
|
||||||
Cosine = 4
|
Cosine = (4,)
|
||||||
Cubic = 5
|
Cubic = (5,)
|
||||||
CubicParabola = 6
|
CubicParabola = (6,)
|
||||||
Radioid = 7
|
Radioid = (7,)
|
||||||
Sinusoid = 8
|
Sinusoid = (8,)
|
||||||
Unknown = 9
|
Unknown = 9
|
||||||
|
|
||||||
|
|
||||||
@@ -319,7 +315,7 @@ class Spiral(Base, speckle_type=GEOMETRY + "Spiral", detachable={"displayValue"}
|
|||||||
startPoint: Optional[Point] = None
|
startPoint: Optional[Point] = None
|
||||||
endPoint: Optional[Point]
|
endPoint: Optional[Point]
|
||||||
plane: Optional[Plane]
|
plane: Optional[Plane]
|
||||||
turns: Optional[float]
|
turns: Optional[int]
|
||||||
pitchAxis: Optional[Vector] = Vector()
|
pitchAxis: Optional[Vector] = Vector()
|
||||||
pitch: float = 0
|
pitch: float = 0
|
||||||
spiralType: Optional[SpiralType] = None
|
spiralType: Optional[SpiralType] = None
|
||||||
@@ -898,7 +894,7 @@ class Brep(
|
|||||||
def VerticesValue(self) -> List[Point]:
|
def VerticesValue(self) -> List[Point]:
|
||||||
if self.Vertices is None:
|
if self.Vertices is None:
|
||||||
return None
|
return None
|
||||||
encoded_unit = get_encoding_from_units(self.Vertices[0].units)
|
encoded_unit = get_encoding_from_units(self.Vertices[0]._units)
|
||||||
values = [encoded_unit]
|
values = [encoded_unit]
|
||||||
for vertex in self.Vertices:
|
for vertex in self.Vertices:
|
||||||
values.extend(vertex.to_list())
|
values.extend(vertex.to_list())
|
||||||
@@ -913,7 +909,7 @@ class Brep(
|
|||||||
|
|
||||||
for i in range(0, len(value), 3):
|
for i in range(0, len(value), 3):
|
||||||
vertex = Point.from_list(value[i : i + 3])
|
vertex = Point.from_list(value[i : i + 3])
|
||||||
vertex.units = units
|
vertex._units = units
|
||||||
vertices.append(vertex)
|
vertices.append(vertex)
|
||||||
|
|
||||||
self.Vertices = vertices
|
self.Vertices = vertices
|
||||||
|
|||||||
@@ -1,100 +0,0 @@
|
|||||||
from abc import ABC, abstractmethod
|
|
||||||
from typing import Any, Collection, Dict, Generic, Iterable, Optional, Tuple, TypeVar
|
|
||||||
|
|
||||||
from attrs import define
|
|
||||||
|
|
||||||
from specklepy.objects.base import Base
|
|
||||||
|
|
||||||
ROOT: str = "__Root"
|
|
||||||
|
|
||||||
T = TypeVar("T")
|
|
||||||
PARENT_INFO = Tuple[Optional[str], str]
|
|
||||||
|
|
||||||
|
|
||||||
@define(slots=True)
|
|
||||||
class CommitObjectBuilder(ABC, Generic[T]):
|
|
||||||
converted: Dict[str, Base]
|
|
||||||
_parent_infos: Dict[str, Collection[PARENT_INFO]]
|
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self.converted = {}
|
|
||||||
self._parent_infos = {}
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def include_object(self, conversion_result: Base, native_object: T) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def build_commit_object(self, root_commit_object: Base) -> None:
|
|
||||||
self.apply_relationships(self.converted.values(), root_commit_object)
|
|
||||||
|
|
||||||
def set_relationship(
|
|
||||||
self, app_id: Optional[str], *parent_info: PARENT_INFO
|
|
||||||
) -> None:
|
|
||||||
if not app_id:
|
|
||||||
return
|
|
||||||
|
|
||||||
self._parent_infos[app_id] = parent_info
|
|
||||||
|
|
||||||
def apply_relationships(
|
|
||||||
self, to_add: Iterable[Base], root_commit_object: Base
|
|
||||||
) -> None:
|
|
||||||
for c in to_add:
|
|
||||||
try:
|
|
||||||
self.apply_relationship(c, root_commit_object)
|
|
||||||
except Exception as ex:
|
|
||||||
print(f"Failed to add object {type(c)} to commit object: {ex}")
|
|
||||||
|
|
||||||
def apply_relationship(self, current: Base, root_commit_object: Base):
|
|
||||||
if not current.applicationId:
|
|
||||||
raise Exception("Expected applicationId to have been set")
|
|
||||||
|
|
||||||
parents = self._parent_infos[current.applicationId]
|
|
||||||
|
|
||||||
for parent_id, prop_name in parents:
|
|
||||||
if not parent_id:
|
|
||||||
continue
|
|
||||||
|
|
||||||
parent: Optional[Base]
|
|
||||||
if parent_id == ROOT:
|
|
||||||
parent = root_commit_object
|
|
||||||
else:
|
|
||||||
parent = (
|
|
||||||
self.converted[parent_id] if parent_id in self.converted else None
|
|
||||||
)
|
|
||||||
|
|
||||||
if not parent:
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
|
||||||
elements = get_detached_prop(parent, prop_name)
|
|
||||||
if not isinstance(elements, list):
|
|
||||||
elements = []
|
|
||||||
set_detached_prop(parent, prop_name, elements)
|
|
||||||
|
|
||||||
elements.append(current)
|
|
||||||
return
|
|
||||||
except Exception as ex:
|
|
||||||
# A parent was found, but it was invalid (Likely because of a type mismatch on a `elements` property)
|
|
||||||
print(
|
|
||||||
f"Failed to add object {type(current)} to a converted parent; {ex}"
|
|
||||||
)
|
|
||||||
|
|
||||||
raise Exception(
|
|
||||||
f"Could not find a valid parent for object of type {type(current)}. Checked {len(parents)} potential parent, and non were converted!"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_detached_prop(speckle_object: Base, prop_name: str) -> Optional[Any]:
|
|
||||||
detached_prop_name = get_detached_prop_name(speckle_object, prop_name)
|
|
||||||
return getattr(speckle_object, detached_prop_name, None)
|
|
||||||
|
|
||||||
|
|
||||||
def set_detached_prop(
|
|
||||||
speckle_object: Base, prop_name: str, value: Optional[Any]
|
|
||||||
) -> None:
|
|
||||||
detached_prop_name = get_detached_prop_name(speckle_object, prop_name)
|
|
||||||
setattr(speckle_object, detached_prop_name, value)
|
|
||||||
|
|
||||||
|
|
||||||
def get_detached_prop_name(speckle_object: Base, prop_name: str) -> str:
|
|
||||||
return prop_name if hasattr(speckle_object, prop_name) else f"@{prop_name}"
|
|
||||||
@@ -1,125 +0,0 @@
|
|||||||
from typing import Any, Callable, Collection, Iterable, Iterator, List, Optional, Set
|
|
||||||
|
|
||||||
from attrs import define
|
|
||||||
from typing_extensions import Protocol, final
|
|
||||||
|
|
||||||
from specklepy.objects.base import Base
|
|
||||||
|
|
||||||
|
|
||||||
class ITraversalRule(Protocol):
|
|
||||||
def get_members_to_traverse(self, o: Base) -> Set[str]:
|
|
||||||
"""Get the members to traverse."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def does_rule_hold(self, o: Base) -> bool:
|
|
||||||
"""Make sure the rule still holds."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@final
|
|
||||||
@define(slots=True, frozen=True)
|
|
||||||
class DefaultRule:
|
|
||||||
def get_members_to_traverse(self, _) -> Set[str]:
|
|
||||||
return set()
|
|
||||||
|
|
||||||
def does_rule_hold(self, _) -> bool:
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
# we're creating a local protected "singleton"
|
|
||||||
_default_rule = DefaultRule()
|
|
||||||
|
|
||||||
|
|
||||||
@final
|
|
||||||
@define(slots=True, frozen=True)
|
|
||||||
class TraversalContext:
|
|
||||||
current: Base
|
|
||||||
member_name: Optional[str] = None
|
|
||||||
parent: Optional["TraversalContext"] = None
|
|
||||||
|
|
||||||
|
|
||||||
@final
|
|
||||||
@define(slots=True, frozen=True)
|
|
||||||
class GraphTraversal:
|
|
||||||
_rules: List[ITraversalRule]
|
|
||||||
|
|
||||||
def traverse(self, root: Base) -> Iterator[TraversalContext]:
|
|
||||||
stack: List[TraversalContext] = []
|
|
||||||
|
|
||||||
stack.append(TraversalContext(root))
|
|
||||||
|
|
||||||
while len(stack) > 0:
|
|
||||||
head = stack.pop()
|
|
||||||
yield head
|
|
||||||
|
|
||||||
current = head.current
|
|
||||||
active_rule = self._get_active_rule_or_default_rule(current)
|
|
||||||
members_to_traverse = active_rule.get_members_to_traverse(current)
|
|
||||||
for child_prop in members_to_traverse:
|
|
||||||
try:
|
|
||||||
if child_prop in {"speckle_type", "units", "applicationId"}:
|
|
||||||
continue # debug: to avoid noisy exceptions, explicitly avoid checking ones we know will fail, this is not exhaustive
|
|
||||||
if getattr(current, child_prop, None):
|
|
||||||
value = current[child_prop]
|
|
||||||
self._traverse_member_to_stack(stack, value, child_prop, head)
|
|
||||||
except KeyError:
|
|
||||||
# Unset application ids, and class variables like SpeckleType will throw when __getitem__ is called
|
|
||||||
pass
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _traverse_member_to_stack(
|
|
||||||
stack: List[TraversalContext],
|
|
||||||
value: Any,
|
|
||||||
member_name: Optional[str] = None,
|
|
||||||
parent: Optional[TraversalContext] = None,
|
|
||||||
):
|
|
||||||
if isinstance(value, Base):
|
|
||||||
stack.append(TraversalContext(value, member_name, parent))
|
|
||||||
elif isinstance(value, list):
|
|
||||||
for obj in value:
|
|
||||||
GraphTraversal._traverse_member_to_stack(
|
|
||||||
stack, obj, member_name, parent
|
|
||||||
)
|
|
||||||
elif isinstance(value, dict):
|
|
||||||
for obj in value.values():
|
|
||||||
GraphTraversal._traverse_member_to_stack(
|
|
||||||
stack, obj, member_name, parent
|
|
||||||
)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def traverse_member(value: Optional[Any]) -> Iterator[Base]:
|
|
||||||
if isinstance(value, Base):
|
|
||||||
yield value
|
|
||||||
elif isinstance(value, list):
|
|
||||||
for obj in value:
|
|
||||||
for o in GraphTraversal.traverse_member(obj):
|
|
||||||
yield o
|
|
||||||
elif isinstance(value, dict):
|
|
||||||
for obj in value.values():
|
|
||||||
for o in GraphTraversal.traverse_member(obj):
|
|
||||||
yield o
|
|
||||||
|
|
||||||
def _get_active_rule_or_default_rule(self, o: Base) -> ITraversalRule:
|
|
||||||
return self._get_active_rule(o) or _default_rule
|
|
||||||
|
|
||||||
def _get_active_rule(self, o: Base) -> Optional[ITraversalRule]:
|
|
||||||
for rule in self._rules:
|
|
||||||
if rule.does_rule_hold(o):
|
|
||||||
return rule
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
@final
|
|
||||||
@define(slots=True, frozen=True)
|
|
||||||
class TraversalRule:
|
|
||||||
_conditions: Collection[Callable[[Base], bool]]
|
|
||||||
_members_to_traverse: Callable[[Base], Iterable[str]]
|
|
||||||
|
|
||||||
def get_members_to_traverse(self, o: Base) -> Set[str]:
|
|
||||||
return set(self._members_to_traverse(o))
|
|
||||||
|
|
||||||
def does_rule_hold(self, o: Base) -> bool:
|
|
||||||
for condition in self._conditions:
|
|
||||||
if condition(o):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
from typing import Any, List, Optional
|
from typing import Any, List, Optional
|
||||||
|
|
||||||
from deprecated import deprecated
|
from deprecated import deprecated
|
||||||
|
|
||||||
from specklepy.objects.geometry import Plane, Point, Polyline, Vector
|
from specklepy.objects.geometry import Point, Vector
|
||||||
|
|
||||||
from .base import Base
|
from .base import Base
|
||||||
|
|
||||||
@@ -72,19 +71,6 @@ class DisplayStyle(Base, speckle_type=OTHER + "DisplayStyle"):
|
|||||||
lineweight: float = 0
|
lineweight: float = 0
|
||||||
|
|
||||||
|
|
||||||
class Text(Base, speckle_type=OTHER + "Text"):
|
|
||||||
"""
|
|
||||||
Text object to render it on viewer.
|
|
||||||
"""
|
|
||||||
|
|
||||||
plane: Plane
|
|
||||||
value: str
|
|
||||||
height: float
|
|
||||||
rotation: float
|
|
||||||
displayValue: Optional[List[Polyline]] = None
|
|
||||||
richText: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
class Transform(
|
class Transform(
|
||||||
Base,
|
Base,
|
||||||
speckle_type=OTHER + "Transform",
|
speckle_type=OTHER + "Transform",
|
||||||
@@ -261,7 +247,9 @@ class BlockDefinition(
|
|||||||
geometry: Optional[List[Base]] = None
|
geometry: Optional[List[Base]] = None
|
||||||
|
|
||||||
|
|
||||||
class Instance(Base, speckle_type=OTHER + "Instance", detachable={"definition"}):
|
class Instance(
|
||||||
|
Base, speckle_type=OTHER + "Instance", detachable={"definition"}
|
||||||
|
):
|
||||||
transform: Optional[Transform] = None
|
transform: Optional[Transform] = None
|
||||||
definition: Optional[Base] = None
|
definition: Optional[Base] = None
|
||||||
|
|
||||||
@@ -280,17 +268,17 @@ class BlockInstance(
|
|||||||
def blockDefinition(self, value: Optional[BlockDefinition]) -> None:
|
def blockDefinition(self, value: Optional[BlockDefinition]) -> None:
|
||||||
self.definition = value
|
self.definition = value
|
||||||
|
|
||||||
|
|
||||||
class RevitInstance(Instance, speckle_type=OTHER_REVIT + "RevitInstance"):
|
class RevitInstance(Instance, speckle_type=OTHER_REVIT + "RevitInstance"):
|
||||||
level: Optional[Base] = None
|
level: Optional[Base] = None
|
||||||
facingFlipped: bool
|
facingFlipped: bool
|
||||||
handFlipped: bool
|
handFlipped: bool
|
||||||
parameters: Optional[Base] = None
|
parameters: Optional[Base] = None
|
||||||
elementId: Optional[str]
|
elementId: Optional[str]
|
||||||
|
|
||||||
|
|
||||||
# TODO: prob move this into a built elements module, but just trialling this for now
|
# 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
|
name: Optional[str] = None
|
||||||
value: Any = None
|
value: Any = None
|
||||||
applicationUnitType: Optional[str] = None # eg UnitType UT_Length
|
applicationUnitType: Optional[str] = None # eg UnitType UT_Length
|
||||||
@@ -302,10 +290,9 @@ class RevitParameter(Base, speckle_type="Objects.BuiltElements.Revit.Parameter")
|
|||||||
isReadOnly: bool = False
|
isReadOnly: bool = False
|
||||||
isTypeParameter: bool = False
|
isTypeParameter: bool = False
|
||||||
|
|
||||||
|
|
||||||
class Collection(
|
class Collection(
|
||||||
Base, speckle_type="Speckle.Core.Models.Collection", detachable={"elements"}
|
Base, speckle_type="Speckle.Core.Models.Collection", detachable={"elements"}
|
||||||
):
|
):
|
||||||
name: Optional[str] = None
|
name: Optional[str] = None
|
||||||
collectionType: Optional[str] = None
|
collectionType: Optional[str] = None
|
||||||
elements: Optional[List[Base]] = None
|
elements: Optional[List[Base]] = None
|
||||||
@@ -6,7 +6,10 @@ from specklepy.objects.structural.analysis import (
|
|||||||
ModelSettings,
|
ModelSettings,
|
||||||
ModelUnits,
|
ModelUnits,
|
||||||
)
|
)
|
||||||
from specklepy.objects.structural.axis import Axis, AxisType
|
from specklepy.objects.structural.axis import (
|
||||||
|
AxisType,
|
||||||
|
Axis
|
||||||
|
)
|
||||||
from specklepy.objects.structural.geometry import (
|
from specklepy.objects.structural.geometry import (
|
||||||
Element1D,
|
Element1D,
|
||||||
Element2D,
|
Element2D,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from enum import Enum
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
from specklepy.objects.base import Base
|
from specklepy.objects.base import Base
|
||||||
from specklepy.objects.geometry import Plane
|
from specklepy.objects.geometry import Plane
|
||||||
|
|||||||
@@ -65,7 +65,6 @@ class ShapeType(int, Enum):
|
|||||||
Box = 7
|
Box = 7
|
||||||
Catalogue = 8
|
Catalogue = 8
|
||||||
Explicit = 9
|
Explicit = 9
|
||||||
Undefined = 10
|
|
||||||
|
|
||||||
|
|
||||||
class PropertyTypeSpring(int, Enum):
|
class PropertyTypeSpring(int, Enum):
|
||||||
@@ -91,9 +90,7 @@ class Property(Base, speckle_type=STRUCTURAL_PROPERTY):
|
|||||||
name: Optional[str] = None
|
name: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
class SectionProfile(
|
class SectionProfile(Base, speckle_type=STRUCTURAL_PROPERTY + ".Profiles.SectionProfile"):
|
||||||
Base, speckle_type=STRUCTURAL_PROPERTY + ".Profiles.SectionProfile"
|
|
||||||
):
|
|
||||||
name: Optional[str] = None
|
name: Optional[str] = None
|
||||||
shapeType: Optional[ShapeType] = None
|
shapeType: Optional[ShapeType] = None
|
||||||
area: float = 0.0
|
area: float = 0.0
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ UNITS_STRINGS = {
|
|||||||
Units.none: ["none", "null"],
|
Units.none: ["none", "null"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
UNITS_ENCODINGS = {
|
UNITS_ENCODINGS = {
|
||||||
Units.none: 0,
|
Units.none: 0,
|
||||||
None: 0,
|
None: 0,
|
||||||
@@ -50,20 +49,6 @@ UNITS_ENCODINGS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
UNIT_SCALE = {
|
|
||||||
Units.none: 1,
|
|
||||||
Units.mm: 0.001,
|
|
||||||
Units.cm: 0.01,
|
|
||||||
Units.m: 1.0,
|
|
||||||
Units.km: 1000.0,
|
|
||||||
Units.inches: 0.0254,
|
|
||||||
Units.feet: 0.3048,
|
|
||||||
Units.yards: 0.9144,
|
|
||||||
Units.miles: 1609.340,
|
|
||||||
}
|
|
||||||
"""Unit scaling factor to meters"""
|
|
||||||
|
|
||||||
|
|
||||||
def get_units_from_string(unit: str) -> Units:
|
def get_units_from_string(unit: str) -> Units:
|
||||||
if not isinstance(unit, str):
|
if not isinstance(unit, str):
|
||||||
raise SpeckleInvalidUnitException(unit)
|
raise SpeckleInvalidUnitException(unit)
|
||||||
@@ -74,10 +59,10 @@ def get_units_from_string(unit: str) -> Units:
|
|||||||
raise SpeckleInvalidUnitException(unit)
|
raise SpeckleInvalidUnitException(unit)
|
||||||
|
|
||||||
|
|
||||||
def get_units_from_encoding(unit: int) -> Units:
|
def get_units_from_encoding(unit: int):
|
||||||
for name, encoding in UNITS_ENCODINGS.items():
|
for name, encoding in UNITS_ENCODINGS.items():
|
||||||
if unit == encoding:
|
if unit == encoding:
|
||||||
return name or Units.none
|
return name
|
||||||
|
|
||||||
raise SpeckleException(
|
raise SpeckleException(
|
||||||
message=(
|
message=(
|
||||||
@@ -87,38 +72,13 @@ def get_units_from_encoding(unit: int) -> Units:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_encoding_from_units(unit: Union[Units, str, None]):
|
def get_encoding_from_units(unit: Union[Units, None]):
|
||||||
maybe_sanitized_unit = unit
|
|
||||||
if isinstance(unit, str):
|
|
||||||
for unit_enum, aliases in UNITS_STRINGS.items():
|
|
||||||
if unit in aliases:
|
|
||||||
maybe_sanitized_unit = unit_enum
|
|
||||||
try:
|
try:
|
||||||
return UNITS_ENCODINGS[maybe_sanitized_unit]
|
return UNITS_ENCODINGS[unit]
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
raise SpeckleException(
|
raise SpeckleException(
|
||||||
message=(
|
message=(
|
||||||
f"No encoding exists for unit {maybe_sanitized_unit}."
|
f"No encoding exists for unit {unit}."
|
||||||
f"Please enter a valid unit to encode (eg {UNITS_ENCODINGS})."
|
f"Please enter a valid unit to encode (eg {UNITS_ENCODINGS})."
|
||||||
)
|
)
|
||||||
) from e
|
) from e
|
||||||
|
|
||||||
|
|
||||||
def get_scale_factor_from_string(fromUnits: str, toUnits: str) -> float:
|
|
||||||
"""Returns a scalar to convert distance values from one unit system to another"""
|
|
||||||
return get_scale_factor(
|
|
||||||
get_units_from_string(fromUnits), get_units_from_string(toUnits)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_scale_factor(fromUnits: Units, toUnits: Units) -> float:
|
|
||||||
"""Returns a scalar to convert distance values from one unit system to another"""
|
|
||||||
return get_scale_factor_to_meters(fromUnits) / get_scale_factor_to_meters(toUnits)
|
|
||||||
|
|
||||||
|
|
||||||
def get_scale_factor_to_meters(fromUnits: Units) -> float:
|
|
||||||
"""Returns a scalar to convert distance values from one unit system to meters"""
|
|
||||||
if fromUnits not in UNIT_SCALE:
|
|
||||||
raise ValueError(f"Invalid units provided: {fromUnits}")
|
|
||||||
|
|
||||||
return UNIT_SCALE[fromUnits]
|
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from pydantic.config import Extra
|
||||||
|
|
||||||
|
|
||||||
|
class AbstractTransport(ABC, BaseModel):
|
||||||
|
_name: str = "Abstract"
|
||||||
|
|
||||||
class AbstractTransport(ABC):
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
|
||||||
def name(self):
|
def name(self):
|
||||||
pass
|
return type(self)._name
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def begin_write(self) -> None:
|
def begin_write(self) -> None:
|
||||||
@@ -83,3 +87,7 @@ class AbstractTransport(ABC):
|
|||||||
str -- the string representation of the root object
|
str -- the string representation of the root object
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
extra = Extra.allow
|
||||||
|
arbitrary_types_allowed = True
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
from typing import Dict, List
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
from specklepy.transports.abstract_transport import AbstractTransport
|
from specklepy.transports.abstract_transport import AbstractTransport
|
||||||
|
|
||||||
|
|
||||||
class MemoryTransport(AbstractTransport):
|
class MemoryTransport(AbstractTransport):
|
||||||
def __init__(self, name="Memory") -> None:
|
_name: str = "Memory"
|
||||||
super().__init__()
|
objects: dict = {}
|
||||||
self._name = name
|
saved_object_count: int = 0
|
||||||
self.objects = {}
|
|
||||||
self.saved_object_count = 0
|
|
||||||
|
|
||||||
@property
|
def __init__(self, name=None, **data: Any) -> None:
|
||||||
def name(self) -> str:
|
super().__init__(**data)
|
||||||
return self._name
|
if name:
|
||||||
|
self._name = name
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"MemoryTransport(objects: {len(self.objects)})"
|
return f"MemoryTransport(objects: {len(self.objects)})"
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ class BatchSender(object):
|
|||||||
stream_id,
|
stream_id,
|
||||||
token,
|
token,
|
||||||
max_batch_size_mb=1,
|
max_batch_size_mb=1,
|
||||||
max_batch_length=20000,
|
|
||||||
batch_buffer_length=10,
|
batch_buffer_length=10,
|
||||||
thread_count=4,
|
thread_count=4,
|
||||||
):
|
):
|
||||||
@@ -27,7 +26,6 @@ class BatchSender(object):
|
|||||||
self._token = token
|
self._token = token
|
||||||
|
|
||||||
self.max_size = int(max_batch_size_mb * 1000 * 1000)
|
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._batches = queue.Queue(batch_buffer_length)
|
||||||
self._crt_batch = []
|
self._crt_batch = []
|
||||||
self._crt_batch_size = 0
|
self._crt_batch_size = 0
|
||||||
@@ -41,11 +39,7 @@ class BatchSender(object):
|
|||||||
self._create_threads()
|
self._create_threads()
|
||||||
|
|
||||||
crt_obj_size = len(obj)
|
crt_obj_size = len(obj)
|
||||||
crt_batch_length = len(self._crt_batch)
|
if not self._crt_batch or self._crt_batch_size + crt_obj_size < self.max_size:
|
||||||
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.append((id, obj))
|
||||||
self._crt_batch_size += crt_obj_size
|
self._crt_batch_size += crt_obj_size
|
||||||
return
|
return
|
||||||
@@ -137,7 +131,7 @@ class BatchSender(object):
|
|||||||
raise SpeckleException(
|
raise SpeckleException(
|
||||||
message=(
|
message=(
|
||||||
"Could not save the object to the server - status code"
|
"Could not save the object to the server - status code"
|
||||||
f" {r.status_code} ({r.text[:1000]})"
|
f" {r.status_code}"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except json.JSONDecodeError as error:
|
except json.JSONDecodeError as error:
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import json
|
import json
|
||||||
from typing import Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
from warnings import warn
|
from warnings import warn
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from specklepy.core.api.client import SpeckleClient
|
from specklepy.api.client import SpeckleClient
|
||||||
from specklepy.core.api.credentials import Account, get_account_from_token
|
from specklepy.api.credentials import Account, get_account_from_token
|
||||||
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
|
||||||
from specklepy.transports.abstract_transport import AbstractTransport
|
from specklepy.transports.abstract_transport import AbstractTransport
|
||||||
|
|
||||||
@@ -45,6 +45,13 @@ class ServerTransport(AbstractTransport):
|
|||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
_name = "RemoteTransport"
|
||||||
|
url: Optional[str] = None
|
||||||
|
stream_id: Optional[str] = None
|
||||||
|
account: Optional[Account] = None
|
||||||
|
saved_obj_count: int = 0
|
||||||
|
session: Optional[requests.Session] = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
stream_id: str,
|
stream_id: str,
|
||||||
@@ -52,18 +59,15 @@ class ServerTransport(AbstractTransport):
|
|||||||
account: Optional[Account] = None,
|
account: Optional[Account] = None,
|
||||||
token: Optional[str] = None,
|
token: Optional[str] = None,
|
||||||
url: Optional[str] = None,
|
url: Optional[str] = None,
|
||||||
name: str = "RemoteTransport",
|
**data: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__()
|
super().__init__(**data)
|
||||||
if client is None and account is None and token is None and url is None:
|
if client is None and account is None and token is None and url is None:
|
||||||
raise SpeckleException(
|
raise SpeckleException(
|
||||||
"You must provide either a client or a token and url to construct a"
|
"You must provide either a client or a token and url to construct a"
|
||||||
" ServerTransport."
|
" ServerTransport."
|
||||||
)
|
)
|
||||||
|
|
||||||
self._name = name
|
|
||||||
self.account = None
|
|
||||||
self.saved_obj_count = 0
|
|
||||||
if account:
|
if account:
|
||||||
self.account = account
|
self.account = account
|
||||||
url = account.serverInfo.url
|
url = account.serverInfo.url
|
||||||
@@ -73,7 +77,7 @@ class ServerTransport(AbstractTransport):
|
|||||||
warn(
|
warn(
|
||||||
SpeckleWarning(
|
SpeckleWarning(
|
||||||
"Unauthenticated Speckle Client provided to Server Transport"
|
"Unauthenticated Speckle Client provided to Server Transport"
|
||||||
f" for {url}. Receiving from private streams will fail."
|
f" for {self.url}. Receiving from private streams will fail."
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@@ -84,22 +88,14 @@ class ServerTransport(AbstractTransport):
|
|||||||
self.stream_id = stream_id
|
self.stream_id = stream_id
|
||||||
self.url = url
|
self.url = url
|
||||||
|
|
||||||
|
self._batch_sender = BatchSender(
|
||||||
|
self.url, self.stream_id, self.account.token, max_batch_size_mb=1
|
||||||
|
)
|
||||||
|
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
|
self.session.headers.update(
|
||||||
if self.account is not None:
|
{"Authorization": f"Bearer {self.account.token}", "Accept": "text/plain"}
|
||||||
self._batch_sender = BatchSender(
|
)
|
||||||
self.url, self.stream_id, self.account.token, max_batch_size_mb=1
|
|
||||||
)
|
|
||||||
self.session.headers.update(
|
|
||||||
{
|
|
||||||
"Authorization": f"Bearer {self.account.token}",
|
|
||||||
"Accept": "text/plain",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
def begin_write(self) -> None:
|
def begin_write(self) -> None:
|
||||||
self.saved_obj_count = 0
|
self.saved_obj_count = 0
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
from typing import Dict, List, Optional, Tuple
|
from typing import Any, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
from specklepy.core.helpers import speckle_path_provider
|
from specklepy.core.helpers import speckle_path_provider
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
from specklepy.logging.exceptions import SpeckleException
|
||||||
@@ -9,22 +9,31 @@ from specklepy.transports.abstract_transport import AbstractTransport
|
|||||||
|
|
||||||
|
|
||||||
class SQLiteTransport(AbstractTransport):
|
class SQLiteTransport(AbstractTransport):
|
||||||
|
_name = "SQLite"
|
||||||
|
_base_path: Optional[str] = None
|
||||||
|
_root_path: Optional[str] = None
|
||||||
|
__connection: Optional[sqlite3.Connection] = None
|
||||||
|
app_name: str = ""
|
||||||
|
scope: str = ""
|
||||||
|
saved_obj_count: int = 0
|
||||||
|
max_size: Optional[int] = None
|
||||||
|
_current_batch: Optional[List[Tuple[str, str]]] = None
|
||||||
|
_current_batch_size: Optional[int] = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
base_path: Optional[str] = None,
|
base_path: Optional[str] = None,
|
||||||
app_name: Optional[str] = None,
|
app_name: Optional[str] = None,
|
||||||
scope: Optional[str] = None,
|
scope: Optional[str] = None,
|
||||||
max_batch_size_mb: float = 10.0,
|
max_batch_size_mb: float = 10.0,
|
||||||
name: str = "SQLite",
|
**data: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__()
|
super().__init__(**data)
|
||||||
self._name = name
|
|
||||||
self.app_name = app_name or "Speckle"
|
self.app_name = app_name or "Speckle"
|
||||||
self.scope = scope or "Objects"
|
self.scope = scope or "Objects"
|
||||||
self._base_path = base_path or self.get_base_path(self.app_name)
|
self._base_path = base_path or self.get_base_path(self.app_name)
|
||||||
self.max_size = int(max_batch_size_mb * 1000 * 1000)
|
self.max_size = int(max_batch_size_mb * 1000 * 1000)
|
||||||
self.saved_obj_count = 0
|
self._current_batch = []
|
||||||
self._current_batch: List[Tuple[str, str]] = []
|
|
||||||
self._current_batch_size = 0
|
self._current_batch_size = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -45,12 +54,24 @@ class SQLiteTransport(AbstractTransport):
|
|||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"SQLiteTransport(app: '{self.app_name}', scope: '{self.scope}')"
|
return f"SQLiteTransport(app: '{self.app_name}', scope: '{self.scope}')"
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_base_path(app_name):
|
def get_base_path(app_name):
|
||||||
|
# # from appdirs https://github.com/ActiveState/appdirs/blob/master/appdirs.py
|
||||||
|
# # default mac path is not the one we use (we use unix path), so using special case for this
|
||||||
|
# system = sys.platform
|
||||||
|
# if system.startswith("java"):
|
||||||
|
# import platform
|
||||||
|
|
||||||
|
# os_name = platform.java_ver()[3][0]
|
||||||
|
# if os_name.startswith("Mac"):
|
||||||
|
# system = "darwin"
|
||||||
|
|
||||||
|
# if system != "darwin":
|
||||||
|
# return user_data_dir(appname=app_name, appauthor=False, roaming=True)
|
||||||
|
|
||||||
|
# path = os.path.expanduser("~/.config/")
|
||||||
|
# return os.path.join(path, app_name)
|
||||||
|
|
||||||
return str(
|
return str(
|
||||||
speckle_path_provider.user_application_data_path().joinpath(app_name)
|
speckle_path_provider.user_application_data_path().joinpath(app_name)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,259 +0,0 @@
|
|||||||
"""Run integration tests with a speckle server."""
|
|
||||||
import os
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Dict
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
from gql import gql
|
|
||||||
|
|
||||||
from speckle_automate import (
|
|
||||||
AutomationContext,
|
|
||||||
AutomationRunData,
|
|
||||||
AutomationStatus,
|
|
||||||
run_function,
|
|
||||||
)
|
|
||||||
from speckle_automate.fixtures import (
|
|
||||||
create_test_automation_run_data,
|
|
||||||
crypto_random_string,
|
|
||||||
)
|
|
||||||
from speckle_automate.schema import AutomateBase
|
|
||||||
from specklepy.api.client import SpeckleClient
|
|
||||||
from specklepy.objects.base import Base
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def speckle_token(user_dict: Dict[str, str]) -> str:
|
|
||||||
"""Provide a speckle token for the test suite."""
|
|
||||||
return user_dict["token"]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def speckle_server_url(host: str) -> str:
|
|
||||||
"""Provide a speckle server url for the test suite, default to localhost."""
|
|
||||||
return f"http://{host}"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def test_client(speckle_server_url: str, speckle_token: str) -> SpeckleClient:
|
|
||||||
"""Initialize a SpeckleClient for testing."""
|
|
||||||
test_client = SpeckleClient(speckle_server_url, use_ssl=False)
|
|
||||||
test_client.authenticate_with_token(speckle_token)
|
|
||||||
return test_client
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def automation_run_data(
|
|
||||||
test_client: SpeckleClient, speckle_server_url: str
|
|
||||||
) -> AutomationRunData:
|
|
||||||
"""TODO: Set up a test automation for integration testing"""
|
|
||||||
project_id = crypto_random_string(10)
|
|
||||||
test_automation_id = crypto_random_string(10)
|
|
||||||
|
|
||||||
return create_test_automation_run_data(
|
|
||||||
test_client, speckle_server_url, project_id, test_automation_id
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def automation_context(
|
|
||||||
automation_run_data: AutomationRunData, speckle_token: str
|
|
||||||
) -> AutomationContext:
|
|
||||||
"""Set up the run context."""
|
|
||||||
return AutomationContext.initialize(automation_run_data, speckle_token)
|
|
||||||
|
|
||||||
|
|
||||||
def get_automation_status(
|
|
||||||
project_id: str,
|
|
||||||
model_id: str,
|
|
||||||
speckle_client: SpeckleClient,
|
|
||||||
):
|
|
||||||
query = gql(
|
|
||||||
"""
|
|
||||||
query AutomationRuns(
|
|
||||||
$projectId: String!
|
|
||||||
$modelId: String!
|
|
||||||
)
|
|
||||||
{
|
|
||||||
project(id: $projectId) {
|
|
||||||
model(id: $modelId) {
|
|
||||||
automationStatus {
|
|
||||||
id
|
|
||||||
status
|
|
||||||
statusMessage
|
|
||||||
automationRuns {
|
|
||||||
id
|
|
||||||
automationId
|
|
||||||
versionId
|
|
||||||
createdAt
|
|
||||||
updatedAt
|
|
||||||
status
|
|
||||||
functionRuns {
|
|
||||||
id
|
|
||||||
functionId
|
|
||||||
elapsed
|
|
||||||
status
|
|
||||||
contextView
|
|
||||||
statusMessage
|
|
||||||
results
|
|
||||||
resultVersions {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
params = {
|
|
||||||
"projectId": project_id,
|
|
||||||
"modelId": model_id,
|
|
||||||
}
|
|
||||||
response = speckle_client.httpclient.execute(query, params)
|
|
||||||
return response["project"]["model"]["automationStatus"]
|
|
||||||
|
|
||||||
|
|
||||||
class FunctionInputs(AutomateBase):
|
|
||||||
forbidden_speckle_type: str
|
|
||||||
|
|
||||||
|
|
||||||
def automate_function(
|
|
||||||
automation_context: AutomationContext,
|
|
||||||
function_inputs: FunctionInputs,
|
|
||||||
) -> None:
|
|
||||||
"""Hey, trying the automate sdk experience here."""
|
|
||||||
version_root_object = automation_context.receive_version()
|
|
||||||
|
|
||||||
count = 0
|
|
||||||
if version_root_object.speckle_type == function_inputs.forbidden_speckle_type:
|
|
||||||
if not version_root_object.id:
|
|
||||||
raise ValueError("Cannot operate on objects without their id's.")
|
|
||||||
automation_context.attach_error_to_objects(
|
|
||||||
"Forbidden speckle_type",
|
|
||||||
version_root_object.id,
|
|
||||||
"This project should not contain the type: "
|
|
||||||
f"{function_inputs.forbidden_speckle_type}",
|
|
||||||
)
|
|
||||||
count += 1
|
|
||||||
|
|
||||||
if count > 0:
|
|
||||||
automation_context.mark_run_failed(
|
|
||||||
"Automation failed: "
|
|
||||||
f"Found {count} object that have a forbidden speckle type: "
|
|
||||||
f"{function_inputs.forbidden_speckle_type}"
|
|
||||||
)
|
|
||||||
|
|
||||||
else:
|
|
||||||
automation_context.mark_run_success("No forbidden types found.")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip(
|
|
||||||
"currently the function run cannot be integration tested with the server"
|
|
||||||
)
|
|
||||||
def test_function_run(automation_context: AutomationContext) -> None:
|
|
||||||
"""Run an integration test for the automate function."""
|
|
||||||
automation_context = run_function(
|
|
||||||
automation_context,
|
|
||||||
automate_function,
|
|
||||||
FunctionInputs(forbidden_speckle_type="Base"),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert automation_context.run_status == AutomationStatus.FAILED
|
|
||||||
status = get_automation_status(
|
|
||||||
automation_context.automation_run_data.project_id,
|
|
||||||
automation_context.automation_run_data.model_id,
|
|
||||||
automation_context.speckle_client,
|
|
||||||
)
|
|
||||||
assert status["status"] == automation_context.run_status
|
|
||||||
status_message = status["automationRuns"][0]["functionRuns"][0]["statusMessage"]
|
|
||||||
assert status_message == automation_context.status_message
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def test_file_path():
|
|
||||||
path = Path(f"./{crypto_random_string(10)}").resolve()
|
|
||||||
yield path
|
|
||||||
os.remove(path)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip(
|
|
||||||
"currently the function run cannot be integration tested with the server"
|
|
||||||
)
|
|
||||||
def test_file_uploads(
|
|
||||||
automation_run_data: AutomationRunData, speckle_token: str, test_file_path: Path
|
|
||||||
):
|
|
||||||
"""Test file store capabilities of the automate sdk."""
|
|
||||||
automation_context = AutomationContext.initialize(
|
|
||||||
automation_run_data, speckle_token
|
|
||||||
)
|
|
||||||
|
|
||||||
test_file_path.write_text("foobar")
|
|
||||||
|
|
||||||
automation_context.store_file_result(test_file_path)
|
|
||||||
|
|
||||||
assert len(automation_context._automation_result.blobs) == 1
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip(
|
|
||||||
"currently the function run cannot be integration tested with the server"
|
|
||||||
)
|
|
||||||
def test_create_version_in_project_raises_error_for_same_model(
|
|
||||||
automation_context: AutomationContext,
|
|
||||||
) -> None:
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
automation_context.create_new_version_in_project(
|
|
||||||
Base(), automation_context.automation_run_data.branch_name
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip(
|
|
||||||
"currently the function run cannot be integration tested with the server"
|
|
||||||
)
|
|
||||||
def test_create_version_in_project(
|
|
||||||
automation_context: AutomationContext,
|
|
||||||
) -> None:
|
|
||||||
root_object = Base()
|
|
||||||
root_object.foo = "bar"
|
|
||||||
model_id, version_id = automation_context.create_new_version_in_project(
|
|
||||||
root_object, "foobar"
|
|
||||||
)
|
|
||||||
|
|
||||||
assert model_id is not None
|
|
||||||
assert version_id is not None
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip(
|
|
||||||
"currently the function run cannot be integration tested with the server"
|
|
||||||
)
|
|
||||||
def test_set_context_view(automation_context: AutomationContext) -> None:
|
|
||||||
automation_context.set_context_view()
|
|
||||||
|
|
||||||
assert automation_context.context_view is not None
|
|
||||||
assert automation_context.context_view.endswith(
|
|
||||||
f"models/{automation_context.automation_run_data.model_id}@{automation_context.automation_run_data.version_id}"
|
|
||||||
)
|
|
||||||
|
|
||||||
automation_context.report_run_status()
|
|
||||||
|
|
||||||
automation_context._automation_result.result_view = None
|
|
||||||
|
|
||||||
dummy_context = "foo@bar"
|
|
||||||
automation_context.set_context_view([dummy_context])
|
|
||||||
|
|
||||||
assert automation_context.context_view is not None
|
|
||||||
assert automation_context.context_view.endswith(
|
|
||||||
f"models/{automation_context.automation_run_data.model_id}@{automation_context.automation_run_data.version_id},{dummy_context}"
|
|
||||||
)
|
|
||||||
automation_context.report_run_status()
|
|
||||||
|
|
||||||
automation_context._automation_result.result_view = None
|
|
||||||
|
|
||||||
dummy_context = "foo@baz"
|
|
||||||
automation_context.set_context_view(
|
|
||||||
[dummy_context], include_source_model_version=False
|
|
||||||
)
|
|
||||||
|
|
||||||
assert automation_context.context_view is not None
|
|
||||||
assert automation_context.context_view.endswith(f"models/{dummy_context}")
|
|
||||||
automation_context.report_run_status()
|
|
||||||
@@ -23,7 +23,7 @@ def host():
|
|||||||
def seed_user(host):
|
def seed_user(host):
|
||||||
seed = uuid.uuid4().hex
|
seed = uuid.uuid4().hex
|
||||||
user_dict = {
|
user_dict = {
|
||||||
"email": f"{seed[0:7]}@example.org",
|
"email": f"{seed[0:7]}@spockle.com",
|
||||||
"password": "$uper$3cr3tP@ss",
|
"password": "$uper$3cr3tP@ss",
|
||||||
"name": f"{seed[0:7]} Name",
|
"name": f"{seed[0:7]} Name",
|
||||||
"company": "test spockle",
|
"company": "test spockle",
|
||||||
@@ -39,7 +39,7 @@ class TestUser:
|
|||||||
assert my_activity.totalCount
|
assert my_activity.totalCount
|
||||||
assert isinstance(their_activity, ActivityCollection)
|
assert isinstance(their_activity, ActivityCollection)
|
||||||
|
|
||||||
older_activity = client.active_user.activity(before=my_activity.items[0].time)
|
older_activity = client.user.activity(before=my_activity.items[0].time)
|
||||||
|
|
||||||
assert isinstance(older_activity, ActivityCollection)
|
assert isinstance(older_activity, ActivityCollection)
|
||||||
assert older_activity.totalCount
|
assert older_activity.totalCount
|
||||||
@@ -17,7 +17,7 @@ class TestSerialization:
|
|||||||
deserialized = operations.deserialize(serialized)
|
deserialized = operations.deserialize(serialized)
|
||||||
|
|
||||||
assert base.get_id() == deserialized.get_id()
|
assert base.get_id() == deserialized.get_id()
|
||||||
assert base.units == "millimetres"
|
assert base.units == "mm"
|
||||||
assert isinstance(base.test_bases[0], Base)
|
assert isinstance(base.test_bases[0], Base)
|
||||||
assert base["@revit_thing"].speckle_type == "SpecialRevitFamily"
|
assert base["@revit_thing"].speckle_type == "SpecialRevitFamily"
|
||||||
assert base["@detach"].name == deserialized["@detach"].name
|
assert base["@detach"].name == deserialized["@detach"].name
|
||||||
@@ -18,7 +18,6 @@ class TestServer:
|
|||||||
server = client.server.get()
|
server = client.server.get()
|
||||||
|
|
||||||
assert isinstance(server, ServerInfo)
|
assert isinstance(server, ServerInfo)
|
||||||
assert isinstance(server.frontend2, bool)
|
|
||||||
|
|
||||||
def test_server_version(self, client: SpeckleClient):
|
def test_server_version(self, client: SpeckleClient):
|
||||||
version = client.server.version()
|
version = client.server.version()
|
||||||
@@ -8,7 +8,11 @@ from specklepy.api.models import (
|
|||||||
Stream,
|
Stream,
|
||||||
User,
|
User,
|
||||||
)
|
)
|
||||||
from specklepy.logging.exceptions import GraphQLException, SpeckleException
|
from specklepy.logging.exceptions import (
|
||||||
|
GraphQLException,
|
||||||
|
SpeckleException,
|
||||||
|
UnsupportedException,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.run(order=3)
|
@pytest.mark.run(order=3)
|
||||||
@@ -33,7 +37,7 @@ class TestStream:
|
|||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def second_user(self, second_client: SpeckleClient):
|
def second_user(self, second_client: SpeckleClient):
|
||||||
return second_client.active_user.get()
|
return second_client.user.get()
|
||||||
|
|
||||||
def test_stream_create(self, client, stream, updated_stream):
|
def test_stream_create(self, client, stream, updated_stream):
|
||||||
stream.id = updated_stream.id = client.stream.create(
|
stream.id = updated_stream.id = client.stream.create(
|
||||||
@@ -44,14 +48,6 @@ class TestStream:
|
|||||||
|
|
||||||
assert isinstance(stream.id, str)
|
assert isinstance(stream.id, str)
|
||||||
|
|
||||||
def test_stream_create_short_name(self, client, stream, updated_stream):
|
|
||||||
new_stream_id = client.stream.create(
|
|
||||||
name="x",
|
|
||||||
description=stream.description,
|
|
||||||
is_public=stream.isPublic,
|
|
||||||
)
|
|
||||||
assert isinstance(new_stream_id, SpeckleException)
|
|
||||||
|
|
||||||
def test_stream_get(self, client, stream):
|
def test_stream_get(self, client, stream):
|
||||||
fetched_stream = client.stream.get(stream.id)
|
fetched_stream = client.stream.get(stream.id)
|
||||||
|
|
||||||
@@ -97,6 +93,15 @@ class TestStream:
|
|||||||
assert isinstance(favorited, Stream)
|
assert isinstance(favorited, Stream)
|
||||||
assert unfavorited.favoritedDate is None
|
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(
|
def test_stream_invite(
|
||||||
self, client: SpeckleClient, stream: Stream, second_user_dict: dict
|
self, client: SpeckleClient, stream: Stream, second_user_dict: dict
|
||||||
):
|
):
|
||||||
@@ -117,18 +122,18 @@ class TestStream:
|
|||||||
self, second_client: SpeckleClient, stream: Stream
|
self, second_client: SpeckleClient, stream: Stream
|
||||||
):
|
):
|
||||||
# NOTE: these are user queries, but testing here to contain the flow
|
# NOTE: these are user queries, but testing here to contain the flow
|
||||||
invites = second_client.active_user.get_all_pending_invites()
|
invites = second_client.user.get_all_pending_invites()
|
||||||
|
|
||||||
assert isinstance(invites, list)
|
assert isinstance(invites, list)
|
||||||
assert isinstance(invites[0], PendingStreamCollaborator)
|
assert isinstance(invites[0], PendingStreamCollaborator)
|
||||||
assert len(invites) == 1
|
assert len(invites) == 1
|
||||||
|
|
||||||
invite = second_client.active_user.get_pending_invite(stream_id=stream.id)
|
invite = second_client.user.get_pending_invite(stream_id=stream.id)
|
||||||
assert isinstance(invite, PendingStreamCollaborator)
|
assert isinstance(invite, PendingStreamCollaborator)
|
||||||
|
|
||||||
def test_stream_invite_use(self, second_client: SpeckleClient, stream: Stream):
|
def test_stream_invite_use(self, second_client: SpeckleClient, stream: Stream):
|
||||||
invite: PendingStreamCollaborator = (
|
invite: PendingStreamCollaborator = (
|
||||||
second_client.active_user.get_all_pending_invites()[0]
|
second_client.user.get_all_pending_invites()[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
accepted = second_client.stream.invite_use(
|
accepted = second_client.stream.invite_use(
|
||||||
@@ -183,7 +188,7 @@ class TestStream:
|
|||||||
# NOTE: only works for server admins
|
# NOTE: only works for server admins
|
||||||
# invited = client.stream.invite_batch(
|
# invited = client.stream.invite_batch(
|
||||||
# stream_id=stream.id,
|
# stream_id=stream.id,
|
||||||
# emails=["userA@example.org", "userB@example.org"],
|
# emails=["userA@speckle.xyz", "userB@speckle.xyz"],
|
||||||
# user_ids=[second_user.id],
|
# user_ids=[second_user.id],
|
||||||
# message="yeehaw 🤠",
|
# message="yeehaw 🤠",
|
||||||
# )
|
# )
|
||||||
@@ -192,7 +197,7 @@ class TestStream:
|
|||||||
|
|
||||||
# invited_only_email = client.stream.invite_batch(
|
# invited_only_email = client.stream.invite_batch(
|
||||||
# stream_id=stream.id,
|
# stream_id=stream.id,
|
||||||
# emails=["userC@example.org"],
|
# emails=["userC@speckle.xyz"],
|
||||||
# message="yeehaw 🤠",
|
# message="yeehaw 🤠",
|
||||||
# )
|
# )
|
||||||
|
|
||||||
@@ -2,13 +2,11 @@ import json
|
|||||||
import tempfile
|
import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Iterable
|
from typing import Iterable
|
||||||
from urllib.parse import unquote
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from specklepy.api.wrapper import StreamWrapper
|
from specklepy.api.wrapper import StreamWrapper
|
||||||
from specklepy.core.helpers import speckle_path_provider
|
from specklepy.core.helpers import speckle_path_provider
|
||||||
from specklepy.logging.exceptions import SpeckleException
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module", autouse=True)
|
@pytest.fixture(scope="module", autouse=True)
|
||||||
@@ -31,22 +29,6 @@ def user_path() -> Iterable[Path]:
|
|||||||
speckle_path_provider.override_application_data_path(None)
|
speckle_path_provider.override_application_data_path(None)
|
||||||
|
|
||||||
|
|
||||||
def test_parse_empty():
|
|
||||||
try:
|
|
||||||
StreamWrapper("https://testing.speckle.dev/streams")
|
|
||||||
assert False
|
|
||||||
except SpeckleException:
|
|
||||||
assert True
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_empty_fe2():
|
|
||||||
try:
|
|
||||||
StreamWrapper("https://latest.speckle.systems/projects")
|
|
||||||
assert False
|
|
||||||
except SpeckleException:
|
|
||||||
assert True
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_stream():
|
def test_parse_stream():
|
||||||
wrap = StreamWrapper("https://testing.speckle.dev/streams/a75ab4f10f")
|
wrap = StreamWrapper("https://testing.speckle.dev/streams/a75ab4f10f")
|
||||||
assert wrap.type == "stream"
|
assert wrap.type == "stream"
|
||||||
@@ -100,20 +82,16 @@ def test_parse_globals_as_commit():
|
|||||||
|
|
||||||
|
|
||||||
#! NOTE: the following three tests may not pass locally
|
#! NOTE: the following three tests may not pass locally
|
||||||
# if you have a `app.speckle.systems` account in manager
|
# if you have a `speckle.xyz` account in manager
|
||||||
def test_get_client_without_auth():
|
def test_get_client_without_auth():
|
||||||
wrap = StreamWrapper(
|
wrap = StreamWrapper("https://speckle.xyz/streams/4c3ce1459c/commits/8b9b831792")
|
||||||
"https://app.speckle.systems/streams/4c3ce1459c/commits/8b9b831792"
|
|
||||||
)
|
|
||||||
client = wrap.get_client()
|
client = wrap.get_client()
|
||||||
|
|
||||||
assert client is not None
|
assert client is not None
|
||||||
|
|
||||||
|
|
||||||
def test_get_new_client_with_token(user_path):
|
def test_get_new_client_with_token(user_path):
|
||||||
wrap = StreamWrapper(
|
wrap = StreamWrapper("https://speckle.xyz/streams/4c3ce1459c/commits/8b9b831792")
|
||||||
"https://app.speckle.systems/streams/4c3ce1459c/commits/8b9b831792"
|
|
||||||
)
|
|
||||||
client = wrap.get_client()
|
client = wrap.get_client()
|
||||||
client = wrap.get_client(token="super-secret-token")
|
client = wrap.get_client(token="super-secret-token")
|
||||||
|
|
||||||
@@ -121,9 +99,7 @@ def test_get_new_client_with_token(user_path):
|
|||||||
|
|
||||||
|
|
||||||
def test_get_transport_with_token():
|
def test_get_transport_with_token():
|
||||||
wrap = StreamWrapper(
|
wrap = StreamWrapper("https://speckle.xyz/streams/4c3ce1459c/commits/8b9b831792")
|
||||||
"https://app.speckle.systems/streams/4c3ce1459c/commits/8b9b831792"
|
|
||||||
)
|
|
||||||
client = wrap.get_client()
|
client = wrap.get_client()
|
||||||
assert not client.account.token # unauthenticated bc no local accounts
|
assert not client.account.token # unauthenticated bc no local accounts
|
||||||
|
|
||||||
@@ -150,72 +126,3 @@ def test_wrapper_url_match(user_path) -> None:
|
|||||||
account = wrap.get_account()
|
account = wrap.get_account()
|
||||||
|
|
||||||
assert account.userInfo.email is None
|
assert account.userInfo.email is None
|
||||||
|
|
||||||
|
|
||||||
def test_parse_project():
|
|
||||||
wrap = StreamWrapper("https://latest.speckle.systems/projects/843d07eb10")
|
|
||||||
assert wrap.type == "stream"
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_model():
|
|
||||||
wrap = StreamWrapper(
|
|
||||||
"https://latest.speckle.systems/projects/843d07eb10/models/d9eb4918c8"
|
|
||||||
)
|
|
||||||
|
|
||||||
assert wrap.branch_name == "building wrapper"
|
|
||||||
assert wrap.type == "branch"
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_federated_model():
|
|
||||||
try:
|
|
||||||
StreamWrapper("https://latest.speckle.systems/projects/843d07eb10/models/$main")
|
|
||||||
assert False
|
|
||||||
except SpeckleException:
|
|
||||||
assert True
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_multi_model():
|
|
||||||
try:
|
|
||||||
StreamWrapper(
|
|
||||||
"https://latest.speckle.systems/projects/2099ac4b5f/models/1870f279e3,a9cfdddc79"
|
|
||||||
)
|
|
||||||
assert False
|
|
||||||
except SpeckleException:
|
|
||||||
assert True
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_object_fe2():
|
|
||||||
wrap = StreamWrapper(
|
|
||||||
"https://latest.speckle.systems/projects/24c3741255/models/b48d1b10f5a732f4ca4144286391282c"
|
|
||||||
)
|
|
||||||
assert wrap.type == "object"
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_version():
|
|
||||||
wrap = StreamWrapper(
|
|
||||||
"https://latest.speckle.systems/projects/843d07eb10/models/4e7345c838@c42d5cbac1"
|
|
||||||
)
|
|
||||||
wrap_quoted = StreamWrapper(
|
|
||||||
"https://latest.speckle.systems/projects/843d07eb10/models/4e7345c838%40c42d5cbac1"
|
|
||||||
)
|
|
||||||
assert wrap.type == "commit"
|
|
||||||
assert wrap_quoted.type == "commit"
|
|
||||||
|
|
||||||
|
|
||||||
def test_to_string():
|
|
||||||
urls = [
|
|
||||||
"https://testing.speckle.dev/streams/a75ab4f10f",
|
|
||||||
"https://testing.speckle.dev/streams/4c3ce1459c/branches/%F0%9F%8D%95%E2%AC%85%F0%9F%8C%9F%20you%20wat%3F",
|
|
||||||
"https://testing.speckle.dev/streams/0c6ad366c4/globals",
|
|
||||||
"https://testing.speckle.dev/streams/0c6ad366c4/globals/abd3787893",
|
|
||||||
"https://testing.speckle.dev/streams/4c3ce1459c/commits/8b9b831792",
|
|
||||||
"https://testing.speckle.dev/streams/a75ab4f10f/objects/5530363e6d51c904903dafc3ea1d2ec6",
|
|
||||||
"https://latest.speckle.systems/projects/843d07eb10",
|
|
||||||
"https://latest.speckle.systems/projects/843d07eb10/models/4e7345c838",
|
|
||||||
"https://latest.speckle.systems/projects/843d07eb10/models/4e7345c838@c42d5cbac1",
|
|
||||||
"https://latest.speckle.systems/projects/843d07eb10/models/4e7345c838%40c42d5cbac1",
|
|
||||||
"https://latest.speckle.systems/projects/24c3741255/models/b48d1b10f5a732f4ca4144286391282c",
|
|
||||||
]
|
|
||||||
for url in urls:
|
|
||||||
wrap = StreamWrapper(url)
|
|
||||||
assert unquote(wrap.to_string()) == unquote(url)
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
import os
|
|
||||||
import uuid
|
|
||||||
from typing import List, Optional, Tuple
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from specklepy.core.api.credentials import Account, UserInfo, get_accounts_for_server
|
|
||||||
from specklepy.core.api.models import ServerInfo, ServerMigration
|
|
||||||
from specklepy.core.helpers import speckle_path_provider
|
|
||||||
|
|
||||||
|
|
||||||
def _create_account(
|
|
||||||
id: str, url: str, movedFrom: Optional[str], movedTo: Optional[str]
|
|
||||||
) -> Account:
|
|
||||||
return Account(
|
|
||||||
id=uuid.uuid4().hex[:6].lower(),
|
|
||||||
token="myToken",
|
|
||||||
serverInfo=ServerInfo(
|
|
||||||
url=url,
|
|
||||||
name="myServer",
|
|
||||||
migration=ServerMigration(movedTo=movedTo, movedFrom=movedFrom),
|
|
||||||
),
|
|
||||||
userInfo=UserInfo(id=id),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _test_cases() -> List[Tuple[List[Account], str, List[Account]]]:
|
|
||||||
user_id_1 = uuid.uuid4().hex[:6].lower()
|
|
||||||
user_id_2 = uuid.uuid4().hex[:6].lower()
|
|
||||||
old = _create_account(
|
|
||||||
user_id_1, "https://old.example.com", None, "https://new.example.com"
|
|
||||||
)
|
|
||||||
new = _create_account(
|
|
||||||
user_id_1, "https://new.example.com", "https://old.example.com", None
|
|
||||||
)
|
|
||||||
other = _create_account(user_id_2, "https://other.example.com", None, None)
|
|
||||||
|
|
||||||
given_accounts = [old, new, other]
|
|
||||||
reversed = [other, new, old]
|
|
||||||
|
|
||||||
return [
|
|
||||||
(given_accounts, "https://old.example.com", [new]),
|
|
||||||
(given_accounts, "https://new.example.com", [new]),
|
|
||||||
(reversed, "https://old.example.com", [new]),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def _clean_accounts(accounts: List[Account]) -> None:
|
|
||||||
json_accounts = speckle_path_provider.accounts_folder_path()
|
|
||||||
|
|
||||||
for acc in accounts:
|
|
||||||
# deleting acc json file in json_accounts path
|
|
||||||
os.remove(os.path.join(json_accounts, f"{acc.id}.json"))
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def _add_accounts(accounts: List[Account]) -> None:
|
|
||||||
json_accounts = speckle_path_provider.accounts_folder_path()
|
|
||||||
|
|
||||||
for acc in accounts:
|
|
||||||
data = Account.model_dump_json(acc)
|
|
||||||
with open(os.path.join(json_accounts, f"{acc.id}.json"), "w") as f:
|
|
||||||
f.write(data)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("accounts, requested_url, expected", _test_cases())
|
|
||||||
def test_server_migration(
|
|
||||||
accounts: List[Account], requested_url: str, expected: List[Account]
|
|
||||||
) -> None:
|
|
||||||
_add_accounts(accounts)
|
|
||||||
try:
|
|
||||||
res = get_accounts_for_server(urlparse(requested_url).netloc)
|
|
||||||
assert res == expected
|
|
||||||
|
|
||||||
finally:
|
|
||||||
_clean_accounts(accounts)
|
|
||||||
@@ -85,15 +85,14 @@ def test_speckle_type_cannot_be_set(base: Base) -> None:
|
|||||||
|
|
||||||
def test_setting_units():
|
def test_setting_units():
|
||||||
b = Base(units="foot")
|
b = Base(units="foot")
|
||||||
assert b.units == "foot"
|
assert b.units == "ft"
|
||||||
|
|
||||||
# with pytest.raises(SpeckleInvalidUnitException):
|
with pytest.raises(SpeckleInvalidUnitException):
|
||||||
b.units = "big"
|
b.units = "big"
|
||||||
assert b.units == "big"
|
|
||||||
|
|
||||||
with pytest.raises(SpeckleInvalidUnitException):
|
with pytest.raises(SpeckleInvalidUnitException):
|
||||||
b.units = 7 # invalid args are skipped
|
b.units = 7 # invalid args are skipped
|
||||||
assert b.units == "big"
|
assert b.units == "ft"
|
||||||
|
|
||||||
b.units = None # None should be a valid arg
|
b.units = None # None should be a valid arg
|
||||||
assert b.units is None
|
assert b.units is None
|
||||||
|
|||||||
@@ -388,9 +388,9 @@ def test_brep_curve3d_values_serialization(curve, polyline, circle):
|
|||||||
def test_brep_vertices_values_serialization():
|
def test_brep_vertices_values_serialization():
|
||||||
brep = Brep()
|
brep = Brep()
|
||||||
brep.VerticesValue = [1, 1, 1, 1, 2, 2, 2, 3, 3, 3]
|
brep.VerticesValue = [1, 1, 1, 1, 2, 2, 2, 3, 3, 3]
|
||||||
assert brep.Vertices[0].get_id() == Point(x=1, y=1, z=1, units=Units.mm).get_id()
|
assert brep.Vertices[0].get_id() == Point(x=1, y=1, z=1, _units=Units.mm).get_id()
|
||||||
assert brep.Vertices[1].get_id() == Point(x=2, y=2, z=2, units=Units.mm).get_id()
|
assert brep.Vertices[1].get_id() == Point(x=2, y=2, z=2, _units=Units.mm).get_id()
|
||||||
assert brep.Vertices[2].get_id() == Point(x=3, y=3, z=3, units=Units.mm).get_id()
|
assert brep.Vertices[2].get_id() == Point(x=3, y=3, z=3, _units=Units.mm).get_id()
|
||||||
|
|
||||||
|
|
||||||
def test_trims_value_serialization():
|
def test_trims_value_serialization():
|
||||||
|
|||||||
@@ -1,105 +0,0 @@
|
|||||||
from dataclasses import dataclass
|
|
||||||
from typing import Dict, List, Optional
|
|
||||||
from unittest import TestCase
|
|
||||||
|
|
||||||
from specklepy.objects import Base
|
|
||||||
from specklepy.objects.graph_traversal.traversal import GraphTraversal, TraversalRule
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass()
|
|
||||||
class TraversalMock(Base):
|
|
||||||
child: Optional[Base]
|
|
||||||
list_children: List[Base]
|
|
||||||
dict_children: Dict[str, Base]
|
|
||||||
|
|
||||||
|
|
||||||
class GraphTraversalTests(TestCase):
|
|
||||||
def test_traverse_list_members(self):
|
|
||||||
traverse_lists_rule = TraversalRule(
|
|
||||||
[lambda _: True],
|
|
||||||
lambda x: [
|
|
||||||
item
|
|
||||||
for item in x.get_member_names()
|
|
||||||
if isinstance(getattr(x, item, None), list)
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
expected_traverse = Base()
|
|
||||||
expected_traverse.id = "List Member"
|
|
||||||
|
|
||||||
expected_ignore = Base()
|
|
||||||
expected_ignore.id = "Not List Member"
|
|
||||||
|
|
||||||
test_case = TraversalMock(
|
|
||||||
list_children=[expected_traverse],
|
|
||||||
dict_children={"myprop": expected_ignore},
|
|
||||||
child=expected_ignore,
|
|
||||||
)
|
|
||||||
|
|
||||||
ret = [
|
|
||||||
context.current
|
|
||||||
for context in GraphTraversal([traverse_lists_rule]).traverse(test_case)
|
|
||||||
]
|
|
||||||
|
|
||||||
self.assertCountEqual(ret, [test_case, expected_traverse])
|
|
||||||
self.assertNotIn(expected_ignore, ret)
|
|
||||||
self.assertEqual(len(ret), 2)
|
|
||||||
|
|
||||||
def test_traverse_dict_members(self):
|
|
||||||
traverse_lists_rule = TraversalRule(
|
|
||||||
[lambda _: True],
|
|
||||||
lambda x: [
|
|
||||||
item
|
|
||||||
for item in x.get_member_names()
|
|
||||||
if isinstance(getattr(x, item, None), dict)
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
expected_traverse = Base()
|
|
||||||
expected_traverse.id = "Dict Member"
|
|
||||||
|
|
||||||
expected_ignore = Base()
|
|
||||||
expected_ignore.id = "Not Dict Member"
|
|
||||||
|
|
||||||
test_case = TraversalMock(
|
|
||||||
list_children=[expected_ignore],
|
|
||||||
dict_children={"myprop": expected_traverse},
|
|
||||||
child=expected_ignore,
|
|
||||||
)
|
|
||||||
|
|
||||||
ret = [
|
|
||||||
context.current
|
|
||||||
for context in GraphTraversal([traverse_lists_rule]).traverse(test_case)
|
|
||||||
]
|
|
||||||
|
|
||||||
self.assertCountEqual(ret, [test_case, expected_traverse])
|
|
||||||
self.assertNotIn(expected_ignore, ret)
|
|
||||||
self.assertEqual(len(ret), 2)
|
|
||||||
|
|
||||||
def test_traverse_dynamic(self):
|
|
||||||
traverse_lists_rule = TraversalRule(
|
|
||||||
[lambda _: True], lambda x: x.get_dynamic_member_names()
|
|
||||||
)
|
|
||||||
|
|
||||||
expected_traverse = Base()
|
|
||||||
expected_traverse.id = "List Member"
|
|
||||||
|
|
||||||
expected_ignore = Base()
|
|
||||||
expected_ignore.id = "Not List Member"
|
|
||||||
|
|
||||||
test_case = TraversalMock(
|
|
||||||
child=expected_ignore,
|
|
||||||
list_children=[expected_ignore],
|
|
||||||
dict_children={"myprop": expected_ignore},
|
|
||||||
)
|
|
||||||
test_case["dynamicChild"] = expected_traverse
|
|
||||||
test_case["dynamicListChild"] = [expected_traverse]
|
|
||||||
|
|
||||||
ret = [
|
|
||||||
context.current
|
|
||||||
for context in GraphTraversal([traverse_lists_rule]).traverse(test_case)
|
|
||||||
]
|
|
||||||
|
|
||||||
self.assertCountEqual(ret, [test_case, expected_traverse, expected_traverse])
|
|
||||||
self.assertNotIn(expected_ignore, ret)
|
|
||||||
self.assertEqual(len(ret), 3)
|
|
||||||
@@ -107,12 +107,7 @@ fake_bases = [FakeBase("foo"), FakeBase("bar")]
|
|||||||
fake_bases,
|
fake_bases,
|
||||||
),
|
),
|
||||||
(List["int"], [2, 3, 4], True, [2, 3, 4]),
|
(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": 1, "bar": 2},
|
|
||||||
True,
|
|
||||||
{"foo": 1.0, "bar": 2.0},
|
|
||||||
),
|
|
||||||
(Union[float, Dict[str, float]], {"foo": "bar"}, False, {"foo": "bar"}),
|
(Union[float, Dict[str, float]], {"foo": "bar"}, False, {"foo": "bar"}),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
import pytest
|
|
||||||
|
|
||||||
from specklepy.objects.units import Units, get_scale_factor
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"fromUnits, toUnits, inValue, expectedOutValue",
|
|
||||||
[
|
|
||||||
# To self
|
|
||||||
(Units.km, Units.km, 1.5, 1.5),
|
|
||||||
(Units.km, Units.km, 0, 0),
|
|
||||||
(Units.m, Units.m, 1.5, 1.5),
|
|
||||||
(Units.m, Units.m, 0, 0),
|
|
||||||
(Units.cm, Units.cm, 1.5, 1.5),
|
|
||||||
(Units.cm, Units.cm, 0, 0),
|
|
||||||
(Units.mm, Units.mm, 1.5, 1.5),
|
|
||||||
(Units.mm, Units.mm, 0, 0),
|
|
||||||
(Units.miles, Units.miles, 1.5, 1.5),
|
|
||||||
(Units.miles, Units.miles, 0, 0),
|
|
||||||
(Units.yards, Units.yards, 1.5, 1.5),
|
|
||||||
(Units.yards, Units.yards, 0, 0),
|
|
||||||
(Units.feet, Units.feet, 1.5, 1.5),
|
|
||||||
(Units.feet, Units.feet, 0, 0),
|
|
||||||
# To Meters
|
|
||||||
(Units.km, Units.m, 987654.321, 987654321),
|
|
||||||
(Units.m, Units.m, 987654.321, 987654.321),
|
|
||||||
(Units.mm, Units.m, 98765432.1, 98765.4321),
|
|
||||||
(Units.cm, Units.m, 9876543.21, 98765.4321),
|
|
||||||
# To negative meters
|
|
||||||
(Units.km, Units.m, -987654.321, -987654321),
|
|
||||||
(Units.m, Units.m, -987654.321, -987654.321),
|
|
||||||
(Units.mm, Units.m, -98765432.1, -98765.4321),
|
|
||||||
(Units.cm, Units.m, -9876543.21, -98765.4321),
|
|
||||||
(Units.m, Units.km, 987654.321, 987.654321),
|
|
||||||
(Units.m, Units.cm, 987654.321, 98765432.1),
|
|
||||||
(Units.m, Units.mm, 987654.321, 987654321),
|
|
||||||
# Imperial
|
|
||||||
(Units.miles, Units.m, 123.45, 198673.517),
|
|
||||||
(Units.miles, Units.inches, 123.45, 7821792),
|
|
||||||
(Units.yards, Units.m, 123.45, 112.88268),
|
|
||||||
(Units.yards, Units.inches, 123.45, 4444.2),
|
|
||||||
(Units.feet, Units.m, 123.45, 37.62756),
|
|
||||||
(Units.feet, Units.inches, 123.45, 1481.4),
|
|
||||||
(Units.inches, Units.m, 123.45, 3.13563),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_get_scale_factor_between_units(
|
|
||||||
fromUnits: Units, toUnits: Units, inValue: float, expectedOutValue: float
|
|
||||||
):
|
|
||||||
Tolerance = 1e-10
|
|
||||||
actual = inValue * get_scale_factor(fromUnits, toUnits)
|
|
||||||
assert actual - expectedOutValue < Tolerance
|
|
||||||
+26
-26
@@ -7,6 +7,8 @@ from importlib import import_module, invalidate_caches
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
import pkg_resources
|
||||||
|
|
||||||
_user_data_env_var = "SPECKLE_USERDATA_PATH"
|
_user_data_env_var = "SPECKLE_USERDATA_PATH"
|
||||||
|
|
||||||
|
|
||||||
@@ -131,8 +133,8 @@ def ensure_pip() -> None:
|
|||||||
|
|
||||||
def get_requirements_path() -> Path:
|
def get_requirements_path() -> Path:
|
||||||
# we assume that a requirements.txt exists next to the __init__.py file
|
# we assume that a requirements.txt exists next to the __init__.py file
|
||||||
path = Path(Path(__file__).parent, "requirements.txt")
|
path = Path(__file__).parent.with_name("requirements.txt")
|
||||||
assert path.exists()
|
assert path.exists(), f"Can't find requirements file at {path}"
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
@@ -161,7 +163,10 @@ def install_requirements(host_application: str) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if completed_process.returncode != 0:
|
if completed_process.returncode != 0:
|
||||||
m = f"Failed to install dependenices through pip, got {completed_process.returncode} return code"
|
m = (
|
||||||
|
"Failed to install dependenices through pip, ",
|
||||||
|
f"got {completed_process.returncode} return code",
|
||||||
|
)
|
||||||
print(m)
|
print(m)
|
||||||
raise Exception(m)
|
raise Exception(m)
|
||||||
|
|
||||||
@@ -173,30 +178,25 @@ def install_dependencies(host_application: str) -> None:
|
|||||||
install_requirements(host_application)
|
install_requirements(host_application)
|
||||||
|
|
||||||
|
|
||||||
def _import_dependencies() -> None:
|
def _dependencies_installed() -> bool:
|
||||||
import_module("specklepy")
|
try:
|
||||||
# the code above doesn't work for now, it fails on importing graphql-core
|
pkg_resources.require(get_requirements_path().read_text())
|
||||||
# despite that, the connector seams to be working as expected
|
return True
|
||||||
# But it would be nice to make this solution work
|
except (pkg_resources.DistributionNotFound, pkg_resources.VersionConflict):
|
||||||
# it would ensure that all dependencies are fully loaded
|
return False
|
||||||
# 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:
|
def ensure_dependencies(host_application: str) -> None:
|
||||||
try:
|
if _dependencies_installed():
|
||||||
install_dependencies(host_application)
|
return
|
||||||
invalidate_caches()
|
|
||||||
_import_dependencies()
|
install_dependencies(host_application)
|
||||||
|
invalidate_caches()
|
||||||
|
if _dependencies_installed():
|
||||||
print("Successfully found dependencies")
|
print("Successfully found dependencies")
|
||||||
except ImportError:
|
return
|
||||||
raise Exception(
|
|
||||||
f"Cannot automatically ensure Speckle dependencies. Please try restarting the host application {host_application}!"
|
raise Exception(
|
||||||
)
|
"Cannot automatically ensure Speckle dependencies. ",
|
||||||
|
f"Please try restarting the host application {host_application}!",
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user