Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MCRA.DataConversionTools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Biometris
MCRA.DataConversionTools
Commits
dfafd085
Commit
dfafd085
authored
5 years ago
by
Hans van den Heuvel
Browse files
Options
Downloads
Patches
Plain Diff
Converted part of Marco's script into objects.
parent
68263c3f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Download-MCRA/Download-MCRA.py
+18
-28
18 additions, 28 deletions
Download-MCRA/Download-MCRA.py
Download-MCRA/McraApi.py
+85
-0
85 additions, 0 deletions
Download-MCRA/McraApi.py
with
103 additions
and
28 deletions
Download-MCRA/Download-MCRA.py
+
18
−
28
View file @
dfafd085
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
'
]))
This diff is collapsed.
Click to expand it.
Download-MCRA/McraApi.py
+
85
−
0
View file @
dfafd085
#
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment