-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-job.ps1
81 lines (67 loc) · 2.85 KB
/
create-job.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
###################################
# create-job.ps1
###################################
if (-NOT (TEST-PATH -PATH $AUTH_FILE))
{
Write-Host 'Auth credentials file does not exist or this program does not have permission to access it'
exit
}
if ($AB2D_API_URL -like "*/v1" -And $UNTIL)
{
Write-Host 'The _until parameter is only available with version 2 (FHIR R4) of the API'
exit
}
$AUTH = Get-Content $AUTH_FILE
$AUTH = $AUTH.Trim()
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
function Get-Bearer-Token {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic $AUTH")
$headers.Add("Accept", "application/json")
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$response = Invoke-RestMethod "$OKTA_URI_WITH_PARAMS" -Method "POST" -Headers $headers -Body $body
if ($response.access_token) {
Write-Host 'Received access token that was not null'
}
return $response.access_token
}
# Set API variables
$OKTA_URI_WITH_PARAMS = "$AUTHENTICATION_URL`?grant_type=client_credentials&scope=clientCreds"
$EXPORT_URL = "$AB2D_API_URL/fhir/Patient/`$export`?_outputFormat=application%2Ffhir%2Bndjson&_type=ExplanationOfBenefit"
if ($SINCE)
{
$SINCE = $SINCE -replace ":", "%3A"
$EXPORT_URL = $EXPORT_URL + "&_since=" + $SINCE
}
if ($UNTIL)
{
$UNTIL = $UNTIL -replace ":", "%3A"
$EXPORT_URL = $EXPORT_URL + "&_until=" + $UNTIL
}
Write-Host '---------------------------------------------------------------------------------------------------------------------'
Write-Host 'The OKTA URI used for getting bearer token'
Write-Host '---------------------------------------------------------------------------------------------------------------------'
Write-Host $OKTA_URI_WITH_PARAMS
Write-Host ''
Write-Host '---------------------------------------------------------------------------------------------------------------------'
Write-Host 'The AB2D API endpoint for starting an export job'
Write-Host '---------------------------------------------------------------------------------------------------------------------'
Write-Host $EXPORT_URL
Write-Host ''
# Get initial bearer token
$BEARER_TOKEN = Get-Bearer-Token
# Create an export job
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Prefer", "respond-async")
$headers.Add("Authorization", "Bearer $BEARER_TOKEN")
$response = Invoke-WebRequest "$EXPORT_URL" -Method 'GET' -Headers $headers -Body $body
$STATUS_URL = $response.Headers['Content-Location']
if ($STATUS_URL)
{
Write-Host "Starting a job succeeded"
Write-Host "Saving the status url to use for monitoring $STATUS_URL"
Set-Content -Path 'status_url.txt' $STATUS_URL
} else {
Write-Host 'Starting a job failed'
}