Minitour commented on issue #20288:
URL: https://github.com/apache/superset/issues/20288#issuecomment-1150450935

   I was facing the same issue with the CLI, so I decided to use the API.
   It is a bit hacky but I hope this will help
   
   ```python
   import requests
   
   new_zip_file = 'path/to/dashboard_export.zip'
   username = 'admin'
   password = 'admin'
   
   base_url = 'http://localhost:8088'
   
   while True:
       try:
           response = requests.get(base_url, timeout=10)
           response.raise_for_status()
           break
       except Exception:
           pass
   
   login_url = f"{base_url}/login/"
   session = requests.Session()
   
   payload = {}
   headers = {
       'Accept': 
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
       'Accept-Language': 'en-US,en;q=0.9'
   }
   
   response = session.request("GET", login_url, headers=headers, data=payload)
   csrf_token = response.text.split('<input id="csrf_token" name="csrf_token" 
type="hidden" value="')[1].split('">')[0]
   
   # perform login
   
   payload = f'csrf_token={csrf_token}&username={username}&password={password}'
   headers = {
       'Accept': 
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
       'Content-Type': 'application/x-www-form-urlencoded'}
   
   session.request("POST", login_url, headers=headers, data=payload, 
allow_redirects=False)
   cookie = session.cookies.get_dict().get('session')
   print(session.cookies.get_dict())
   
   # Import dashboards
   
   import_dashboard_url = f"{base_url}/api/v1/dashboard/import/"
   
   with open(new_zip_file, 'rb') as f:
       payload = {
           'passwords': '{"databases/OMOP_CDM.yaml":"postgres"}',
           'overwrite': 'true'
       }
       files = [
           ('formData', ('dashboards.zip', f, 'application/zip'))
       ]
       headers = {
           'Accept': 'application/json',
           'Cookie': f'session={cookie}',
           'X-CSRFToken': csrf_token
       }
   
       response = requests.request("POST", import_dashboard_url, 
headers=headers, data=payload, files=files)
   
   print(response.text)
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to