-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added examples for Portal APIs using IAM authz
* This effectively support without needing setup PORTAL_TOKEN environment variable but using AWS CLI credential or IAM role. * Updated README and a couple of examples for possible backend and end user ad-hoc use case code snippet. * R example is still using Python for v4 signing facility and http `requests` package; through `reticulate` R library. This can be improved to pure R with `httr` and `cloudyr`. * Related to #415 #377
- Loading branch information
Showing
6 changed files
with
855 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# -*- coding: utf-8 -*- | ||
# Use Case: | ||
# Using Portal API without needing PORTAL_TOKEN but, just natively with your existing AWS CLI credentials. | ||
# | ||
# Note: | ||
# Instead of going through Python library with `reticulate`, we could do the _pure_ R approach with | ||
# just `aws.signature` and `httr`. | ||
# | ||
# Usage: | ||
# conda activate data-portal-apis | ||
# pip install requests-aws4auth | ||
# export AWS_PROFILE=prodops | ||
# Rscript portal_api_sig4.R | ||
|
||
depedencies <- list("reticulate", "aws.signature", "jsonlite", "testthat") | ||
invisible(lapply(depedencies, function(d) if (!require(d, character.only = TRUE)) install.packages(d, repos = "https://cran.ms.unimelb.edu.au"))) | ||
|
||
library(reticulate) | ||
library(aws.signature) | ||
library(jsonlite) | ||
library(testthat) | ||
|
||
print_dash <- function(times = 64) { | ||
cat(strrep("-", times)) | ||
cat("\n") | ||
} | ||
|
||
use_condaenv(condaenv = "data-portal-apis") | ||
py_config() | ||
|
||
print_dash() | ||
|
||
# --- | ||
|
||
py_awsauth <- import("requests_aws4auth") | ||
py_requests <- import("requests") | ||
region <- "ap-southeast-2" | ||
service <- "execute-api" | ||
|
||
credentials <- aws.signature::locate_credentials() | ||
|
||
authr <- py_awsauth$AWS4Auth( | ||
credentials$key, | ||
credentials$secret, | ||
region, | ||
service, | ||
session_token = credentials$session_token | ||
) | ||
|
||
url <- "https://api.data.prod.umccr.org/iam/lims" # using iam endpoint | ||
|
||
params <- reticulate::py_dict( | ||
c("rowsPerPage", "subject_id"), | ||
c("1000", "SBJ01651") | ||
) | ||
|
||
response <- py_requests$get(url, auth = authr, params = params) | ||
|
||
response_list <- response$text | ||
# response_list <- response$json() | ||
# typeof(response_list) | ||
# response_list | ||
|
||
response_df <- fromJSON(response_list) | ||
|
||
test_that("SBJ01651 should have only 3 samples/libraries", { | ||
expect_equal(response_df$pagination$count, 3) | ||
}) | ||
print_dash() | ||
|
||
results <- response_df$results | ||
results | ||
|
||
print_dash() | ||
|
||
cat("All SBJ01651 samples:\n\t") | ||
results$sample_id | ||
results$sample_id[1] | ||
results$sample_id[2] | ||
results$sample_id[3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// -*- coding: utf-8 -*- | ||
/** | ||
Usage: | ||
yarn add aws4-axios axios aws-sdk | ||
export AWS_PROFILE=prodops | ||
node portal_api_sig4.js | ||
**/ | ||
import axios from 'axios'; | ||
import AWS from 'aws-sdk'; | ||
import {aws4Interceptor} from 'aws4-axios'; | ||
|
||
// https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html | ||
const credentials = new AWS.SharedIniFileCredentials(); | ||
|
||
const interceptor = aws4Interceptor( | ||
{ | ||
region: 'ap-southeast-2', | ||
service: 'execute-api', | ||
}, | ||
credentials | ||
); | ||
|
||
axios.interceptors.request.use(interceptor); | ||
|
||
axios.get('https://api.data.prod.umccr.org/iam/lims').then((res) => { | ||
console.log(res.data) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Usage: | ||
pip install aws-requests-auth botocore | ||
export AWS_PROFILE=prodops | ||
python portal_api_sig4.py | ||
""" | ||
from urllib.parse import urlparse, ParseResult | ||
|
||
import requests | ||
from aws_requests_auth.boto_utils import BotoAWSRequestsAuth | ||
|
||
if __name__ == '__main__': | ||
obj: ParseResult = urlparse("https://api.data.prod.umccr.org/iam/lims") | ||
|
||
auth = BotoAWSRequestsAuth( | ||
aws_host=obj.hostname, | ||
aws_region="ap-southeast-2", | ||
aws_service="execute-api" | ||
) | ||
|
||
response = requests.get(obj.geturl(), auth=auth) | ||
|
||
resp_dict: dict = response.json() | ||
|
||
print(resp_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.