---

# (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 a configuration access routine that resolves the external currency API key from the application's classpath resources. It creates a `Properties` container, opens `config.properties` through the class loader, and extracts the `API_KEY` entry that is needed to compose downstream exchange-rate service URLs. In business terms, it is the credential lookup step that enables the application to authenticate against the remote ExchangeRate API service.

The method implements a simple resource-loading and delegation pattern: it does not calculate the key itself, but delegates key storage to the configuration file and returns the resolved value to callers such as `getURL()`. It also acts as a fault boundary for missing configuration or I/O failures by logging the problem and returning `null` instead of throwing an exception. This makes it a shared support method for the API client flow, not a domain transaction handler.

The method has two failure branches and one success path. If `config.properties` cannot be found on the classpath, it records an error and returns `null`. If the properties file is found but loading fails with an `IOException`, it also logs the error and returns `null`. Otherwise, it returns the configured `API_KEY` value for use by the rest of the currency rate integration flow.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["getApiKeyService()"]
    INIT["Create Properties instance"]
    LOAD["Open config.properties from classpath"]
    CHECK{"InputStream is null?"}
    LOG_MISSING["Log error - Unable to find config.properties"]
    RETURN_NULL_1["Return null"]
    PROPS_LOAD["Load properties from InputStream"]
    GET_KEY["Read API_KEY from Properties"]
    RETURN_KEY["Return API_KEY value"]
    CATCH["Catch IOException"]
    LOG_IO["Log error - Error loading config.properties"]
    RETURN_NULL_2["Return null"]
    END_NODE["End"]

    START --> INIT
    INIT --> LOAD
    LOAD --> CHECK
    CHECK -->|Yes| LOG_MISSING
    LOG_MISSING --> RETURN_NULL_1
    RETURN_NULL_1 --> END_NODE
    CHECK -->|No| PROPS_LOAD
    PROPS_LOAD --> GET_KEY
    GET_KEY --> RETURN_KEY
    RETURN_KEY --> END_NODE
    LOAD -.-> CATCH
    CATCH --> LOG_IO
    LOG_IO --> RETURN_NULL_2
    RETURN_NULL_2 --> END_NODE
```

## 3. Parameter Analysis

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

External state read by the method:
- Classpath resource `config.properties`
- Property key `API_KEY`
- Class logger `logger` for operational error reporting

## 4. CRUD Operations / Called Services

This method does not perform direct CRUD operations against a database or repository. Its only functional dependency is configuration retrieval from a classpath properties file, which is an application resource access concern rather than a CRUD action.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `getResourceAsStream` | - | `config.properties` | Reads application configuration from the classpath to obtain the exchange-rate API credential |
| R | `properties.load` | - | `config.properties` | Loads key-value pairs from the configuration stream into memory |
| R | `properties.getProperty` | - | `API_KEY` | Retrieves the configured API key value used by the URL builder |
| - | `logger.error` | - | - | Writes operational diagnostics when configuration is missing or cannot be loaded |

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

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

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENTIAL] (L23-L26)

> Initializes the property container and attempts to resolve the configuration resource from the application classpath.

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

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

> Handles the missing-configuration case when the credential file is not packaged or not visible to the class loader.

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

**Block 3** — [SEQUENTIAL] `(input != null)` (L29-L30)

> Loads the configuration content into memory and resolves the API key value.

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

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

> Handles file read or stream parsing failure while loading the properties resource.

| # | 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 values such as the external API credential |
| `API_KEY` | Configuration key | Secret credential used to authenticate requests to the ExchangeRate API service |
| classpath | Technical term | Runtime resource lookup scope used by the Java class loader to find packaged files |
| `Properties` | Technical term | Java key-value container used to read and store configuration entries |
| `InputStream` | Technical term | Byte stream used to consume the configuration file contents |
| `logger` | Technical term | Application logger used to record operational failures for support and troubleshooting |
| ExchangeRate API | Business term | External service that provides currency exchange-rate data |
| currency rate integration | Business term | Application feature that builds exchange-rate API requests and consumes rate data from the external provider |
| credential | Business term | Secret value required for authorized access to a third-party service |
