# jmojiwat/sparkpostfun architecture

> Architecture diagram of the GitHub repository jmojiwat/sparkpostfun, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jmojiwat/sparkpostfun
- Repository: https://github.com/jmojiwat/sparkpostfun

## Overview

This C# project is a library that provides a functional-style API client for SparkPost. From the file tree and README, the design clearly emphasizes:
- Functional programming (using LanguageExt) and immutability,
- A clear separation of concerns between different SparkPost API areas,
- Strong encapsulation of response handling through response wrappers,
- Robust testing (with dedicated test projects).

##### Main Components
- Client/Facade Layer: At the core, you have the SparkPostFun Client (in SparkPostFun/Client.cs) that serves as the entry point for making API calls.
- Domain Modules for API Areas: Separate modules exist for different aspects of the SparkPost API:
– Accounts: Handles operations such as account creation, update, usage, and subaccounts (see SparkPostFun/Accounts).
– Analytics: Manages metrics, campaign analyses, and other reporting functionalities (SparkPostFun/Analytics).
– Sending: Covers email transmission functionalities, including creating, retrieving, and updating transmissions, templates, and recipient lists (SparkPostFun/Sending).
– Receiving: Deals with inbound domains and relay webhooks (SparkPostFun/Receiving).
- Response Wrappers: A dedicated library under SparkPostFun.ResponseWrappers that provides extensions and specialized wrappers to handle API responses in a functional, monadic style.
- Infrastructure: A set of utility classes (in SparkPostFun/Infrastructure) for JSON serialization, naming policies, and extension methods that support the rest of the library.
- Test Projects: SparkPostFun.Tests and SparkPostFun.ResponseWrappers.Tests encapsulate unit and serialization tests to ensure behavior across the modules.

##### Relationships and Interactions
- The Client Layer interacts with the Domain Modules (Accounts, Analytics, Sending, Receiving) to build requests and handle responses.
- Each Domain Module encapsulates its own models and methods for creating requests (e.g., CreateSubaccount, CreateTransmission) and receiving responses.
- The Response Wrappers provide a consistent mechanism for wrapping the raw responses. They extend functionality to convert responses into the Task<Either<TError, TResponse>> monad.
- Infrastructure components (like custom JSON converters) support serialization/deserialization across these modules.
- Unit Tests interact with all major modules to validate both core business logic and serialization behaviors.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The project clearly divides the functionalities based on API areas.
- Functional Programming Style: The use of LanguageExt and monadic patterns (Task<Either<…>>) emphasizes immutability and pure function principles.
- Immutable Data and Extensions: The library uses extension methods to enhance client functionality without side effects.
- Robust Error Handling: Instead of throwing exceptions, the design returns error states in a controlled and typed manner.

## Diagram (Mermaid)

```mermaid
graph TD
    A["SparkPostFun Client"]:::client

    subgraph "Domain Modules"
        B["Accounts Module"]:::domain
        C["Analytics Module"]:::domain
        D["Sending Module"]:::domain
        E["Receiving Module"]:::domain
    end

    F["Response Wrappers"]:::response
    G["Infrastructure"]:::infra

    subgraph "Test Suite"
        I["Core Tests"]:::test
        J["Response Wrappers Tests"]:::test
    end

    %% Client calls Domain Modules
    A -->|"calls"| B
    A -->|"calls"| C
    A -->|"calls"| D
    A -->|"calls"| E

    %% Domain Modules use Response Wrappers and Infrastructure
    B -->|"wraps"| F
    C -->|"wraps"| F
    D -->|"wraps"| F
    E -->|"wraps"| F

    B -->|"supports"| G
    C -->|"supports"| G
    D -->|"supports"| G
    E -->|"supports"| G

    %% Test Suite interacts with components
    I -->|"tests"| A
    I -->|"tests"| B
    I -->|"tests"| C
    I -->|"tests"| D
    I -->|"tests"| E
    I -->|"tests"| G

    J -->|"tests"| F

    %% Styles
    classDef client fill:#f9c,stroke:#333,stroke-width:2px;
    classDef domain fill:#bbf,stroke:#333,stroke-width:2px;
    classDef response fill:#fdd,stroke:#333,stroke-width:2px;
    classDef infra fill:#dfd,stroke:#333,stroke-width:2px;
    classDef test fill:#ffd,stroke:#333,stroke-width:2px;

    %% Click Events
    click A "https://github.com/jmojiwat/sparkpostfun/blob/master/src/SparkPostFun/Client.cs"
    click B "https://github.com/jmojiwat/sparkpostfun/tree/master/src/SparkPostFun/Accounts"
    click C "https://github.com/jmojiwat/sparkpostfun/tree/master/src/SparkPostFun/Analytics"
    click D "https://github.com/jmojiwat/sparkpostfun/tree/master/src/SparkPostFun/Sending"
    click E "https://github.com/jmojiwat/sparkpostfun/tree/master/src/SparkPostFun/Receiving"
    click F "https://github.com/jmojiwat/sparkpostfun/blob/master/src/SparkPostFun.ResponseWrappers"
    click G "https://github.com/jmojiwat/sparkpostfun/tree/master/src/SparkPostFun/Infrastructure"
    click I "https://github.com/jmojiwat/sparkpostfun/blob/master/src/SparkPostFun.Tests"
    click J "https://github.com/jmojiwat/sparkpostfun/blob/master/src/SparkPostFun.ResponseWrappers.Tests"
```
