Skip to content

Instantly share code, notes, and snippets.

@giohappy
Last active July 5, 2022 13:42
Show Gist options
  • Save giohappy/1087babaa0869afd8b11ae1274f7850c to your computer and use it in GitHub Desktop.
Save giohappy/1087babaa0869afd8b11ae1274f7850c to your computer and use it in GitHub Desktop.
Configure GWC caching for all Geoserver layers
import requests
import json
REQ_TEMPLATE = '''<?xml version="1.0" encoding="UTF-8"?>
<GeoServerLayer>
<enabled>true</enabled>
<inMemoryCached>true</inMemoryCached>
<name>{}</name>
<metaWidthHeight>
<int>2</int>
<int>1</int>
</metaWidthHeight>
<mimeFormats>
<string>application/json;type=utfgrid</string>
<string>image/png</string>
<string>image/vnd.jpeg-png</string>
<string>image/jpeg</string>
<string>image/vnd.jpeg-png8</string>
<string>image/gif</string>
<string>image/png8</string>
</mimeFormats>
<gridSubsets>
<gridSubset>
<gridSetName>EPSG:3857</gridSetName>
</gridSubset>
<gridSubset>
<gridSetName>EPSG:3857x2</gridSetName>
</gridSubset>
<gridSubset>
<gridSetName>EPSG:4326</gridSetName>
</gridSubset>
<gridSubset>
<gridSetName>EPSG:900913</gridSetName>
</gridSubset>
</gridSubsets>
</GeoServerLayer>'''
BASE_API_URL = 'https:/xxxxxx/geoserver/gwc/rest/'
GS_uSER = 'xxxxxx'
GS_PWD = 'xxxxxx'
with requests.Session() as s:
s.auth = (GS_uSER, GS_PWD)
response = s.get(BASE_API_URL + 'layers.json')
layers = []
if response.status_code == 200:
print('Success!')
layers = json.loads(response.text)
elif response.status_code == 404:
print('Error.')
headers = {'Content-Type': 'application/xml'}
for k, layer in enumerate(layers):
data = REQ_TEMPLATE.format(layer)
url = BASE_API_URL + 'layers/' + layer
response = s.put(url, data=data, headers=headers)
if response.status_code == 200:
print(f'{k} - Configured layer {layer}')
else:
print(f'layer {layer} couldn't be configured')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment