# peranetwork/viewstats-api architecture

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

- Interactive diagram: https://gitdiagram.com/peranetwork/viewstats-api
- Repository: https://github.com/peranetwork/viewstats-api

## Overview

##### Project type and purpose
- This is a language‐library (PHP) that provides a wrapper around an external REST API (ViewStats).
- Its purpose is to simplify authentication, request/response handling, decryption, and logging for consumers of the ViewStats service.

##### File structure
- Top‐level files: composer.json/lock (dependency config), README.md, .gitignore
- public/index.php – a minimal example / entrypoint (not part of core library)
- src/
– Exceptions/ (APIException, DecryptionException)
– Logger.php (log writer)
– ViewStatsAPI.php (main façade)

##### README for insights
- Core components mentioned: authentication, GET/POST methods, error handling, decryption (AES-256-GCM), logging (debug.log).
- External dependency: cURL + OpenSSL, external ViewStats REST endpoints.

##### Main components & interactions
a. Consumer Application
– Any PHP app that invokes the ViewStatsAPI class via Composer autoload.
b. ViewStats API Wrapper
– ViewStatsAPI façade
- Builds HTTP requests (method, path, headers with API token)
- Calls the HTTP Client (cURL)
- Feeds response to the DecryptionHandler
- Throws Exceptions on HTTP or decryption errors
– DecryptionHandler (inside ViewStatsAPI or helper)
– Error Handling (Exceptions under src/Exceptions)
– Logger (writes info, errors to debug.log)
c. External ViewStats REST API
– HTTPS endpoints for channels, stats, rankings, etc.
d. Filesystem
– debug.log in project root

##### Architectural patterns & design principles
- Façade pattern: ViewStatsAPI hides low‐level HTTP, auth, decryption.
- Single Responsibility:
– Logger only writes logs
– Exceptions only represent errors
– ViewStatsAPI orchestrates calls
- Dependency inversion: consumers depend on the high‐level API, not on cURL directly.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Project Metadata
    subgraph "Project Files"
        composer["composer.json"] 
        composerLock["composer.lock"]
        readme["README.md"]
        gitignore[".gitignore"]
    end

    %% Consumer Application
    ConsumerApp["Consumer Application"]:::consumer

    %% Wrapper Layer
    subgraph "ViewStats API Wrapper"
        ViewStatsFacade["ViewStatsAPI Facade"]:::internal
        RequestBuilder["Request Builder"]:::internal
        HTTPClient["HTTP Client (cURL)"]:::internal
        ResponseDecryption["Response Decryption"]:::internal
        subgraph "Error Handling"
            APIException["APIException"]:::internal
            DecryptionException["DecryptionException"]:::internal
        end
        LoggerNode["Logger"]:::internal
    end

    %% External System
    ExternalAPI["External ViewStats REST API"]:::external

    %% Persistence
    DebugLog["debug.log (Filesystem)"]:::persistence

    %% Connections
    ConsumerApp -->|"calls get()/post()"| ViewStatsFacade
    ViewStatsFacade -->|"builds request"| RequestBuilder
    RequestBuilder -->|"sends request"| HTTPClient
    HTTPClient -->|"HTTPS request"| ExternalAPI
    ExternalAPI -->|"HTTPS response"| HTTPClient
    HTTPClient -->|"raw response"| ResponseDecryption
    ResponseDecryption -->|"decrypted JSON payload"| ViewStatsFacade
    ViewStatsFacade -->|"returns data"| ConsumerApp
    ViewStatsFacade -->|"log(info/error)"| LoggerNode
    HTTPClient -->|"HTTP error"| APIException
    ResponseDecryption -->|"decryption error"| DecryptionException
    APIException -->|"log(error)"| LoggerNode
    DecryptionException -->|"log(error)"| LoggerNode
    LoggerNode -->|"writes log entry"| DebugLog

    %% Click Events
    click ConsumerApp "https://github.com/peranetwork/viewstats-api/blob/main/public/index.php"
    click ViewStatsFacade "https://github.com/peranetwork/viewstats-api/blob/main/src/ViewStatsAPI.php"
    click LoggerNode "https://github.com/peranetwork/viewstats-api/blob/main/src/Logger.php"
    click APIException "https://github.com/peranetwork/viewstats-api/blob/main/src/Exceptions/APIException.php"
    click DecryptionException "https://github.com/peranetwork/viewstats-api/blob/main/src/Exceptions/DecryptionException.php"
    click composer "https://github.com/peranetwork/viewstats-api/blob/main/composer.json"
    click composerLock "https://github.com/peranetwork/viewstats-api/blob/main/composer.lock"
    click readme "https://github.com/peranetwork/viewstats-api/blob/main/README.md"
    click gitignore "https://github.com/peranetwork/viewstats-api/blob/main/.gitignore"

    %% Styles
    classDef consumer fill:#add8e6,stroke:#000,stroke-width:1px
    classDef internal fill:#90ee90,stroke:#000,stroke-width:1px
    classDef external fill:#ffa500,stroke:#000,stroke-width:1px
    classDef persistence fill:#d3d3d3,stroke:#000,stroke-width:1px
    classDef metadata fill:#f0f0f0,stroke:#000,stroke-width:1px
```
