Hello,
I'm hoping someone can help me to access a value in a JSON response that results from a successful CreateReplica() call. I'm following the documentation here:
https://developers.arcgis.com/rest/services-reference/enterprise/create-replica.htm
In the examples below, a call to CreateReplica() with all the correct parameters in place would return the first example dictionary.
The statusURL value in the first dictionary links to a web page containing the data in the second dictionary, presented in HTML. What I'm trying to do is access the "status" value in the second dictionary using the API for Python. The closest I've gotten to it so far is by viewing the response.text result from a GET request on the statusUrl, but that's the full HTML contents of the web page. I could scrape that, but I'd rather not. Surely there's a better way?
From the docs:
JSON Response example
Example one
When async is set to true, the request is processed as an asynchronous job and a URL that a client can visit to check the status of the job is returned. The file returned in the resultURL is a sqlite geodatabase. The sqlite replica geodatabase contains the same information and data as the JSON file content in the subsequent example:
{
"statusUrl": "https://services.myserver.com/arcgis/rest/services/LandUse/FeatureServer/jobs/j0b4b6064db0f44e6b5f39..."
}
Response example for the status resource:
{
"transportType":"esriTransportTypeUrl",
"responseType": "esriReplicaResponseTypeData",
"replicaName": "Meters",
"resultUrl": "https://arcgis.com/lidGgNLxw9LL0SbI/ArcGIS/rest/services/SaveTheBay/replicafiles/c2f366ffbf5549a4872...",
"submissionTime": 1379366479000,
"lastUpdatedTime": 1379366482000,
"status": "Completed"
}
Solved! Go to Solution.
Is this what you're trying to do?
import requests
from arcgis import GIS
from arcgis.features import FeatureLayerCollection
url = "https://pythonapi.playground.esri.com/server/rest/services/Hosted/Ports_in_the_Western_US/FeatureServer"
gis = GIS("https://pythonapi.playground.esri.com/portal", "arcgis_python", "amazing_arcgis_123")
flc = FeatureLayerCollection(url, gis)
replica = flc.replicas.create(
replica_name = 'arcgis_python_api_ports',
layers='0',
sync_model="perLayer",
target_type="server",
data_format="sqlite",
asynchronous=True
)
status_url = replica["statusUrl"]
params = {
"f": "json",
"token": gis._con.token
}
r = requests.post(status_url, params=params)
Where you can then use r.json() to get a json response from the status url:
{'responseType': 'esriReplicaResponseTypeData',
'replicaID': '037B207A-4E7B-423C-AC99-F9EE3AAE96E6',
'replicaName': 'arcgis_python_1696596866617',
'resultUrl': 'https://pythonapi.playground.esri.com/server/rest/directories/arcgisjobs/system/synctools_gpserver/je1fb0eacd85444baae62274cd6ea9811/scratch/_ags_data5464957EDC9346558F5FFEA3DCE5AEBC.geodatabase',
'transportType': 'esriTransportTypeURL',
'targetType': 'server',
'lastUpdatedTime': 1696596867000,
'layerServerGens': [{'serverGen': 1696596865593, 'id': 0, 'serverSibGen': 0}],
'submissionTime': 1696596865000,
'status': 'Completed'}
Thank you very much! I hadn't set up a post request, which is the missing piece.
Is this what you're trying to do?
import requests
from arcgis import GIS
from arcgis.features import FeatureLayerCollection
url = "https://pythonapi.playground.esri.com/server/rest/services/Hosted/Ports_in_the_Western_US/FeatureServer"
gis = GIS("https://pythonapi.playground.esri.com/portal", "arcgis_python", "amazing_arcgis_123")
flc = FeatureLayerCollection(url, gis)
replica = flc.replicas.create(
replica_name = 'arcgis_python_api_ports',
layers='0',
sync_model="perLayer",
target_type="server",
data_format="sqlite",
asynchronous=True
)
status_url = replica["statusUrl"]
params = {
"f": "json",
"token": gis._con.token
}
r = requests.post(status_url, params=params)
Where you can then use r.json() to get a json response from the status url:
{'responseType': 'esriReplicaResponseTypeData',
'replicaID': '037B207A-4E7B-423C-AC99-F9EE3AAE96E6',
'replicaName': 'arcgis_python_1696596866617',
'resultUrl': 'https://pythonapi.playground.esri.com/server/rest/directories/arcgisjobs/system/synctools_gpserver/je1fb0eacd85444baae62274cd6ea9811/scratch/_ags_data5464957EDC9346558F5FFEA3DCE5AEBC.geodatabase',
'transportType': 'esriTransportTypeURL',
'targetType': 'server',
'lastUpdatedTime': 1696596867000,
'layerServerGens': [{'serverGen': 1696596865593, 'id': 0, 'serverSibGen': 0}],
'submissionTime': 1696596865000,
'status': 'Completed'}
Thank you very much! I hadn't set up a post request, which is the missing piece.