# (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 the centralized configuration reader for the exchange-rate API integration. It opens the application classpath resource `config.properties`, loads the property set, and returns the `API_KEY` value used by downstream HTTP request construction. In business terms, it supplies the authentication credential required to call the external currency exchange service, so the rest of the API client can build a valid endpoint URL.

The method implements a simple read-and-return pattern with defensive fallback handling. If the configuration file cannot be located on the classpath, it writes an error to the application log and returns `null` instead of attempting any external call. If the file is present but loading fails due to an I/O problem, it logs the exception and also returns `null`. This makes the method a single-purpose credential retrieval utility rather than a stateful service.

Within the larger system, this method is an internal dependency of `getURL()`, which concatenates the returned key into the exchange-rate API base URL. Because the method has no parameters and no branching by business category, it does not dispatch across service types; instead, it acts as a configuration lookup point that decouples the hard-coded endpoint structure from the secret value stored in the deployment environment.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["getApiKeyService()"]
    INIT["Create Properties instance"]
    LOAD["Open classpath resource config.properties"]
    CHECK{"InputStream is null"}
    MISSING["Log missing config.properties and return null"]
    READ["Load properties from InputStream"]
    GETKEY["Read property API_KEY"]
    RETURNKEY["Return API_KEY value"]
    CATCH["Catch IOException"]
    LOGERR["Log load failure and return null"]
    END["End"]

    START --> INIT
    INIT --> LOAD
    LOAD --> CHECK
    CHECK -->|Yes| MISSING
    CHECK -->|No| READ
    READ --> GETKEY
    GETKEY --> RETURNKEY
    LOAD --> CATCH
    CATCH --> LOGERR
    MISSING --> END
    RETURNKEY --> END
    LOGERR --> END
```

## 3. Parameter Analysis

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

External state read by the method:
- `config.properties` from the application classpath
- Property key `API_KEY` within that file
- `logger` instance for error reporting

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `getResourceAsStream("config.properties")` | - | Classpath resource `config.properties` | Reads the application configuration file from packaged resources. |
| R | `properties.load(input)` | - | `java.util.Properties` in-memory configuration store | Loads key/value pairs from the stream into the local configuration object. |
| R | `properties.getProperty("API_KEY")` | - | `API_KEY` property | Retrieves the API credential used to authenticate currency-rate requests. |

## 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` | - |

Trace who calls this method and what this method ultimately calls.
Use the pre-computed evidence and caller search results from Step 2 above.

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

## 6. Per-Branch Detail Blocks

**Block 1** — [TRY] `(open config.properties from classpath)` (L23-L25)

> Opens the packaged configuration resource and prepares a local property container for credential lookup.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Properties properties = new Properties();` |
| 2 | EXEC | `getClass().getClassLoader().getResourceAsStream("config.properties")` // opens the classpath resource |

**Block 1.1** — [IF] `(input == null)` (L26)

> Handles the missing-configuration case before any property load is attempted.

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

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

> Loads the configuration values and extracts the API credential required by the currency-rate URL builder.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `properties.load(input);` // reads key/value pairs from the configuration stream |
| 2 | RETURN | `return properties.getProperty("API_KEY");` // returns the external API key |

**Block 1.3** — [CATCH] `(IOException)` (L31-L34)

> Handles file read failures and suppresses the exception by returning no credential.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `logger.error("Error loading config.properties", e);` // logs the load failure and exception detail |
| 2 | RETURN | `return null;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `config.properties` | Configuration file | Application resource file that stores runtime settings, including the exchange API credential. |
| `API_KEY` | Configuration key | Secret access token used to authenticate requests to the external currency exchange service. |
| `Properties` | Java utility | Key/value container used to load and query configuration entries. |
| `classpath` | Technical term | Runtime resource location that packages files inside the deployed application. |
| `InputStream` | Technical term | Stream abstraction used to read the configuration file contents. |
| `IOException` | Technical term | Read failure raised when the configuration resource cannot be loaded. |
| `logger` | Technical term | Logging facility used to record configuration and I/O errors. |
| Currency exchange API | Business term | External service used to retrieve foreign exchange rates and conversion results. |
| API key | Business term | Authentication credential that authorizes access to the exchange-rate provider. |
