Skip to content

Commit fe70230

Browse files
committed
Added Http2HtppsSample.ipynb code.
1 parent 4a50f9e commit fe70230

1 file changed

Lines changed: 239 additions & 0 deletions

File tree

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## How can I update layers in my web map to use HTTPS?\n",
8+
"\n",
9+
"As the owner or administrator of a web map, you can update all layers in the web map to use HTTPS. This can be done either by the GUI on ArcGIS Online from the Settings tab of the map's item page or through the ArcGIS API for Python. It is recommended that you access all layers using HTTPS, which encrypts the information in the layer when it's transmitted over the Internet.\n",
10+
"\n",
11+
"When you update the layers in your map to use HTTPS, ArcGIS Online inspects each layer in the map to determine whether it needs to be updated to HTTPS. If any layers are found to use HTTP, ArcGIS Online attempts to make a request to the same layer using HTTPS and updates the map and any associated layer items you own or administer. If a layer does not support HTTPS, you are notified of this fact and the layer is not updated in the map. If you are notified that the layer does not support HTTPS, contact the owner of the layer, who can either configure the layer to support HTTPS or provide an alternative resource.\n",
12+
"\n",
13+
"When you attempt to update layers in your map to use HTTPS, any custom symbols that use HTTP are automatically updated to use HTTPS if the source of the custom symbols supports it. Other layer customizations, such as images embedded in pop-ups, are not updated to HTTPS. To update these references to use HTTPS, open the layer (or map) in Map Viewer and update the references there."
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 29,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"from arcgis.gis import GIS\n",
23+
"from arcgis.gis import Item"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"### Connect to the GIS"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"gis = GIS(url=\"https://python.playground.esri.com/portal\",\n",
40+
" username=\"arcgis_python\")"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"### Copy the Item for the Demo"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": 37,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"wm = gis.content.get(\"722f8121d63242b98366e012e4431e13\")\n",
57+
"wm = wm.copy(title=\"%s - ALL HTTPS\" % wm.title)"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 38,
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"data": {
67+
"text/html": [
68+
"<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
69+
" <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
70+
" <a href='https://python.playground.esri.com/portal/home/item.html?id=0f7b6918356f4e29b3e27ac102a269ca' target='_blank'>\n",
71+
" <img src='https://python.playground.esri.com/portal/portalimages/desktopapp.png' class=\"itemThumbnail\">\n",
72+
" </a>\n",
73+
" </div>\n",
74+
"\n",
75+
" <div class=\"item_right\" style=\"float: none; width: auto; overflow: hidden;\">\n",
76+
" <a href='https://python.playground.esri.com/portal/home/item.html?id=0f7b6918356f4e29b3e27ac102a269ca' target='_blank'><b>HTTPtoHTTPSExample - ALL HTTPS</b>\n",
77+
" </a>\n",
78+
" <br/>Converts Service from HTTP to HTTPS<img src='https://python.playground.esri.com/portal/home/js/jsapi/esri/css/images/item_type_icons/maps16.png' style=\"vertical-align:middle;\">Web Map by andrew\n",
79+
" <br/>Last Modified: April 12, 2018\n",
80+
" <br/>0 comments, 0 views\n",
81+
" </div>\n",
82+
" </div>\n",
83+
" "
84+
],
85+
"text/plain": [
86+
"<Item title:\"HTTPtoHTTPSExample - ALL HTTPS\" type:Web Map owner:andrew>"
87+
]
88+
},
89+
"execution_count": 38,
90+
"metadata": {},
91+
"output_type": "execute_result"
92+
}
93+
],
94+
"source": [
95+
"wm"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"### Updating the HTTP Scheme to HTTPS Scheme\n",
103+
"\n",
104+
"The code below walks the operational layers and checks if the service can be updated to HTTPS."
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 33,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"import json\n",
114+
"from arcgis.features import FeatureLayer\n",
115+
"from arcgis.raster import ImageryLayer\n",
116+
"from arcgis.mapping import MapImageLayer, VectorTileLayer\n",
117+
"def update_http_to_https(wm):\n",
118+
" \"\"\"\n",
119+
" Updates an Item's JSON by Changing All HTTP Scheme to HTTPS.\n",
120+
" \n",
121+
" ============ =====================================\n",
122+
" **Inputs** **Description**\n",
123+
" ------------ -------------------------------------\n",
124+
" wm Required Item. Must be a Web Map Item.\n",
125+
" ============ =====================================\n",
126+
" \n",
127+
" :returns: boolean\n",
128+
" \"\"\"\n",
129+
"\n",
130+
" if wm.type == 'Web Map':\n",
131+
" data = wm.get_data()\n",
132+
" if 'operationalLayers' in data:\n",
133+
" for lyr in data['operationalLayers']:\n",
134+
" if 'url' in lyr:\n",
135+
" url = lyr['url']\n",
136+
" if url.lower().find(\"http://\") > -1:\n",
137+
" try:\n",
138+
" if lyr['layerType'] == 'ArcGISFeatureLayer':\n",
139+
" url = url.replace(\"http://\", \"https://\")\n",
140+
" fl = FeatureLayer(url, gis=gis)\n",
141+
" fl.properties\n",
142+
" elif lyr['layerType'] == 'ArcGISTiledMapServiceLayer':\n",
143+
" url = url.replace(\"http://\", \"https://\")\n",
144+
" fl = MapImageLayer(url, gis=gis)\n",
145+
" fl.properties\n",
146+
" elif lyr['layerType'] == 'ArcGISImageServiceLayer':\n",
147+
" url = url.replace(\"http://\", \"https://\")\n",
148+
" fl = ImageryLayer(url, gis=gis)\n",
149+
" fl.properties\n",
150+
" lyr['url'] = url\n",
151+
" except:\n",
152+
" print(\"HTTPS is not supported on %s\" % lyr['url'])\n",
153+
" else:\n",
154+
" print(\"Updated service to: %s\" % lyr['url'])\n",
155+
" else:\n",
156+
" print(\"Cannot update layer: %s\" % lyr)\n",
157+
"\n",
158+
" wm.update({\"text\" : json.dumps(data)})\n",
159+
" return True"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 34,
165+
"metadata": {},
166+
"outputs": [
167+
{
168+
"name": "stdout",
169+
"output_type": "stream",
170+
"text": [
171+
"Cannot update layer: {'layerType': 'VectorTileLayer', 'type': 'VectorTileLayer', 'opacity': 1, 'itemId': 'aacf0898e01144f7b3f981ab18fc3626', 'title': 'World_earthquakes_2010', 'visibility': True, 'id': 'VectorTile_7217', 'styleUrl': 'http://python.playground.esri.com/server/rest/services/Hosted/World_earthquakes_2010/VectorTileServer/resources/styles/root.json'}\n",
172+
"Updated service to: https://python.playground.esri.com/server/rest/services/Hosted/earthquakes/FeatureServer/0\n"
173+
]
174+
},
175+
{
176+
"data": {
177+
"text/plain": [
178+
"True"
179+
]
180+
},
181+
"execution_count": 34,
182+
"metadata": {},
183+
"output_type": "execute_result"
184+
}
185+
],
186+
"source": [
187+
"update_http_to_https(wm)"
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": 35,
193+
"metadata": {},
194+
"outputs": [
195+
{
196+
"data": {
197+
"application/vnd.jupyter.widget-view+json": {
198+
"model_id": "49a2dd4ba5c64d34ada92a882b5bcb81"
199+
}
200+
},
201+
"metadata": {},
202+
"output_type": "display_data"
203+
}
204+
],
205+
"source": [
206+
"from arcgis.mapping import WebMap\n",
207+
"WebMap(wm)"
208+
]
209+
},
210+
{
211+
"cell_type": "markdown",
212+
"metadata": {},
213+
"source": [
214+
"We now have a copy of a web map that is HTTPS verse the default version that has HTTP service URLs."
215+
]
216+
}
217+
],
218+
"metadata": {
219+
"kernelspec": {
220+
"display_name": "Python 3",
221+
"language": "python",
222+
"name": "python3"
223+
},
224+
"language_info": {
225+
"codemirror_mode": {
226+
"name": "ipython",
227+
"version": 3
228+
},
229+
"file_extension": ".py",
230+
"mimetype": "text/x-python",
231+
"name": "python",
232+
"nbconvert_exporter": "python",
233+
"pygments_lexer": "ipython3",
234+
"version": "3.5.3"
235+
}
236+
},
237+
"nbformat": 4,
238+
"nbformat_minor": 2
239+
}

0 commit comments

Comments
 (0)