Skip to content

Commit

Permalink
Various changes
Browse files Browse the repository at this point in the history
  • Loading branch information
plasticuproject committed Jan 27, 2024
1 parent c2795a2 commit bc09a6c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Rust
name: Lint Build Deploy

on:
push:
Expand All @@ -11,15 +11,15 @@ env:

jobs:
build:
environment: rust
environment: Testing
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Lint
run: cargo clippy --verbose
- name: Build
run: cargo build --verbose
run: cargo build --release --verbose
- name: Run tests
env:
TAXII_USERNAME: ${{ secrets.TAXII_USERNAME }}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Lint Build Deploy](https://github.com/plasticuproject/cc-taxii2-client-rs/actions/workflows/rust.yml/badge.svg)](https://github.com/plasticuproject/cc-taxii2-client-rs/actions/workflows/rust.yml)

WIP.

Don't roast me too hard.
5 changes: 4 additions & 1 deletion examples/test-cc-taxii2-client-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {
}

// Print availible Collections for an account.
match agent.get_collections("api") {
match agent.get_collections(None) {
Ok(collections) => {
println!("collections: {:?}", collections);
}
Expand All @@ -34,6 +34,9 @@ fn main() {
match agent.get_cc_indicators(None, Some(5), false, None, &None, false) {
Ok(indicators) => {
//println!("indicators: {:?}", indicators);
for i in indicators.iter() {
println!("{:?}", i);
}
println!("{:?}", indicators.len());
}
Err(e) => {
Expand Down
14 changes: 7 additions & 7 deletions src/cctaxiiclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use ureq::{Agent, Response};
/// - `spec_version`: The TAXII specification version.
/// - `type`: The type of the `IoC` (e.g., "indicator").
/// - `valid_from`: The date from which the `IoC` is considered valid.
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct CCIndicator {
pub created: String,
Expand All @@ -55,7 +54,6 @@ pub struct CCIndicator {
/// - `more`: Indicates if more data is available (pagination).
/// - `next`: The URL for the next set of data, if `more` is `true`.
/// - `objects`: A collection of TAXII objects, each represented as a `HashMap<String, String>`.
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct CCEnvelope {
more: Option<bool>,
Expand Down Expand Up @@ -129,8 +127,10 @@ impl TaxiiClient for CCTaxiiClient {
.map_err(|e| Box::new(JsonDeserializationError(e.to_string())))
}

fn get_collections(&self, root: &str) -> Result<Vec<String>> {
let collections_endpoint = format!("{root}/collections/");
fn get_collections(&self, root: Option<&str>) -> Result<Vec<String>> {
let collections_root =
root.map_or_else(|| "api".to_string(), std::string::ToString::to_string);
let collections_endpoint = format!("{collections_root}/collections/");
let response = self.request(&collections_endpoint)?;
let collections: Collections = response
.into_json()
Expand Down Expand Up @@ -217,8 +217,8 @@ impl CCTaxiiClient {
let collection = match collection_id {
Some(id) => id.to_string(),
None => self
.get_collections(root)?
.get(0)
.get_collections(Some(root))?
.first()
.ok_or_else(|| {
Box::new(TaxiiCollectionError("No collections available".to_string()))
})?
Expand Down Expand Up @@ -288,7 +288,7 @@ mod tests {
let api_key = env::var("TAXII_API_KEY").expect("You've not set the TAXII_API_KEY");
let agent = CCTaxiiClient::new(&username, &api_key);
let collections = agent
.get_collections("api")
.get_collections(Some("api"))
.expect("Failed to get collections");
assert_eq!(collections.len(), 1);
}
Expand Down
17 changes: 7 additions & 10 deletions src/taxiiclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ pub trait TaxiiClient {
///
/// ```
/// let agent = TaxiiClient::new("my_username", "my_api_key");
/// let collections = agent.get_collections("api");
/// let collections = agent.get_collections(Some("api"));
/// ```
fn get_collections(&self, root: &str) -> Result<Vec<String>>;
fn get_collections(&self, root: Option<&str>) -> Result<Vec<String>>;
}

/// Represents a TAXII Envelope, used for wrapping TAXII objects.
Expand All @@ -142,7 +142,6 @@ pub trait TaxiiClient {
/// - `more`: Indicates if more data is available (pagination).
/// - `next`: The URL for the next set of data, if `more` is `true`.
/// - `objects`: A collection of TAXII objects, each represented as a `HashMap<String, String>`.
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct Envelope {
pub more: Option<bool>,
Expand All @@ -162,7 +161,6 @@ pub struct Envelope {
/// - `default`: The default API root for this server.
/// - `description`: A human-readable description of this server.
/// - `title`: A human-readable title for this server.
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct Discovery {
pub api_roots: Vec<String>,
Expand All @@ -187,12 +185,12 @@ pub struct Discovery {
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct Collection {
can_read: bool,
can_write: bool,
pub can_read: bool,
pub can_write: bool,
pub id: String,
media_types: [String; 1],
name: String,
title: String,
pub media_types: [String; 1],
pub name: String,
pub title: String,
}

/// A container for multiple `Collection` objects.
Expand All @@ -202,7 +200,6 @@ pub struct Collection {
/// # Fields
///
/// - `collections`: A vector of `Collection` structs.
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct Collections {
pub collections: Vec<Collection>,
Expand Down

0 comments on commit bc09a6c

Please sign in to comment.