Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate request history for different collections (closes #10) #22

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## [Unreleased]

### Added

- Added top-level collection `id` field
- Needed in order to give each collection its own history file

### Fixed

- Differentiate history between different collections [#10](https://github.com/LucasPickering/slumber/issues/10)

## [0.4.0] - 2023-11-02

### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Slumber is based around **collections**. A collection is a group of request **re

```yaml
# slumber.yml
id: example
requests:
- id: get
method: GET
Expand Down
17 changes: 12 additions & 5 deletions docs/src/api/request_collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ Whichever of those files is found _first_ will be used. If you want to use a dif
slumber -c my-collection.yml
```

## Collection ID

Each collection needs a unique ID (via the `id` field). This ID is used to tie the collection to its history. If the ID of a collection changes, you'll lose the history for it. If two collections share an ID, their request history could start interfering with each other. Make sure each collection used on your computer is unique.

## Fields

A request collection supports the following top-level fields:

| Field | Type | Description | Default |
| ---------- | -------------------------------------------- | ------------------------- | ------- |
| `profiles` | [`list[Profile]`](./profile.md) | Static template values | [] |
| `requests` | [`list[RequestRecipe]`](./request_recipe.md) | Requests Slumber can send | [] |
| `chains` | [`list[Chain]`](./chain.md) | Complex template values | [] |
| Field | Type | Description | Default |
| ---------- | -------------------------------------------- | ----------------------------- | -------- |
| `id` | `string` | Unique ID for this collection | Required |
| `profiles` | [`list[Profile]`](./profile.md) | Static template values | [] |
| `requests` | [`list[RequestRecipe]`](./request_recipe.md) | Requests Slumber can send | [] |
| `chains` | [`list[Chain]`](./chain.md) | Complex template values | [] |

## Examples

```yaml
id: example

profiles:
- id: local
name: Local
Expand Down
2 changes: 2 additions & 0 deletions slumber.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
id: example

profiles:
- id: works
name: Works
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Subcommand {
)?;

// Build the request
let repository = Repository::load()?;
let repository = Repository::load(&collection.id)?;
let overrides: IndexMap<_, _> = overrides.into_iter().collect();
let request = RequestBuilder::new(
recipe,
Expand Down
2 changes: 2 additions & 0 deletions src/config/insomnia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use indexmap::IndexMap;
use serde::Deserialize;
use std::{fs::File, path::Path};
use tracing::info;
use uuid::Uuid;

impl RequestCollection<()> {
/// Convert an Insomnia exported collection into the slumber format. This
Expand Down Expand Up @@ -54,6 +55,7 @@ impl RequestCollection<()> {

Ok(RequestCollection {
source: (),
id: Uuid::new_v4().to_string().into(),
profiles,
recipes,
chains: Vec::new(),
Expand Down
8 changes: 8 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub struct RequestCollection<S = PathBuf> {
#[serde(skip)]
source: S,

/// Unique ID for this collection. This should be unique for across all
/// collections used on one computer.
pub id: CollectionId,
#[serde(default)]
pub profiles: Vec<Profile>,
#[serde(default)]
Expand All @@ -41,6 +44,11 @@ pub struct RequestCollection<S = PathBuf> {
pub recipes: Vec<RequestRecipe>,
}

/// A unique ID for a collection. This is necessary to differentiate between
/// responses from different collections in the repository.
#[derive(Clone, Debug, Display, From, Serialize, Deserialize)]
pub struct CollectionId(String);

/// Mutually exclusive hot-swappable config group
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Profile {
Expand Down
12 changes: 7 additions & 5 deletions src/http/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! caching and other ephemeral data (e.g. prettified content).

use crate::{
config::RequestRecipeId,
config::{CollectionId, RequestRecipeId},
http::{Request, RequestId, RequestRecord, Response},
util::{data_directory, ResultExt},
};
Expand Down Expand Up @@ -47,8 +47,10 @@ pub struct Repository {
impl Repository {
/// Load the repository database. This will perform first-time setup, so
/// this should only be called at the main session entrypoint.
pub fn load() -> anyhow::Result<Self> {
let mut connection = Connection::open(Self::path())?;
///
/// Each collection gets its own
pub fn load(collection_id: &CollectionId) -> anyhow::Result<Self> {
let mut connection = Connection::open(Self::path(collection_id))?;
// Use WAL for concurrency
connection.pragma_update(None, "journal_mode", "WAL")?;
Self::setup(&mut connection)?;
Expand All @@ -58,8 +60,8 @@ impl Repository {
}

/// Path to the repository database file
fn path() -> PathBuf {
data_directory().join("repository.sqlite")
fn path(collection_id: &CollectionId) -> PathBuf {
data_directory().join(format!("{collection_id}.sqlite"))
}

/// Apply first-time setup
Expand Down
2 changes: 1 addition & 1 deletion src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Tui {
let messages_tx = MessageSender::new(messages_tx);

let view = View::new(&collection, messages_tx.clone());
let repository = Repository::load().unwrap();
let repository = Repository::load(&collection.id).unwrap();
let app = Tui {
terminal,
messages_rx,
Expand Down
Loading