Added image reference and query support

This commit is contained in:
Rick Rongen
2019-02-13 19:28:15 +01:00
parent 5262c679d0
commit 96659dd9cb

View File

@@ -1,4 +1,4 @@
from typing import Union from typing import Union, List
import requests import requests
from urllib.parse import urljoin from urllib.parse import urljoin
@@ -13,13 +13,21 @@ http://localhost:4502/crx/de/init.jsp?_dc=1549392939742
http://localhost:4502/crx/de/nodetypes.jsp?_dc=1549392939958 http://localhost:4502/crx/de/nodetypes.jsp?_dc=1549392939958
http://localhost:4502/crx/server/crx.default/jcr%3aroot/libs.1.json?_dc=1549392123434&node=xnode-265 http://localhost:4502/crx/server/crx.default/jcr%3aroot/libs.1.json?_dc=1549392123434&node=xnode-265
http://localhost:4502/crx/de/query.jsp?_dc=1549392245191&_charset_=utf-8&type=xpath&stmt=%2Fjcr%3Aroot%2Fbin%2F%2F*%5Bjcr%3Acontains(.%2C%20%27asdf%27)%5D%20order%20by%20%40jcr%3Ascore&showResults=true http://localhost:4502/crx/de/query.jsp?_dc=1549392245191&_charset_=utf-8&type=xpath&stmt=%2Fjcr%3Aroot%2Fbin%2F%2F*%5Bjcr%3Acontains(.%2C%20%27asdf%27)%5D%20order%20by%20%40jcr%3Ascore&showResults=true
http://app30-prd-asd.sbnl.vancis.nl:4502/bin/wcm/references.json?path=%2Fcontent%2Fdam%2Fbeeldbank%2F_0005_home_algemeen.png&predicate=wcmcontent&_charset_=utf-8
""" """
CRX_SERVER_ROOT = '/crx/server/crx.default/jcr:root/' CRX_SERVER_ROOT = '/crx/server/crx.default/jcr:root/'
CRX_QUERY = '/crx/de/query.jsp' CRX_QUERY = '/crx/de/query.jsp'
WCM_REFERENCES = '/bin/wcm/references.json'
JSON_DATA_EXTENSION = '.1.json' JSON_DATA_EXTENSION = '.1.json'
QUERY_TYPES = {
'XPATH': 'xpath',
'SQL': 'sql',
'SQL2': 'JCR-SQL2'
}
class CrxException(ValueError): class CrxException(ValueError):
pass pass
@@ -31,17 +39,38 @@ class Connection:
port: int = 4502, port: int = 4502,
protocol: str = 'http', protocol: str = 'http',
root: str = CRX_SERVER_ROOT, root: str = CRX_SERVER_ROOT,
query: str = CRX_QUERY): query: str = CRX_QUERY,
image_references: str = WCM_REFERENCES):
self.host = f'{protocol}://{host}:{port}' self.host = f'{protocol}://{host}:{port}'
self.data_root = self.host + root self.data_root = self.host + root
self.query_path = self.host + query self.query_path = self.host + query
self.image_references = self.host + image_references
self.session = requests.session() self.session = requests.session()
def login_basic(self, username: str, password: str): def login_basic(self, username: str, password: str):
self.session.auth = (username, password) self.session.auth = (username, password)
def query(self, query: str, query_type: str = 'SQL2') -> List[str]:
response = self.session.get(self.query_path, params={
'_charset': 'utf-8',
'type': QUERY_TYPES.get(query_type, query_type),
'stmt': query,
'showResults': 'true'
})
data = response.json()
# TODO check for error
return list(map(lambda node: node['path'], data['results']))
def get_image_references(self, path: str):
response = self.session.get(self.image_references, params={
'path': path
})
return response.json()['pages']
def get_node_raw(self, path: str): def get_node_raw(self, path: str):
url = urljoin(self.data_root, '.' + path + JSON_DATA_EXTENSION) url = urljoin(self.data_root, '.' + path + JSON_DATA_EXTENSION)
try: try: