# zekfad/nhentai_dart architecture

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

- Interactive diagram: https://gitdiagram.com/zekfad/nhentai_dart
- Repository: https://github.com/zekfad/nhentai_dart

## Overview

##### Project type and purpose
- This is a Dart client‐library (“package:nhentai”) that provides a typed wrapper over the undocumented nhentai.net REST API.
- It exposes a high‐level API for fetching “books,” images, tags, comments, users, etc., handling HTTP, JSON parsing, host configuration, proxies, and platform differences (native vs. web).

##### File structure
- Top‐level:
- pubspec.yaml, build.yaml → package metadata and build configuration
- lib/ → library source, subdivided into:
– before_request_add_cookies.dart, data_model.dart, nhentai.dart (facade API)
– src/api*.dart → API interface, RESTful implementation, exceptions
– src/hosts → host strategies (api_host, image_host, thumbnail_host, selected_hosts)
– src/models → domain models + mapper and hook extensions for JSON serialization
– src/platform → IO / JS / stub proxies
- example/ → sample usage illustrating how to import and call API methods.
- No backend, database, or UI components—this is pure client library.

##### README
- Describes features, usage, platform caveats (CORS, XHR limitations, proxy unsupported on web), and how to get a random book, search, override user‐agent, cookies, custom HTTP client injection.

##### Main components to surface
a. Consumer code (example.dart or any Dart/Flutter app)
b. Facade API (class API in nhentai.dart)
c. API core → ApiRestfulImplementation (makes HTTP calls)
d. HTTP client abstraction (package:http.Client or custom Client, or fetch_client on web)
e. Platform layer (IO vs JS implementations, proxy config, cookie helpers)
f. Hosts subsystem (ApiHost, ImageHost, ThumbnailHost, SelectedHosts) – constructs correct URIs for assets
g. Remote nhentai.net REST endpoints
h. JSON responses → Model mappers and hooks → Domain objects (Book, Tag, Image, SearchResult, etc.)
i. Consumer receives typed models and uses host helpers to build image URLs

##### Relationships and interactions
- Consumer → calls API methods → API forwards to ApiRestfulImplementation
- Implementation → uses HTTP client + host URIs → sends HTTP request to nhentai.net (or mirror)
- Response JSON → passed into mapper layer → instantiated Dart model objects
- Models may invoke hooks for custom parsing (dates, user avatars, encoded images)
- Consumer → inspects models → uses Hosts subsystem to get final image URIs
- All HTTP calls pass through Platform layer for proxy, user agent, cookies adaptation

##### Architectural patterns and design principles
- Facade pattern: `API` class hides internal details
- Strategy pattern: multiple Host strategies and Platform implementations selected at runtime
- Separation of concerns: HTTP + parsing + hosts + domain models → independent modules
- Dependency injection: custom `http.Client` can be plugged in
- Code generation / mapping: mapper classes (.mapper.dart) and hooks for JSON ↔ Dart model conversion

##### Technologies and frameworks
- Dart language
- package:http or package:fetch_client (web)
- Build runner/code‐generation for mappers (implied by build.yaml)
- Custom JSON mapping library via mapper_container_extension

##### Final advice
- Every layer: facade, core, adapters, mappers, hosts, external.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Consumer Layer
    Consumer["Consumer App\n(example)"]:::public
    click Consumer "https://github.com/zekfad/nhentai_dart/blob/master/example/example.dart"

    %% Public API Facade
    API["API Facade\nlib/nhentai.dart"]:::public
    click API "https://github.com/zekfad/nhentai_dart/blob/master/lib/nhentai.dart"

    %% API Core
    ApiInterface["API Interface\nlib/src/api.dart"]:::internal
    click ApiInterface "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/api.dart"
    ApiImpl["ApiRestfulImplementation\nlib/src/api_restful_implementation.dart"]:::internal
    click ApiImpl "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/api_restful_implementation.dart"
    ApiInterface --> ApiImpl

    %% HTTP Client & Exceptions
    HTTPClient["HTTP Client\npackage:http.Client or custom"]:::external
    ClientException["Client Exception\nlib/src/api_client_exception.dart"]:::internal
    click ClientException "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/api_client_exception.dart"
    GeneralException["API Exception\nlib/src/api_exception.dart"]:::internal
    click GeneralException "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/api_exception.dart"

    %% Hosts Subsystem
    subgraph "Hosts Subsystem"
        HostsFacade["Hosts Facade\nlib/src/hosts.dart"]:::public
        click HostsFacade "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/hosts.dart"
        APIHost["ApiHost\nlib/src/hosts/api_host.dart"]:::internal
        click APIHost "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/hosts/api_host.dart"
        ImageHost["ImageHost\nlib/src/hosts/image_host.dart"]:::internal
        click ImageHost "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/hosts/image_host.dart"
        ThumbHost["ThumbnailHost\nlib/src/hosts/thumbnail_host.dart"]:::internal
        click ThumbHost "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/hosts/thumbnail_host.dart"
        SelectedHosts["SelectedHosts\nlib/src/hosts/selected_hosts.dart"]:::internal
        click SelectedHosts "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/hosts/selected_hosts.dart"
        HostType["HostType\nlib/src/hosts/host_type.dart"]:::internal
        click HostType "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/hosts/host_type.dart"
        Host["Host Base\nlib/src/hosts/host.dart"]:::internal
        click Host "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/hosts/host.dart"
        HTTPHost["HTTPHost\nlib/src/hosts/http_host.dart"]:::internal
        click HTTPHost "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/hosts/http_host.dart"
    end

    %% Platform Adapters
    subgraph "Platform Adapters"
        PlatformFacade["Platform Facade\nlib/src/platform.dart"]:::adapter
        click PlatformFacade "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/platform.dart"
        IOPlat["IO Implementation\nlib/src/platform/io.dart"]:::adapter
        click IOPlat "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/platform/io.dart"
        JSPlat["JS Implementation\nlib/src/platform/js.dart"]:::adapter
        click JSPlat "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/platform/js.dart"
        StubPlat["Stub Implementation\nlib/src/platform/stub.dart"]:::adapter
        click StubPlat "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/platform/stub.dart"
        ProxyConfig["Proxy Config\nlib/src/platform/http_proxy_config.dart"]:::adapter
        click ProxyConfig "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/platform/http_proxy_config.dart"
    end

    %% Models & Mapping
    subgraph "Models & Mapping"
        ModelsInit["Models Init\nlib/src/models.init.dart"]:::internal
        click ModelsInit "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/models.init.dart"
        ModelsFacade["Models Facade\nlib/src/models.dart"]:::internal
        click ModelsFacade "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/models.dart"
        BookModel["Book Model\nlib/src/models/book.dart"]:::internal
        click BookModel "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/models/book.dart"
        BookMapper["Book Mapper\nlib/src/models/book.mapper.dart"]:::internal
        click BookMapper "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/models/book.mapper.dart"
        BookHook["Book Hook\nlib/src/models/book_hook.dart"]:::internal
        click BookHook "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/models/book_hook.dart"
        ImageModel["Image Model\nlib/src/models/images/image.dart"]:::internal
        click ImageModel "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/models/images/image.dart"
        ImageMapper["Images Mapper\nlib/src/models/images/images.mapper.dart"]:::internal
        click ImageMapper "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/models/images/images.mapper.dart"
        EncodeImageHook["Encode Image Hook\nlib/src/models/encode_image_hook.dart"]:::internal
        click EncodeImageHook "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/models/encode_image_hook.dart"
        MapperExtension["Mapper Extension\nlib/src/models/mapper_container_extension.dart"]:::internal
        click MapperExtension "https://github.com/zekfad/nhentai_dart/blob/master/lib/src/models/mapper_container_extension.dart"
    end

    %% Cookie Helper & Data Model Entry
    CookieHelper["Cookie Helper\nlib/before_request_add_cookies.dart"]:::internal
    click CookieHelper "https://github.com/zekfad/nhentai_dart/blob/master/lib/before_request_add_cookies.dart"
    DataModel["Data Model\nlib/data_model.dart"]:::internal
    click DataModel "https://github.com/zekfad/nhentai_dart/blob/master/lib/data_model.dart"

    %% External Service
    External["nhentai.net REST API"]:::external

    %% Connections
    Consumer -->|calls| API
    API -->|uses| ApiInterface
    ApiImpl -->|HTTP calls| HTTPClient
    ApiImpl -->|handles cookies| CookieHelper
    ApiImpl -->|selects URIs| HostsFacade
    ApiImpl -->|uses| PlatformFacade
    HTTPClient -->|error| ClientException
    HTTPClient -->|error| GeneralException
    HTTPClient -->|GET/POST| External
    External -->|JSON| ApiImpl
    ApiImpl -->|parses| ModelsFacade
    ModelsFacade -->|init| ModelsInit
    ModelsFacade -->|maps| BookMapper
    ModelsInit -->|register| MapperExtension
    BookMapper -->|hook| BookHook
    BookMapper -->|hook| EncodeImageHook
    ModelsFacade -->|produces| BookModel
    Consumer -->|build URLs| HostsFacade

    classDef public fill:#cce5ff,stroke:#4a90e2
    classDef internal fill:#d5f5e3,stroke:#27ae60
    classDef adapter fill:#fdebd0,stroke:#e67e22
    classDef external fill:#e0e0e0,stroke:#7f8c8d
```
