# (DD09) Business Logic — CurrencyRateAPI.getCurrencyCode() [10 LOC]

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

## 1. Role

### CurrencyRateAPI.getCurrencyCode()

This method retrieves the available currency codes from the external exchange-rate service by calling the USD latest-rate endpoint and extracting the keys from the `conversion_rates` object. In business terms, it acts as a currency master discovery function: instead of hardcoding supported currencies, it dynamically asks the upstream API which foreign currency codes are currently available for conversion. The method follows a routing-and-extraction pattern: it first constructs the request URL, then delegates the HTTP/JSON retrieval to `getJsonObject()`, and finally transforms the returned JSON map into a `String[]` of currency codes. If the upstream response is missing, malformed, or does not contain any exchange-rate entries, the method returns `null` to indicate that no valid currency list could be established. This method is a shared API utility used by UI-side logic to populate currency selection behavior and keep the application aligned with the external provider’s supported currencies.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["getCurrencyCode()"])
    BUILD_URL["Build request URL with getURL() + /latest/USD"]
    CALL_JSON["Call getJsonObject(URL)"]
    JSON_OK{"jsonObject != null and has conversion_rates?"}
    GET_CODES["Read conversion_rates as JsonObject code"]
    CODES_OK{"code != null and keySet not empty?"}
    RETURN_CODES["Return code.keySet().toArray(new String[0])"]
    RETURN_NULL["Return null"]
    START --> BUILD_URL
    BUILD_URL --> CALL_JSON
    CALL_JSON --> JSON_OK
    JSON_OK -->|Yes| GET_CODES
    JSON_OK -->|No| RETURN_NULL
    GET_CODES --> CODES_OK
    CODES_OK -->|Yes| RETURN_CODES
    CODES_OK -->|No| RETURN_NULL
```

The method has no internal constant-based branching. The only fixed business selector is the request target `USD`, which is used as the base currency for the upstream latest-rate query.

## 3. Parameter Analysis

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

This method takes no parameters. Its processing depends on external instance methods and runtime state: `getURL()` for API base address construction, `getJsonObject(URL)` for HTTP retrieval and JSON parsing, and the loaded API key from `config.properties` via `getApiKeyService()`.

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `CurrencyRateAPI.getJsonObject` | CurrencyRateAPI | - | Calls `getJsonObject` in `CurrencyRateAPI` |
| R | `CurrencyRateAPI.getURL` | CurrencyRateAPI | - | Calls `getURL` in `CurrencyRateAPI` |

Analyze all method calls within this method and classify each as a CRUD operation.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `getURL` | CurrencyRateAPI | External exchange-rate API base URL | Builds the service endpoint prefix using the configured API key and upstream host |
| R | `getJsonObject` | CurrencyRateAPI | External exchange-rate API response | Sends a GET request to `/latest/USD` and parses the JSON payload |
| R | `has` | JsonObject | JSON field `conversion_rates` | Checks whether the response contains the exchange-rate map |
| R | `getAsJsonObject` | JsonObject | JSON field `conversion_rates` | Reads the nested exchange-rate object from the response |
| R | `keySet` | JsonObject | JSON keys under `conversion_rates` | Retrieves supported currency code keys from the exchange-rate map |
| R | `toArray` | String[] conversion | - | Converts the currency-code set into a string array for the caller |

## 5. Dependency Trace

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

No screen/batch entry points found within 8 hops. Direct callers found: 1 methods.
Terminal operations from this method: `getJsonObject` [R], `getURL` [R]

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | `UI` | `UI` -> `CurrencyRateAPI.getCurrencyCode` | `getJsonObject [R] External exchange-rate API response` |

The search results show one direct caller in `com.github.blaxk3.ui.UI`, where the method is invoked as `new CurrencyRateAPI().getCurrencyCode()`. No additional screen or batch entry points are visible within the available search scope, so this method currently behaves as a utility endpoint called from the UI layer.

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(request construction and dispatch)` (L61)

> Builds the upstream request to the exchange-rate service and delegates JSON retrieval.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `getURL()` |
| 2 | SET | `new java.net.URI(getURL() + "/latest/" + "USD")` |
| 3 | EXEC | `toURL()` |
| 4 | CALL | `getJsonObject(...)` |
| 5 | SET | `JsonObject jsonObject = ...` |

**Block 2** — [IF] `(jsonObject != null && jsonObject.has("conversion_rates"))` (L62)

> Verifies that the upstream response contains the exchange-rate map before extracting business data.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `jsonObject.has("conversion_rates")` |
| 2 | CALL | `jsonObject.getAsJsonObject("conversion_rates")` |
| 3 | SET | `JsonObject code = jsonObject.getAsJsonObject("conversion_rates")` |

**Block 2.1** — [IF] `(code != null && !code.keySet().isEmpty())` (L64)

> Ensures the exchange-rate map contains at least one supported currency code.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `code.keySet()` |
| 2 | EXEC | `code.keySet().isEmpty()` |
| 3 | RETURN | `return code.keySet().toArray(new String[0]);` |

**Block 2.2** — [ELSE] `(response missing or empty)` (L62-L68)

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return null;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|-------------------|
| `getCurrencyCode` | Method | Retrieves the list of supported foreign currency codes from the upstream exchange-rate provider |
| `getURL` | Method | Builds the base request URL for the exchange-rate API using the configured API key |
| `getJsonObject` | Method | Sends an HTTP GET request and parses the JSON response into a `JsonObject` |
| `conversion_rates` | JSON field | Exchange-rate map returned by the provider, with currency codes as keys and rate values as data |
| `USD` | Business term | United States Dollar, used here as the base currency for the latest-rate lookup |
| `API_KEY` | Configuration key | Secret credential used to authenticate against the exchange-rate service |
| `config.properties` | Configuration file | Application property file that stores the external API key |
| `currency code` | Business term | ISO-like code representing a supported currency such as USD, JPY, or EUR |
| `latest` | API resource | Upstream endpoint category that returns current exchange rates |
| `currency master discovery` | Business term | Dynamic retrieval of supported currencies for selection and conversion workflows |
| `UI` | Layer | User-interface layer that consumes this API method to populate selections or display supported currencies |
| `JsonObject` | Technical type | JSON container used to inspect the response structure from the external API |
| `String[]` | Technical type | Array used to return the discovered currency codes to the caller |
