Skip to content

Commit

Permalink
added function create_task from autogenerated, trying some stuff for …
Browse files Browse the repository at this point in the history
…service.rs
  • Loading branch information
aaravm committed Jun 7, 2024
1 parent 4560fc4 commit 4210498
Show file tree
Hide file tree
Showing 24 changed files with 318 additions and 35 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ license = "Apache-2.0"
repository = "https://github.com/elixir-cloud-aai/ga4gh-sdk.git"

[dependencies]
serde = "^1.0"
serde_derive = "^1.0"
serde_json = "^1.0"
url = "^2.2"
uuid = { version = "^1.0", features = ["serde", "v4"] }
[dependencies.reqwest]
version = "^0.11"
features = ["json", "multipart"]

[lib]
name = "my_project"
Expand Down
21 changes: 10 additions & 11 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! This is the main library file for the `lib` crate.
#![warn(missing_docs)]
#![allow(unused_imports)]

#[macro_use]
extern crate serde_derive;

// UNIT TESTING
#[cfg(test)]
mod tests {
// use super::*;
extern crate serde;
extern crate serde_json;
extern crate url;
extern crate reqwest;

#[test]
fn test() {
// This test currently does nothing, it is just a placeholder.
}
}

pub mod service;
pub mod tes;
77 changes: 77 additions & 0 deletions lib/src/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use reqwest::Client;
use serde::Serialize;
use serde_json::Value;
use std::error::Error;

#[derive(Debug)]
pub struct Service {
base_url: String,
client: Client,
username: Option<String>,
password: Option<String>,
token: Option<String>,
}

impl Service {
pub fn new(base_url: String, username: Option<String>, password: Option<String>, token: Option<String>) -> Self {
Service {
base_url,
client: Client::new(),
username,
password,
token,
}
}

pub async fn request(
&self,
method: reqwest::Method,
endpoint: &str,
data: Option<Value>,
params: Option<Value>,
) -> Result<Value, Box<dyn Error>> {
let url = format!("{}/{}", self.base_url, endpoint);
let mut headers = reqwest::header::HeaderMap::new();
headers.insert("Content-Type", "application/json".parse()?);

if let Some(token) = &self.token {
headers.insert(
"Authorization",
format!("Bearer {}", token).parse()?,
);
}

let mut req_builder = self.client.request(method, &url).headers(headers);

if let Some(data) = data {
req_builder = req_builder.json(&data);
}

if let Some(params) = params {
req_builder = req_builder.query(&params);
}

let response = req_builder.send().await?;

if !response.status().is_success() {
let error_message = format!("Error: HTTP {} - {}", response.status(), response.status().canonical_reason().unwrap_or("Unknown error"));
eprintln!("{}", error_message);
return Err(error_message.into());
}

let content_type = response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.unwrap_or("");

let response_data = if content_type.contains("application/json") {
response.json::<Value>().await?
} else {
Value::String(response.text().await?)
};

Ok(response_data)
}

}
53 changes: 53 additions & 0 deletions lib/src/tes/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Task Execution Service
*
* ## Executive Summary The Task Execution Service (TES) API is a standardized schema and API for describing and executing batch execution tasks. A task defines a set of input files, a set of containers and commands to run, a set of output files and some other logging and metadata. TES servers accept task documents and execute them asynchronously on available compute resources. A TES server could be built on top of a traditional HPC queuing system, such as Grid Engine, Slurm or cloud style compute systems such as AWS Batch or Kubernetes. ## Introduction This document describes the TES API and provides details on the specific endpoints, request formats, and responses. It is intended to provide key information for developers of TES-compatible services as well as clients that will call these TES services. Use cases include: - Deploying existing workflow engines on new infrastructure. Workflow engines such as CWL-Tes and Cromwell have extentions for using TES. This will allow a system engineer to deploy them onto a new infrastructure using a job scheduling system not previously supported by the engine. - Developing a custom workflow management system. This API provides a common interface to asynchronous batch processing capabilities. A developer can write new tools against this interface and expect them to work using a variety of backend solutions that all support the same specification. ## Standards The TES API specification is written in OpenAPI and embodies a RESTful service philosophy. It uses JSON in requests and responses and standard HTTP/HTTPS for information transport. HTTPS should be used rather than plain HTTP except for testing or internal-only purposes. ### Authentication and Authorization Is is envisaged that most TES API instances will require users to authenticate to use the endpoints. However, the decision if authentication is required should be taken by TES API implementers. If authentication is required, we recommend that TES implementations use an OAuth2 bearer token, although they can choose other mechanisms if appropriate. Checking that a user is authorized to submit TES requests is a responsibility of TES implementations. ### CORS If TES API implementation is to be used by another website or domain it must implement Cross Origin Resource Sharing (CORS). Please refer to https://w3id.org/ga4gh/product-approval-support/cors for more information about GA4GH’s recommendations and how to implement CORS.
*
* The version of the OpenAPI document: 1.1.0
*
* Generated by: https://openapi-generator.tech
*/



#[derive(Debug, Clone)]
pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub client: reqwest::Client,
pub basic_auth: Option<BasicAuth>,
pub oauth_access_token: Option<String>,
pub bearer_access_token: Option<String>,
pub api_key: Option<ApiKey>,
// TODO: take an oauth2 token source, similar to the go one
}

pub type BasicAuth = (String, Option<String>);

#[derive(Debug, Clone)]
pub struct ApiKey {
pub prefix: Option<String>,
pub key: String,
}


impl Configuration {
pub fn new() -> Configuration {
Configuration::default()
}
}

impl Default for Configuration {
fn default() -> Self {
Configuration {
base_path: "/ga4gh/tes/v1".to_owned(),
user_agent: Some("OpenAPI-Generator/1.1.0/rust".to_owned()),
client: reqwest::Client::new(),
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
api_key: None,

}
}
}
95 changes: 95 additions & 0 deletions lib/src/tes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use std::error;
use std::fmt;

#[derive(Debug, Clone)]
pub struct ResponseContent<T> {
pub status: reqwest::StatusCode,
pub content: String,
pub entity: Option<T>,
}

#[derive(Debug)]
pub enum Error<T> {
Reqwest(reqwest::Error),
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent<T>),
}

impl <T> fmt::Display for Error<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (module, e) = match self {
Error::Reqwest(e) => ("reqwest", e.to_string()),
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
};
write!(f, "error in {}: {}", module, e)
}
}

impl <T: fmt::Debug> error::Error for Error<T> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match self {
Error::Reqwest(e) => e,
Error::Serde(e) => e,
Error::Io(e) => e,
Error::ResponseError(_) => return None,
})
}
}

impl <T> From<reqwest::Error> for Error<T> {
fn from(e: reqwest::Error) -> Self {
Error::Reqwest(e)
}
}

impl <T> From<serde_json::Error> for Error<T> {
fn from(e: serde_json::Error) -> Self {
Error::Serde(e)
}
}

impl <T> From<std::io::Error> for Error<T> {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}

pub fn urlencode<T: AsRef<str>>(s: T) -> String {
::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
}

pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
if let serde_json::Value::Object(object) = value {
let mut params = vec![];

for (key, value) in object {
match value {
serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
&format!("{}[{}]", prefix, key),
value,
)),
serde_json::Value::Array(array) => {
for (i, value) in array.iter().enumerate() {
params.append(&mut parse_deep_object(
&format!("{}[{}][{}]", prefix, key, i),
value,
));
}
},
serde_json::Value::String(s) => params.push((format!("{}[{}]", prefix, key), s.clone())),
_ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
}
}

return params;
}

unimplemented!("Only objects are supported with style=deepObject")
}

pub mod tes;
pub mod configuration;
pub mod models;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// Service : GA4GH service
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// ServiceOrganization : Organization providing the service
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* Task Execution Service
*
* ## Executive Summary The Task Execution Service (TES) API is a standardized schema and API for describing and executing batch execution tasks. A task defines a set of input files, a set of containers and commands to run, a set of output files and some other logging and metadata. TES servers accept task documents and execute them asynchronously on available compute resources. A TES server could be built on top of a traditional HPC queuing system, such as Grid Engine, Slurm or cloud style compute systems such as AWS Batch or Kubernetes. ## Introduction This document describes the TES API and provides details on the specific endpoints, request formats, and responses. It is intended to provide key information for developers of TES-compatible services as well as clients that will call these TES services. Use cases include: - Deploying existing workflow engines on new infrastructure. Workflow engines such as CWL-Tes and Cromwell have extentions for using TES. This will allow a system engineer to deploy them onto a new infrastructure using a job scheduling system not previously supported by the engine. - Developing a custom workflow management system. This API provides a common interface to asynchronous batch processing capabilities. A developer can write new tools against this interface and expect them to work using a variety of backend solutions that all support the same specification. ## Standards The TES API specification is written in OpenAPI and embodies a RESTful service philosophy. It uses JSON in requests and responses and standard HTTP/HTTPS for information transport. HTTPS should be used rather than plain HTTP except for testing or internal-only purposes. ### Authentication and Authorization Is is envisaged that most TES API instances will require users to authenticate to use the endpoints. However, the decision if authentication is required should be taken by TES API implementers. If authentication is required, we recommend that TES implementations use an OAuth2 bearer token, although they can choose other mechanisms if appropriate. Checking that a user is authorized to submit TES requests is a responsibility of TES implementations. ### CORS If TES API implementation is to be used by another website or domain it must implement Cross Origin Resource Sharing (CORS). Please refer to https://w3id.org/ga4gh/product-approval-support/cors for more information about GA4GH’s recommendations and how to implement CORS.
* ## Executive Summary The Task Execution Service (TES) API is a standardized schema and API for describing and executing batch execution tasks. A task defines a set of input files, a set of containers and commands to run, a set of output files and some other logging and metadata. TES servers accept task documents and execute them asynchronously on available compute resources. A TES server could be built on top of a traditional HPC queuing system, such as Grid Engine, Slurm or cloud style compute systems such as AWS Batch or Kubernetes. ## Introduction This document describes the TES API and provides details on the specific endpoints, request formats, and responses. It is intended to provide key information for developers of TES-compatible services as well as clients that will call these TES services. Use cases include: - Deploying existing workflow engines on new infrastructure. Workflow engines such as CWL-Tes and Cromwell have extentions for using TES. This will allow a system engineer to deploy them onto a new infrastructure using a job scheduling system not previously supported by the engine. - Developing a custom workflow management system. This API provides a common interface to asynchronous batch processing capabilities. A developer can write new tools against this interface and expect them to work using a variety of backend solutions that all support the same specification. ## Standards The TES API specification is written in OpenAPI and embodies a RESTful service philosophy. It uses JSON in requests and responses and standard HTTP/HTTPS for information transport. HTTPS should be used rather than plain HTTP except for testing or internal-only purposes. ### Authentication and Authorization Is is envisaged that most TES API instances will require users to authenticate to use the endpoints. However, the decision if authentication is required should be taken by TES API implementers. If authentication is required, we recommend that TES implementations use an OAuth2 bearer token, although they can choose other mechanisms if appropriate. Checking that a user is authorized to submit TES requests is a responsibility of TES implementations. ### CORS If TES API implementation is to be used by another website or domain it must implement Cross Origin Resource Sharing (CORS). Please refer to https://w3id.org/ga4gh/product-approval-support/cors for more information about GA4GH’s recommendations and how to implement CORS.
*
* The version of the OpenAPI document: 1.1.0
*
*
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// ServiceType : Type of a GA4GH service
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand All @@ -34,4 +34,3 @@ impl ServiceType {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// TesCreateTaskResponse : CreateTaskResponse describes a response from the CreateTask endpoint. It will include the task ID that can be used to look up the status of the job.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// TesExecutor : Executor describes a command to be executed, and its environment.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// TesExecutorLog : ExecutorLog describes logging information related to an Executor.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// TesFileType : Define if input/output element is a file or a directory. It is not required that the user provide this value, but it is required that the server fill in the value once the information is avalible at run time.
/// Define if input/output element is a file or a directory. It is not required that the user provide this value, but it is required that the server fill in the value once the information is avalible at run time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// TesInput : Input describes Task input files.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// TesListTasksResponse : ListTasksResponse describes a response from the ListTasks endpoint.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// TesOutput : Output describes Task output files.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// TesOutputFileLog : OutputFileLog describes a single output file. This describes file details after the task has completed successfully, for logging purposes.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use crate::tes::models;

/// TesResources : Resources describes the resources requested by a task.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Loading

0 comments on commit 4210498

Please sign in to comment.