Querying projects of UrbanDesignDatabases of a model

894
8
01-26-2023 01:48 PM
MaximeDemers
Occasional Contributor III

Hi,

I am trying to retreive all the projects from a Urban model including those of UrbanDesignDatabases.

I am using the following snippet in python using sgqlc but the results is empty. 

 

 

def query_urban_design_db_projects(endpoint_url, urban_model_id, token):
    endpoint = HTTPEndpoint(endpoint_url, {"Authorization": "Bearer " + token, "Content-type": "application/json", "Accept": "application/json"})
    op = Operation(schema.Query)
    projects = op.urban_model(urban_model_id=urban_model_id).urban_design_databases.projects(
        paging={"limit": 100}
    )
    attributes = projects.attributes()
    attributes.event_name()

    result = endpoint(op)
    errors = result.get("errors")
    if errors:
        raise Exception(errors)
    return result

 

 

This function returns: {'data': {'urbanModel': {'urbanDesignDatabases': []}}}

I know there is many projects from UrbanDesignDatabase in the Urban model I am querying.

Am I using the API correctly? Do I have to be the owner of the UrbanDesignDatabases to be able to query them?

I am the owner of the Urban model.

Thank you!

0 Kudos
8 Replies
AgnieszkaRozniak
Esri Contributor

Hi Maxime! Thank you so much for your question. 

By default, a project or a plan (stored in the urban design database) is only accessible to the user who created it. Could you try to change sharing settings on one of the design databases that you are trying to query? You can for example change it from Private to Public or share the design in the organization that your user belongs to. Note, that projects and plans can also be shared in a group.

Please let me know if that solves your issue!

0 Kudos
MaximeDemers
Occasional Contributor III

Hi @AgnieszkaRozniak ,

Thank you for your reply.

I have created a UrbanDesignDatabase project and shared it with my organization but it does not works.

MaximeDemers_1-1675110924681.png

 


However, if I share the project with everyone, the project is returned by the function:

{'data': {'urbanModel': {'urbanDesignDatabases': [{'projects': [{'attributes': {'EventName': 'Test'}}]}]}}}

MaximeDemers_0-1675110695127.png

I have verified that I am the owner of the oauth2 application (client id and secret key) which I use with the Urban API and that's correct.

I dont know why I can't query UrbanDesignDatabases that are not publicly shared if I am the owner of the model and the owner of the projects and I am using a valid oauth2 application.

Do you have an idea?

0 Kudos
AgnieszkaRozniak
Esri Contributor

Hi @MaximeDemers, if you are the owner of the project (design database) and provide the correct token, you should be able to query any information from this database. Since the project is accessible when shared with the public, but not accessible when sharing is set to private, I would suspect that the token that you are providing is not correct.

Could you please share with me the exact steps you are using to obtain the authentication token? Then I can try to reproduce your issue and try to see if there are any problems with your approach.

0 Kudos
MaximeDemers
Occasional Contributor III

The oauth2 application is created by me (DemersM) from the dashboard in developers.arcgis.com

MaximeDemers_0-1675179670130.png

 



I use the Client ID and Client Secret from that oauth2 application above to get a token like this:

 

 

 

 

import requests
from sgqlc.operation import Operation
from sgqlc.endpoint.http import HTTPEndpoint
from . import urban_api_schema as schema

def get_client_oauth2_token(portal_url, client_id, client_secret):
    url = "{}/sharing/rest/oauth2/token".format(portal_url)
    params = {
        'client_id': client_id,
        'grant_type': "client_credentials",
        'client_secret': client_secret
    }
    r = requests.post(url, params=params, verify=True)
    return r.json()

def query_urban_design_db_projects(endpoint_url, urban_model_id, token):
    endpoint = HTTPEndpoint(endpoint_url, {"Authorization": "Bearer " + token, "Content-type": "application/json", "Accept": "application/json"})
    op = Operation(schema.Query)
    projects = op.urban_model(urban_model_id=urban_model_id).urban_design_databases.projects(
        paging={"limit": 100}
    )
    attributes = projects.attributes()
    attributes.event_name()

    result = endpoint(op)
    errors = result.get("errors")
    if errors:
        raise Exception(errors)
    return result

token = get_client_oauth2_token("https://www.arcgis.com", "my_client_id", "my_client_secret")["access_token"]
projects = query_urban_design_db_projects("https://urban-api.arcgis.com/graphql", "703144ba056e43e59f373b971431a562", token)
print(projects)

 

 

 

 


Thanks again for your help!

0 Kudos
AgnieszkaRozniak
Esri Contributor

Hi @MaximeDemers. I am trying to reproduce your issue, but I get the following error when trying to import the `request` 

No module named 'request`  

Could you please provide an online example that I could access and test your code? Or tell me how to install this lib? 

Also, could you please copy paste the `Temporary token` from the dashboard in developers.arcgis.com and try if this authenticate your user? Then we could narrow down if the problem is with the token, the piece of your code that generate the token, or maybe it is Urban API related. Thank you!

0 Kudos
MaximeDemers
Occasional Contributor III

Sorry, this is a typo error in my code. The library is requests and it is installed by default with python3.

Yes I can share an online notebook that reproduce the problem. I am using Google Colab, I hope this is correct with you?

I just want to add that my UrbanModel is private, and I can access its private UrbanDatabase projects but not its private UrbanDesignDatabases, which I think it's not normal.

Maybe there is some limitations with the oauth2 token of "client_credential" type (esri doc). As I want to automatize some routines with the Urban API, having to sign in manually with a username/password before is not convenient.

Thanks again for taking the time helping me, it's really appreciated!

P.S.: I'll share you a link to Collab in private soon if it's ok.

MaximeDemers
Occasional Contributor III

Hello @AgnieszkaRozniak ,

I was wondering if you have updates on this issue?

I would like to update projects in UrbanDesignDatabase in a scheduled task using the API. I am looking in a way to authenticate without having to enter the username/password manually. I was hoping that using a Client ID and Client Secret from oauth2 application, like in the script above, would worked.

Thank you if you can help with this.

0 Kudos
AgnieszkaRozniak
Esri Contributor

Hi @MaximeDemers unfortunately we haven't had a chance to look into this issue yet, so I have no further update from our side. Sorry for that. 

0 Kudos