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

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

## 1. Role

### CurrencyRateAPI.getCurrencyCode()

This method acts as a lightweight currency metadata lookup routine that fetches the latest foreign exchange payload from the external currency API, extracts the available currency codes from the response, and returns them as a `String[]`. In business terms, it provides the list of supported quotation currencies that downstream UI or service logic can present to the user when a currency selection is required.

The method follows a routing/delegation pattern: it delegates HTTP response retrieval to `getJsonObject(...)` and endpoint assembly to `getURL()`, then interprets the `conversion_rates` section of the returned JSON. If the payload contains a non-empty `conversion_rates` object, the method returns every key in that object as a currency code list. If the response is missing, malformed, or the key set is empty, the method returns `null` to indicate that no usable currency master data could be resolved.

From a system perspective, this is a shared integration helper used by the presentation layer rather than a transactional business service. It hides the external API shape from callers and normalizes the response into a simple array of currency identifiers. The method does not create, update, or delete persistent business data; it only performs read-style extraction from an external service response.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["getCurrencyCode()"])
    CALL_GETJSON(["Call getJsonObject(getURL() + /latest/USD)"])
    IF_JSON{"jsonObject != null and has conversion_rates"}
    SET_CODE(["code = jsonObject.getAsJsonObject(conversion_rates)"])
    IF_KEYS{"code != null and keySet not empty"}
    RETURN_CODES(["Return code.keySet() as String[]"])
    RETURN_NULL(["Return null"])

    START --> CALL_GETJSON
    CALL_GETJSON --> IF_JSON
    IF_JSON -->|Yes| SET_CODE
    IF_JSON -->|No| RETURN_NULL
    SET_CODE --> IF_KEYS
    IF_KEYS -->|Yes| RETURN_CODES
    IF_KEYS -->|No| RETURN_NULL
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method accepts no input parameters. Its behavior is fully determined by the external API endpoint returned by `getURL()`, the HTTP response retrieved through `getJsonObject(...)`, and the JSON structure of the response body. |

External state read by the method:
- `getURL()` result from the same `CurrencyRateAPI` instance
- JSON payload returned by the external currency service
- The `conversion_rates` object within that payload

## 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 | External currency API response | Calls `getJsonObject` to fetch the latest exchange-rate JSON payload |
| R | `CurrencyRateAPI.getURL` | CurrencyRateAPI | External currency API base URL | Resolves the API base URL used to construct the latest-rates endpoint |

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 currency API base URL | Resolves the service endpoint used for the latest currency-rate query |
| R | `getJsonObject` | CurrencyRateAPI | External currency API response | Retrieves the JSON document from `/latest/USD` |

## 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: `getURL` [R], `getJsonObject` [R]

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

## 6. Per-Branch Detail Blocks

**Block 1** — `CALL` (resolve latest exchange-rate payload) (L61)

> This block builds the external endpoint for the latest USD-based exchange-rate response and requests the JSON document.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `getURL()` // resolve API base URL |
| 2 | EXEC | `new java.net.URI(getURL() + "/latest/" + "USD").toURL()` // build latest-rates endpoint |
| 3 | CALL | `getJsonObject(...)` // fetch response as JSON |

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

> This block verifies that the API returned a usable response and that the response contains the currency-rate map.

| # | Type | Code |
|---|------|------|
| 1 | IF | `if (jsonObject != null && jsonObject.has("conversion_rates"))` |

**Block 2.1** — `EXEC` `(extract conversion_rates object)` (L63)

> The method narrows the JSON payload to the `conversion_rates` object for further validation.

| # | Type | Code |
|---|------|------|
| 1 | SET | `JsonObject code = jsonObject.getAsJsonObject("conversion_rates");` // isolate supported currency map |

**Block 3** — `IF` `(code != null and !code.keySet().isEmpty())` (L64)

> This block confirms that at least one supported currency code is available before returning it to the caller.

| # | Type | Code |
|---|------|------|
| 1 | IF | `if (code != null && !code.keySet().isEmpty())` |

**Block 3.1** — `RETURN` `(supported currency codes)` (L65)

> Returns the currency identifiers as a plain string array for UI consumption.

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

**Block 4** — `RETURN` `(fallback)` (L68)

> If the payload is missing, malformed, or empty, the method returns `null` to indicate that no currency list could be derived.

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

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `CurrencyRateAPI` | Class | API integration component that communicates with an external currency exchange-rate service |
| `getCurrencyCode` | Method | Reads the latest exchange-rate payload and returns the set of supported currency codes |
| `getURL` | Method | Resolves the base URL of the external currency-rate API |
| `getJsonObject` | Method | Executes an HTTP request and parses the response into a JSON object |
| `conversion_rates` | JSON field | Map of supported currency codes to their exchange-rate values in the API response |
| `String[]` | Return type | Array representation of supported currency codes for downstream selection logic |
| USD | Business term | United States Dollar, used here as the base currency for the latest-rate lookup |
| currency code | Business term | Standard three-letter ISO currency identifier such as USD, JPY, or EUR |
| API | Acronym | Application Programming Interface — an external service endpoint consumed by the application |
| JSON | Acronym | JavaScript Object Notation — structured payload format returned by the currency service |
