# (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()

`getApiKeyService()` is a configuration lookup method that retrieves the external ExchangeRate API credential from the application's classpath configuration. Its business role is to centralize API key resolution so that the rest of the currency-rate integration can build a valid request URL without hard-coding secrets into source code. This method implements a simple resource-loading and property-resolution pattern: it opens `config.properties`, loads key-value pairs into a `Properties` object, and extracts the `API_KEY` entry. If the configuration file is missing or cannot be read, it records an error and returns `null`, allowing the caller to detect a non-configured or broken runtime environment. In the larger system, this method acts as a shared utility for the API endpoint construction flow, providing a single place where the external service credential is obtained. The method has two failure branches: one for a missing resource stream and one for an `IOException` during file loading; both branches stop the lookup and return no key.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["getApiKeyService()"]
    INIT["Create Properties object"]
    LOAD["Open config.properties from classpath"]
    CHECK{ "InputStream is null?" }
    LOG_MISSING["Log unable to find config.properties"]
    RETURN_NULL_1["Return null"]
    PROPERTIES_LOAD["Load properties from stream"]
    GET_API_KEY["Read API_KEY property from config.properties"]
    TRY_END["Close InputStream automatically"]
    CATCH_IO["Catch IOException"]
    LOG_IO["Log error loading config.properties"]
    RETURN_NULL_2["Return null"]
    END_NODE["Return API key string"]

    START --> INIT
    INIT --> LOAD
    LOAD --> CHECK
    CHECK -->|yes| LOG_MISSING
    LOG_MISSING --> RETURN_NULL_1
    CHECK -->|no| PROPERTIES_LOAD
    PROPERTIES_LOAD --> GET_API_KEY
    GET_API_KEY --> TRY_END
    TRY_END --> END_NODE
    LOAD --> CATCH_IO
    CATCH_IO --> LOG_IO
    LOG_IO --> RETURN_NULL_2
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method does not accept input parameters. It derives its behavior entirely from application configuration available on the classpath and from the `config.properties` resource. |

Instance fields and external state read by this method:
- `logger` — used to record configuration-loading failures.
- Classpath resource `config.properties` — contains the `API_KEY` entry.
- JDK `ClassLoader` resource resolution — determines whether the configuration file is available at runtime.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `getProperty("API_KEY")` | - | `config.properties` | Reads the API key value from the application configuration file so the ExchangeRate API URL can be built with a runtime secret. |

## 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` | `getProperty("API_KEY") [R] config.properties` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] (L23-L35)

> Reads the external API credential from a classpath properties file and returns it to the URL builder.

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

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

> Handles the missing-configuration case when the properties file is not packaged or not visible on the classpath.

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

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

> Loads the properties file and extracts the API key used by the currency exchange integration.

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

**Block 2** — [CATCH] `(IOException e)` (L29-L33)

> Handles file-read or property-load failures by logging the exception and returning no API key.

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

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `API_KEY` | Field / Config key | External service credential used to authenticate requests to the ExchangeRate API. |
| `config.properties` | Configuration file | Classpath properties file that stores runtime settings for the application, including the external API key. |
| `classpath` | Technical term | Runtime resource lookup scope that allows the application to load packaged configuration files. |
| `Properties` | Technical term | Java key-value container used to read configuration entries from a `.properties` file. |
| `InputStream` | Technical term | Stream handle for reading the configuration file content from the classpath. |
| `logger` | Technical term | Application logging facility used to record missing file and I/O errors. |
| ExchangeRate API | Business term | External currency exchange-rate service whose endpoint requires an API key. |
| API key | Business term | Secret token that authorizes use of the external exchange-rate service. |
