Skip to content
Snippets Groups Projects
Commit dfafd085 authored by Hans van den Heuvel's avatar Hans van den Heuvel
Browse files

Converted part of Marco's script into objects.

parent 68263c3f
No related branches found
No related tags found
No related merge requests found
import base64
#
import getpass
import Mcra90ApiLib
import json
import McraApi
#
from argparse import ArgumentParser, SUPPRESS
......@@ -11,7 +11,7 @@ import keyring
# input parameters
default_mcra_url = 'https://mcra.test.wur.nl/Mcra90/'
default_mcra_url = 'https://mcra.test.wur.nl/Mcra90'
parser = ArgumentParser(
description=
......@@ -39,7 +39,7 @@ parser.add_argument(
args = parser.parse_args()
if args.verbosity >= 1:
print("URL of EuroMix API application ('"+default_mcra_url+"'): ")
print("URL of EuroMix API application: "+default_mcra_url+".")
if args.username is None:
print("MCRA user name:(%s)" % getpass.getuser())
......@@ -52,37 +52,27 @@ if args.keyring is None:
else:
mcrapassword = keyring.get_password(args.keyring, mcrausername)
mcra90Url = args.apiurl
# API call to list all current user's repositories
res = Mcra90ApiLib.getMcra90Result(mcra90Url, "Repositories/GetAll", mcrausername, mcrapassword)
parsed = json.loads(res)
print(json.dumps(parsed, indent=2, sort_keys=True))
api = McraApi.McraApi(
url=args.apiurl,
username=mcrausername,
password=mcrapassword)
# Mooi lijstje maken voor een makkelijk overzicht:
for x in parsed:
print('+++ REPOSITORIES +++++++++++++++++++++++++++++++++++++++++++++')
for x in api.get_repository():
print('{:>5} {:<25.25} {:>3} {:>3} {:>3} {:>3} {:>3} {:<25.25}'.format(
x['id'], x['name'], x['idOwner'], x['idParentRepository'],
x['isUserRootRepository'], x['repositoryType'],
x['userAccessLevel'], x['owner']))
# # API call to list the properties of a single user repository (example with id = 2)
# res = Mcra90ApiLib.getMcra90Result(mcra90Url, "Repositories/Get/2", mcrausername, mcrapassword)
# parsed = json.loads(res)
# print(json.dumps(parsed, indent=2, sort_keys=True))
# # API call to list the properties of a single data source (example with id = 50)
# res = Mcra90ApiLib.getMcra90Result(mcra90Url, "DataSources/Get/50", mcrausername, mcrapassword)
# datasource = json.loads(res)
# print(json.dumps(datasource, indent=2, sort_keys=True))
print('\n+++ DATASOURCES ++++++++++++++++++++++++++++++++++++++++++++')
# #API call to retrieve an originally uploaded data file from the server
# # example id = 50
# Mcra90ApiLib.getMcra90File(mcra90Url, "DataSources/DownloadVersion/50", mcrausername, mcrapassword, f"{datasource['name']}")
for x in api.get_datasource():
print('{:>5} {:<25.25} {:>25.25} {:>3} {:>3} {:>3} {:>3} {:<25.25}'.format(
x['id'], x['name'], x['createdTimeStamp'], x['version'], x['idCurrentVersion'], x['idDataSourceRepository'], x['idOwner'], x['owner']))
# #API call to retrieve a CSV file with the data from the database for this data source as a zipped collection of CSVs file
# Mcra90ApiLib.getMcra90File(mcra90Url, "DataSources/DownloadVersionCsv/50", mcrausername, mcrapassword, f"{datasource['name']}.zip")
print('\n+++ WORKSPACES +++++++++++++++++++++++++++++++++++++++++++++')
# input("Press any key to exit")
for x in api.get_workspace():
print('{:>5} {:<25.25} {:>25.25}'.format(
x['id'], x['name'], x['dateModified']))
#
import keyring
import urllib
import urllib.request
import urllib.parse
import json
#
#
#
class McraApi:
def __init__(self, url, username, password = None, keyring = None):
self.url = url
self.username = username
self.client_id = 'cbd00dfbdd0a4501bf457b52a635353f'
# If password is explicitly given, use that
if password is not None:
self.password = password
# But it is better to use the keyring!
elif keyring is not None:
self.password = keyring.get_password(self.keyring, self.username)
else:
raise Exception('No password or keyring given.')
def get_repository(self, id = None):
if id is None:
return self.__call_api__(apiCall = f'Repositories/GetAll')
else:
return self.__call_api__(apiCall = f'Repositories/Get/{id}')
def get_datasource(self, id = None):
if id is None:
return self.__call_api__(apiCall = f'DataSources/GetAll')
else:
return self.__call_api__(apiCall = f'DataSources/Get/{id}')
def get_workspace(self, id = None):
if id is None:
return self.__call_api__(apiCall = f'Workspace/GetAll')
else:
return self.__call_api__(apiCall = f'Workspace/Get/{id}')
def __get_token__(self):
# Code copied from MvL
tokenUrl = self.url + "/jwtauth/token"
tokenReqData = urllib.parse.urlencode({
"grant_type": "password",
"username": self.username,
"password": self.password,
"client_id": self.client_id
}).encode()
# send request with authorization header 'mcra-hmac ' + auth header value
tokenRequest = urllib.request.Request(tokenUrl, tokenReqData)
tokenRequest.add_header("content-type", "application/json")
tokenResponse = urllib.request.urlopen(tokenRequest)
#gets the token result in a json object {access_token, ...}
tokenResult = json.loads(tokenResponse.read())
return tokenResult["access_token"]
def __call_api__(self, apiCall, parser = 'json'):
# Code copied from MvL
jwtToken = self.__get_token__()
# api function to call
pUrl = f'{self.url}/Api/{apiCall}'
# get action settings xml from euromix web api using jwt token authorization
settingsRequest = urllib.request.Request(pUrl)
settingsRequest.add_header("authorization", "bearer " + jwtToken)
settingsResp = urllib.request.urlopen(settingsRequest)
result = settingsResp.read()
# Parse if necessary
if parser == 'json':
# Will parse json right away
return json.loads(result)
elif parser is None:
# Use if you want raw (binary?) result
return result
else:
return None
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment