Skip to content

Commit

Permalink
Encapsulate style and layer url of GS rest
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git authored and jirik committed Feb 4, 2021
1 parent 147cb8c commit e0a3364
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/layman/layer/filesystem/thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import pathlib
from urllib.parse import urljoin
from layman import settings

from layman import patch_mode
Expand Down Expand Up @@ -64,11 +63,10 @@ def get_layer_thumbnail_path(username, layername):


def generate_layer_thumbnail(workspace, layername):
geoserver_workspace = geoserver_wms.get_geoserver_workspace(workspace)
headers = {
settings.LAYMAN_GS_AUTHN_HTTP_HEADER_ATTRIBUTE: settings.LAYMAN_GS_USER,
}
wms_url = urljoin(settings.LAYMAN_GS_URL, geoserver_workspace + '/ows')
wms_url = geoserver_wms.get_wms_url(workspace)
from layman.layer.geoserver.util import wms_proxy
from layman.common.geoserver import get_layer_thumbnail, get_layer_square_bbox
wms = wms_proxy(wms_url, headers=headers)
Expand Down
29 changes: 19 additions & 10 deletions src/layman/layer/geoserver/sld.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
}


def get_workspace_style_url(workspace, style=None):
style = style or ''
geoserver_workspace = wms.get_geoserver_workspace(workspace)
return urljoin(settings.LAYMAN_GS_REST_WORKSPACES,
geoserver_workspace + '/styles/' + style)


def get_workspace_layer_url(workspace, layer=None):
layer = layer or ''
geoserver_workspace = wms.get_geoserver_workspace(workspace)
return urljoin(settings.LAYMAN_GS_REST_WORKSPACES,
geoserver_workspace + '/layers/' + layer)


def pre_publication_action_check(username, layername):
pass

Expand All @@ -34,9 +48,7 @@ def patch_layer(username, layername):


def delete_layer(workspace, layername):
geoserver_workspace = wms.get_geoserver_workspace(workspace)
style_url = urljoin(settings.LAYMAN_GS_REST_WORKSPACES,
geoserver_workspace + '/styles/' + layername)
style_url = get_workspace_style_url(workspace, layername)
r = requests.get(style_url + '.sld',
auth=settings.LAYMAN_GS_AUTH,
timeout=5,
Expand Down Expand Up @@ -105,7 +117,7 @@ def create_layer_style(workspace, layername):
r.raise_for_status()
sld_file = io.BytesIO(r.content)
r = requests.post(
urljoin(settings.LAYMAN_GS_REST_WORKSPACES, geoserver_workspace + '/styles/'),
get_workspace_style_url(workspace),
data=json.dumps(
{
"style": {
Expand Down Expand Up @@ -148,8 +160,7 @@ def create_layer_style(workspace, layername):
sld_file.seek(0)

r = requests.put(
urljoin(settings.LAYMAN_GS_REST_WORKSPACES,
geoserver_workspace + '/styles/' + layername),
get_workspace_style_url(workspace, layername),
data=sld_file.read(),
headers={
'Accept': 'application/json',
Expand All @@ -161,8 +172,7 @@ def create_layer_style(workspace, layername):
if r.status_code == 400:
raise LaymanError(14, data=r.text)
r.raise_for_status()
r = requests.put(urljoin(settings.LAYMAN_GS_REST_WORKSPACES,
geoserver_workspace + '/layers/' + layername),
r = requests.put(get_workspace_layer_url(workspace, layername),
data=json.dumps(
{
"layer": {
Expand All @@ -185,10 +195,9 @@ def get_metadata_comparison(username, layername):


def get_style_response(workspace, stylename, headers=None, auth=None):
geoserver_workspace = wms.get_geoserver_workspace(workspace)
if headers is None:
headers = headers_sld
url = settings.LAYMAN_GS_REST + f'workspaces/{geoserver_workspace}/styles/{stylename}'
url = get_workspace_style_url(workspace, stylename)

r = requests.get(url,
auth=auth,
Expand Down
21 changes: 9 additions & 12 deletions src/layman/layer/rest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import io
import json
import os
from multiprocessing import Process
import requests
import time
import xml.etree.ElementTree as ET
Expand All @@ -25,6 +24,7 @@
from layman.layer.filesystem.thumbnail import get_layer_thumbnail_path
from layman import uuid
from layman.layer import db
from layman.layer.geoserver import wms as geoserver_wms, sld as geoserver_sld
from layman import celery as celery_util
from .micka import csw
from layman.common.micka import util as micka_common_util
Expand Down Expand Up @@ -289,7 +289,7 @@ def test_post_layers_simple(client):
assert isinstance(layer_info[key_to_check], str) \
or 'status' not in layer_info[key_to_check]

wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows')
wms_url = geoserver_wms.get_wms_url(username)
wms = wms_proxy(wms_url)
assert layername in wms.contents

Expand Down Expand Up @@ -455,7 +455,7 @@ def test_post_layers_shp(client):
flask_client.wait_till_layer_ready(username, layername)
# last_task['last'].get()

wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows')
wms_url = geoserver_wms.get_wms_url(username)
wms = wms_proxy(wms_url)
assert 'ne_110m_admin_0_countries_shp' in wms.contents
uuid.check_redis_consistency(expected_publ_num_by_type={
Expand Down Expand Up @@ -555,7 +555,7 @@ def test_post_layers_complex(client):
# last_task['last'].get()
assert celery_util.is_task_ready(last_task)

wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows')
wms_url = geoserver_wms.get_wms_url(username)
wms = wms_proxy(wms_url)
assert 'countries' in wms.contents
assert wms['countries'].title == 'staty'
Expand All @@ -580,8 +580,7 @@ def test_post_layers_complex(client):
]:
assert 'status' not in resp_json[source]

style_url = urljoin(settings.LAYMAN_GS_REST_WORKSPACES,
username + '_wms/styles/' + layername)
style_url = geoserver_sld.get_workspace_style_url(username, layername)
r = requests.get(style_url + '.sld',
auth=settings.LAYMAN_GS_AUTH
)
Expand Down Expand Up @@ -670,8 +669,7 @@ def test_uppercase_attr(client):
]:
assert 'status' not in resp_json[source], f"{source}: {resp_json[source]}"

style_url = urljoin(settings.LAYMAN_GS_REST_WORKSPACES,
username + '_wms/styles/' + layername)
style_url = geoserver_sld.get_workspace_style_url(username, layername)
r = requests.get(style_url + '.sld',
auth=settings.LAYMAN_GS_AUTH
)
Expand Down Expand Up @@ -807,7 +805,7 @@ def test_patch_layer_style(client):
resp_json = rv.get_json()
assert resp_json['title'] == "countries in blue"

wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows')
wms_url = geoserver_wms.get_wms_url(username)
wms = wms_proxy(wms_url)
assert layername in wms.contents
assert wms[layername].title == 'countries in blue'
Expand Down Expand Up @@ -872,13 +870,12 @@ def test_post_layers_sld_1_1_0(client):
time.sleep(0.1)
layer_info = util.get_layer_info(username, layername)

wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows')
wms_url = geoserver_wms.get_wms_url(username)
wms = wms_proxy(wms_url)
assert layername in wms.contents
assert wms[layername].title == 'countries_sld_1_1_0'

style_url = urljoin(settings.LAYMAN_GS_REST_WORKSPACES,
username + '_wms/styles/' + layername)
style_url = geoserver_sld.get_workspace_style_url(username, layername)
r = requests.get(style_url + '.sld',
auth=settings.LAYMAN_GS_AUTH
)
Expand Down

0 comments on commit e0a3364

Please sign in to comment.