Skip to content

Commit 1fac4b5

Browse files
committed
more methods and url cleanup
1 parent e148ef2 commit 1fac4b5

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

jupiterone/client.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
from warnings import warn
77
from typing import Dict, List
8+
from datetime import datetime
89

910
import re
1011
import requests
@@ -36,7 +37,7 @@
3637
EVALUATE_SMARTCLASS,
3738
GET_SMARTCLASS_DETAILS,
3839
LIST_RULE_INSTANCES,
39-
J1QL_FROM_NATURAL_LANGUAGE
40+
J1QL_FROM_NATURAL_LANGUAGE, UPDATE_RELATIONSHIP
4041
)
4142

4243

@@ -63,9 +64,7 @@ class JupiterOneClient:
6364
def __init__(self, account: str = None, token: str = None, url: str = DEFAULT_URL, sync_url: str = SYNC_API_URL):
6465
self.account = account
6566
self.token = token
66-
self.url = url
67-
self.query_endpoint = self.url
68-
self.rules_endpoint = self.url + "/rules/graphql"
67+
self.graphql_url = url
6968
self.sync_url = sync_url
7069
self.headers = {
7170
"Authorization": "Bearer {}".format(self.token),
@@ -115,7 +114,7 @@ def _execute_query(self, query: str, variables: Dict = None) -> Dict:
115114
s.mount('https://', HTTPAdapter(max_retries=retries))
116115

117116
response = s.post(
118-
self.query_endpoint, headers=self.headers, json=data, timeout=60
117+
self.graphql_url, headers=self.headers, json=data, timeout=60
119118
)
120119

121120
# It is still unclear if all responses will have a status
@@ -401,6 +400,28 @@ def create_relationship(self, **kwargs) -> Dict:
401400
response = self._execute_query(query=CREATE_RELATIONSHIP, variables=variables)
402401
return response["data"]["createRelationship"]
403402

403+
def update_relationship(self, **kwargs) -> Dict:
404+
"""
405+
Update a relationship (edge) between two entities (vertices).
406+
407+
args:
408+
relationship_id (str): Unique _id of the relationship
409+
properties (dict): Dictionary of key/value relationship properties
410+
"""
411+
now_dt = datetime.now()
412+
413+
variables = {
414+
"relationshipId": kwargs.pop("relationship_id"),
415+
"timestamp": datetime.strptime(str(now_dt), "%Y-%m-%d %H:%M:%S.%f").timestamp()
416+
}
417+
418+
properties = kwargs.pop("properties", None)
419+
if properties:
420+
variables["properties"] = properties
421+
422+
response = self._execute_query(query=UPDATE_RELATIONSHIP, variables=variables)
423+
return response["data"]["updateRelationship"]
424+
404425
def delete_relationship(self, relationship_id: str = None):
405426
"""Deletes a relationship between two entities.
406427
@@ -542,6 +563,23 @@ def upload_combined_batch_json(self, instance_job_id: str = None, combined_paylo
542563

543564
return response
544565

566+
def bulk_delete_entities(self, instance_job_id: str = None, entities_list: list = None):
567+
"""Send a request to bulk delete existing entities.
568+
569+
args:
570+
instance_job_id (str): The "Job ID" for the Custom Integration job.
571+
entities_list (list): List of dictionaries containing entities _id's to be deleted.
572+
"""
573+
endpoint = f"/persister/synchronization/jobs/{instance_job_id}/upload"
574+
575+
data = {
576+
"deleteEntities": entities_list
577+
}
578+
579+
response = self._execute_syncapi_request(endpoint=endpoint, payload=data)
580+
581+
return response
582+
545583
def finalize_sync_job(self, instance_job_id: str = None):
546584
"""Start a synchronization job.
547585

jupiterone/constants.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,35 @@
117117
}
118118
"""
119119

120+
UPDATE_RELATIONSHIP = """
121+
mutation UpdateRelationship (
122+
$relationshipId: String!
123+
$timestamp: Long
124+
$properties: JSON
125+
) {
126+
updateRelationship (
127+
relationshipId: $relationshipId,
128+
timestamp: $timestamp,
129+
properties: $properties
130+
) {
131+
relationship {
132+
_id
133+
...
134+
}
135+
edge {
136+
id
137+
toVertexId
138+
fromVertexId
139+
relationship {
140+
_id
141+
...
142+
}
143+
properties
144+
}
145+
}
146+
}
147+
"""
148+
120149
DELETE_RELATIONSHIP = """
121150
mutation DeleteRelationship($relationshipId: String! $timestamp: Long) {
122151
deleteRelationship (relationshipId: $relationshipId, timestamp: $timestamp) {

0 commit comments

Comments
 (0)