# (DD08) Business Logic — CurrencyRateAPI.getApiKeyService() [14 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `com.github.blaxk3.api.CurrencyRateAPI` |
| Layer | Utility |
| Module | `api` (Package: `com.github.blaxk3.api`) |

## 1. Role

### CurrencyRateAPI.getApiKeyService()

This method is responsible for resolving the ExchangeRate API authentication key from the application classpath at runtime. It acts as a small configuration access routine rather than a domain transaction service: the method opens `config.properties`, loads key-value pairs, and returns the value stored under `API_KEY`. In business terms, it provides the secret credential required for outbound foreign-exchange lookups so that higher-level API methods can build a complete request URL.

The method implements a straightforward configuration-loading and fail-fast pattern. If the configuration file cannot be found, it logs an error and returns `null`; if the file exists but cannot be read, it also logs an error and returns `null`. There are no business branches beyond these two failure conditions, and there is no retry, fallback source, or alternate credential resolution path.

Within the larger system, this method is a shared dependency for the currency-rate integration flow. It is called by `getURL()`, which composes the base ExchangeRate API endpoint and is then used by other public methods such as currency list retrieval and conversion. Because this method centralizes credential lookup, it isolates the rest of the API client from direct file access and keeps the API key resolution logic in one place.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["getApiKeyService()"]
    INIT["Create Properties instance"]
    LOAD["Open config.properties from classpath"]
    NULL_CHECK{"Input stream is null?"}
    LOG_MISSING["Log missing config.properties"]
    RETURN_NULL_1["Return null"]
    LOAD_PROPS["Load properties from stream"]
    GET_KEY["Read API_KEY property"]
    RETURN_KEY["Return API_KEY value"]
    CATCH_IO["Catch IOException"]
    LOG_ERROR["Log properties load error"]
    RETURN_NULL_2["Return null"]

    START --> INIT
    INIT --> LOAD
    LOAD --> NULL_CHECK
    NULL_CHECK -- "Yes" --> LOG_MISSING
    LOG_MISSING --> RETURN_NULL_1
    NULL_CHECK -- "No" --> LOAD_PROPS
    LOAD_PROPS --> GET_KEY
    GET_KEY --> RETURN_KEY
    LOAD_PROPS --> CATCH_IO
    CATCH_IO --> LOG_ERROR
    LOG_ERROR --> RETURN_NULL_2
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | - |

**External state read by the method**
- `config.properties` on the application classpath: runtime configuration file that stores external integration settings.
- `API_KEY` property: the credential used to authenticate requests to the ExchangeRate API.
- `logger`: shared class-level logger used for operational error reporting.

## 4. CRUD Operations / Called Services

This method does not perform database CRUD operations and does not call service-component methods. Its only operational actions are configuration file access, property loading, and logging.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `getResourceAsStream("config.properties")` | - | `config.properties` | Reads the application configuration resource from the classpath to obtain API credentials. |
| R | `properties.load(input)` | - | `config.properties` | Loads key-value pairs from the configuration file into memory for later lookup. |
| R | `properties.getProperty("API_KEY")` | - | `API_KEY` property | Retrieves the external API credential required by the currency exchange client. |

## 5. Dependency Trace

### Pre-computed evidence from code analysis graph:

| # | Caller (Screen/Batch) | Call Chain (Full Path to Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|----------------------------------|-------------------------------|
| 1 | Controller:CurrencyRateAPI | `CurrencyRateAPI.getURL` -> `CurrencyRateAPI.getApiKeyService` | `config.properties / API_KEY [R]` |

Trace who calls this method and what this method ultimately calls.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Controller:CurrencyRateAPI | `CurrencyRateAPI.getURL` -> `CurrencyRateAPI.getApiKeyService` | `properties.load [R] config.properties`, `properties.getProperty [R] API_KEY` |

## 6. Per-Branch Detail Blocks

**Block 1** — [TRY] `(load API key from classpath properties)` (L24-L31)

> This block initializes property storage and attempts to read the runtime configuration file that contains the ExchangeRate credential.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Properties properties = new Properties();` |
| 2 | EXEC | `getClass().getClassLoader().getResourceAsStream("config.properties")` |
| 3 | SET | `InputStream input = ...` |

**Block 1.1** — [IF] `(input == null)` (L25-L27)

> Handles the operational failure case where the configuration file is missing from the classpath.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `logger.error("Unable to find config.properties");` |
| 2 | RETURN | `return null;` |

**Block 1.2** — [ELSE] `(input != null)` (L28-L30)

> Loads the configuration file and extracts the API key value used by the outbound exchange-rate integration.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `properties.load(input);` |
| 2 | RETURN | `return properties.getProperty("API_KEY");` |

**Block 1.3** — [CATCH IOException] `(properties.load(input) fails)` (L31-L33)

> Handles read/parse errors while loading the configuration stream.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `logger.error("Error loading config.properties", e);` |
| 2 | RETURN | `return null;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `config.properties` | Configuration file | Classpath-based application settings file that stores runtime integration parameters, including the API credential. |
| `API_KEY` | Configuration property | Secret authentication token required to access the ExchangeRate API service. |
| `Properties` | Java utility class | Key-value container used to load and retrieve application configuration settings. |
| `classpath` | Technical term | Runtime resource location where packaged configuration files are resolved. |
| `logger` | Technical term | Operational logging facility used to record missing-file and load-failure events. |
| `ExchangeRate API` | External service | Third-party currency exchange service used for rate lookup and conversion. |
| `currency rate` | Business term | Exchange value between two currencies used for conversion calculations. |
| `runtime credential` | Business term | Secret value loaded at execution time instead of being hard-coded in source code. |
