Skip to content

APIKey.update method in ArcGIS Python API 2.4.0 contains typo and URL format issues #2232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pmd84 opened this issue Mar 11, 2025 · 1 comment
Labels

Comments

@pmd84
Copy link

pmd84 commented Mar 11, 2025

Describe the bug

The update method in the APIKey class (arcgis.gis._impl.APIKey) contains two bugs in version 2.4.0:

  1. There's a typo in the parameter name "privileges" (spelled as "priveleges" in the API)
  2. The URL construction doesn't properly handle trailing slashes, potentially creating malformed URLs

To Reproduce

Steps to reproduce the behavior:

from arcgis.gis import GIS
import arcgis

# Connect to ArcGIS Online
gis = GIS("https://www.arcgis.com", "username", "password")

# Get an existing API key
api_key_item = gis.content.get("your_api_key_id")

# Create an APIKey Class instance
from arcgis.gis._impl import APIKey
api_key_class = APIKey(api_key_item, gis)

# Try to update privileges - this will fail due to the typo
privileges = api_key_class.properties["privileges"] + ["portal:app:access:item:some_item_id"]
result = api_key_class.update(privileges=privileges)

Error:

KeyError: 'priveleges'

Additionally, when inspecting the URL construction in the update method, it can create malformed URLs if the GIS URL already contains a trailing slash.

Expected behavior

The update method should:

  1. Use the correct spelling "privileges" (not "priveleges")
  2. Properly handle URL construction with consistent slash handling

Proposed fix

Here's a patched version of the method that fixes both issues:

def patched_update(self, http_referers=None, privileges=None):
    """Patched version that fixes both the typo in 'privileges' and the URL format"""
    # Fix the URL format - ensure proper slash between domain and path
    base_url = self._gis._url
    if base_url.endswith('/'):
        base_url = base_url
    else:
        base_url = base_url + '/'
        
    url = f"{base_url}sharing/rest/content/users/{self._gis.users.me.username}/items/{self._item.id}/updateApiKey"
    
    params = {"f": "json"}
    if http_referers is not None:
        params["httpReferrers"] = http_referers
    if privileges is not None:
        params["privileges"] = privileges  # Fixed spelling here
    self._properties = None
    return self._gis._con.post(url, params)

Platform

  • OS: Databricks
  • Python API Version: 2.4.0

Additional context

This issue was discovered while working with API keys and attempting to update their privileges programmatically. The workaround is to patch the method as shown above. The issue appears to be specific to version 2.4.0.

@pmd84
Copy link
Author

pmd84 commented Apr 11, 2025

Hey all, just wanted to check in and see if this is an item that will get addressed in an upcoming release, or if it is in backlog. I'm trying to adapt my workflows around not being able to update API Keys, but it would be excellent to be able to use this library for API Key Management in the near future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant