Design a library to invoke REST calls to external systems

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-11-28

Problem Statement

Design a library/module that allows an application to interact with external REST APIs in a reusable, configurable, and robust way. The library should provide HTTP request abstraction, error handling, retries, and logging, while being thread-safe and extensible.


Functional Requirements (FR)


1.Request Support
•Support all HTTP methods: GET, POST, PUT, DELETE, PATCH.
•Allow sending request headers, query parameters, and body payloads.
2.Response Handling
•Return structured responses (status code, headers, body).
•Deserialize JSON/XML responses into objects.
•Handle different HTTP status codes (success, client error, server error).
3.Retries & Timeouts
•Support configurable retry policies for transient failures.
•Support request timeouts and connection timeouts.
4.Error Handling
•Throw meaningful exceptions for network failures, timeouts, and invalid responses.
•Provide hooks for custom error handling.
5.Authentication & Security
•Support API keys, OAuth tokens, or other authentication mechanisms.
•Optional support for TLS/SSL configuration.
6.Asynchronous Calls
•Support async calls using futures, promises, or callbacks.
•Optionally support batching of requests.
7.Logging & Metrics
•Log request/response info for debugging.
•Provide metrics: success/failure counts, response times.
8.Configuration & Extensibility
•Configure base URLs, default headers, retry policies, and timeouts.
•Allow adding custom interceptors (e.g., for logging or authentication).


Non-Functional Requirements (NFR)


•Thread Safety – Support concurrent requests safely.
•High Performance – Low overhead for request execution.
•Extensibility – Easy to extend for new authentication types or serialization formats.
•Reliability – Robust handling of transient network failures.
•Ease of Use – Provide a simple and consistent API for developers.


Core Classes & Attributes


Class Key Attributes / Methods


RestClient baseUrl, defaultHeaders, timeout, retryPolicy, sendRequest(request)

HttpRequest method, url, headers, queryParams, body

HttpResponse statusCode, headers, body, parseJson()

RetryPolicy maxRetries, backoffStrategy, shouldRetry(response/exception)

Authenticator authenticate(request) – supports API key, OAuth, etc.

Interceptor preRequest(request), postResponse(response) – for logging or custom logic


APIs / Operations


•send(request: HttpRequest) -> HttpResponse
•sendAsync(request: HttpRequest) -> Future<HttpResponse>
•addInterceptor(interceptor: Interceptor)
•setAuthenticator(authenticator: Authenticator)
•setRetryPolicy(policy: RetryPolicy)
•setTimeouts(connectTimeout, readTimeout)


Expectations in LLD


•Class Diagram – Show RestClient, HttpRequest, HttpResponse, RetryPolicy, Authenticator, Interceptor classes and relationships.
•Service Logic –
•How requests are built, sent, retried, and responses parsed.
•How async calls are handled safely.
•Design Patterns –
•Builder pattern for constructing HttpRequest.
•Strategy pattern for RetryPolicy and Authenticator.
•Interceptor/Observer pattern for logging and metrics.
•Concurrency Considerations –
•Thread-safe connection handling.
•Safe async response processing.
•Trade-offs –
•Synchronous vs asynchronous execution.
•Retry attempts vs latency.
•Logging verbosity vs performance overhead.


✅ This LLD problem tests:


•OOP and modular class design skills
•Handling concurrency, retries, and error scenarios
•Extensibility for multiple authentication and serialization mechanisms


Solution

Comments