|
| 1 | +/* |
| 2 | + * Copyright 2013-2024, Seqera Labs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package nextflow.datasource |
| 18 | + |
| 19 | +import groovy.json.JsonSlurper |
| 20 | +import groovy.transform.CompileStatic |
| 21 | +import groovy.util.logging.Slf4j |
| 22 | +import nextflow.Global |
| 23 | +import nextflow.Session |
| 24 | +import nextflow.exception.AbortOperationException |
| 25 | +import nextflow.util.SimpleHttpClient |
| 26 | + |
| 27 | +/** |
| 28 | + * Download datasets from Seqera Platform |
| 29 | + * |
| 30 | + * @author Edmund Miller |
| 31 | + */ |
| 32 | +@Slf4j |
| 33 | +@CompileStatic |
| 34 | +class DatasetExplorer { |
| 35 | + |
| 36 | + static public Map PARAMS = [ |
| 37 | + endpoint: String, |
| 38 | + version: String, |
| 39 | + fileName: String |
| 40 | + ] |
| 41 | + |
| 42 | + private String datasetId |
| 43 | + private String endpoint |
| 44 | + private String version |
| 45 | + private String fileName |
| 46 | + private String accessToken |
| 47 | + private JsonSlurper jsonSlurper = new JsonSlurper() |
| 48 | + |
| 49 | + DatasetExplorer() { |
| 50 | + } |
| 51 | + |
| 52 | + DatasetExplorer(String datasetId, Map opts) { |
| 53 | + this.datasetId = datasetId |
| 54 | + init(opts) |
| 55 | + } |
| 56 | + |
| 57 | + DatasetExplorer setDatasetId(String datasetId) { |
| 58 | + this.datasetId = datasetId |
| 59 | + return this |
| 60 | + } |
| 61 | + |
| 62 | + protected void init(Map opts) { |
| 63 | + this.endpoint = opts.endpoint as String ?: getConfigEndpoint() |
| 64 | + this.version = opts.version as String ?: '1' |
| 65 | + this.fileName = opts.fileName as String |
| 66 | + } |
| 67 | + |
| 68 | + protected Map getEnv() { |
| 69 | + System.getenv() |
| 70 | + } |
| 71 | + |
| 72 | + protected String getConfigEndpoint() { |
| 73 | + def session = Global.session as Session |
| 74 | + def result = session?.config?.navigate('tower.endpoint') |
| 75 | + if (!result) |
| 76 | + result = 'https://api.tower.nf' |
| 77 | + return result as String |
| 78 | + } |
| 79 | + |
| 80 | + protected String getAccessToken() { |
| 81 | + def session = Global.session as Session |
| 82 | + def token = session?.config?.navigate('tower.accessToken') |
| 83 | + if (!token) |
| 84 | + token = getEnv().get('TOWER_ACCESS_TOKEN') |
| 85 | + if (!token) |
| 86 | + throw new AbortOperationException("Missing Seqera Platform access token -- Make sure there's a variable TOWER_ACCESS_TOKEN in your environment or tower.accessToken in your config") |
| 87 | + return token as String |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Fetch dataset metadata to determine the fileName if not provided |
| 92 | + * TODO: Implement this when the list-datasets API is available |
| 93 | + */ |
| 94 | + protected String getDatasetFileName() { |
| 95 | + if (fileName) |
| 96 | + return fileName |
| 97 | + |
| 98 | + // TODO: In the future, we can query the dataset metadata to get the fileName |
| 99 | + // For now, we'll use a default pattern or require the user to provide it |
| 100 | + throw new AbortOperationException("fileName parameter is required for fromDataset(). Future versions will support automatic detection.") |
| 101 | + } |
| 102 | + |
| 103 | + protected String getDownloadUrl() { |
| 104 | + final name = getDatasetFileName() |
| 105 | + return "${endpoint}/datasets/${datasetId}/v/${version}/n/${URLEncoder.encode(name, "UTF-8")}" |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Download the dataset and return its content |
| 110 | + */ |
| 111 | + String apply() { |
| 112 | + if (!accessToken) |
| 113 | + accessToken = getAccessToken() |
| 114 | + |
| 115 | + final url = getDownloadUrl() |
| 116 | + log.debug "Fetching dataset from: $url" |
| 117 | + |
| 118 | + try { |
| 119 | + final client = new SimpleHttpClient() |
| 120 | + client.setAuthToken("Bearer ${accessToken}") |
| 121 | + |
| 122 | + // Make HTTP GET request |
| 123 | + final connection = new URL(url).openConnection() as HttpURLConnection |
| 124 | + connection.setRequestMethod('GET') |
| 125 | + connection.setRequestProperty('Authorization', "Bearer ${accessToken}") |
| 126 | + connection.setRequestProperty('Accept', 'text/csv, text/plain, */*') |
| 127 | + |
| 128 | + final responseCode = connection.responseCode |
| 129 | + |
| 130 | + if (responseCode == 200) { |
| 131 | + final content = connection.inputStream.text |
| 132 | + log.trace "Dataset content received:\n${content?.take(500)}" |
| 133 | + return content |
| 134 | + } |
| 135 | + else if (responseCode == 403) { |
| 136 | + throw new AbortOperationException("Access forbidden to dataset ${datasetId} -- Check your permissions and access token") |
| 137 | + } |
| 138 | + else if (responseCode == 404) { |
| 139 | + throw new AbortOperationException("Dataset ${datasetId} not found -- Check the dataset ID, version, and fileName") |
| 140 | + } |
| 141 | + else { |
| 142 | + final errorMsg = connection.errorStream?.text ?: "HTTP ${responseCode}" |
| 143 | + throw new AbortOperationException("Failed to download dataset ${datasetId}: ${errorMsg}") |
| 144 | + } |
| 145 | + } |
| 146 | + catch (AbortOperationException e) { |
| 147 | + throw e |
| 148 | + } |
| 149 | + catch (Exception e) { |
| 150 | + throw new AbortOperationException("Error downloading dataset ${datasetId}: ${e.message}", e) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments