A minimal file server-client based on Akka Http making Http client calls to its counter part file_server. The project has been built keeping in mind, a service that can be built within approx 4-5 hours.
GET /api/client/get-or-create/{{requestId}} which
- retrieves the value of a resource from the upstream service (server) as soon as it becomes available or with a delay of max 500ms
- stores the value in a file
- no longer calls the server if a resources is available in a local file and responds with the value from that file
- client service should throttle requests for a specific resource (as per server rate limit - never more than 2 requests / second / resource)
- The endpoint responds immediately with either
Output: A: HTTP response code 202 (Accepted) if the resource value is not yet available or B: {"requestId”: "{{S1}}", "fileContent": "{{S2}}"} - if the resource is available in a local file
- Idiomatic approach, using custom code than Akka streams.
- Akka HTTP based application exposing a single route with method get /api/server/get-or-create/{{requestId}}.
- Use Concurrent Hashmap to manage throttle keep track of request and resource, in case
- Store file in a central location.
- Throttling will be based on server request time.
- RequestId is used to identify unique resource.
- When a user calls the API, a file is created in a central location and empty response for a unique requestId with 202 status code is returned.
- When a user calls the API with previous requestId, file is found in central location and fileContent is returned with 200 status code.
- When a user calls the API and error occurs in the server, an empty response with 500 (Internal error) status code is returned.
You need to download and install sbt for this application to run.
Once you have sbt installed, type/run the following command in the terminal:
Pre-requisite
- Please change central-directory to a proper directory where the file is to be saved, we are not force creating directories at this point. file_store{ central-directory = "C:\centralDirClient" default-extension = "txt" }
sbt run
or
Run via IDEUrl: http://127.0.0.1:9090/api/client/get-or-create/testRequest1
###Limitation:
- Throttle is based on server processing time (Reason- Server takes a min 5s, which would lead to multiple retries call to the server).
- Max retry is currently not set and will keep on retrying after 500ms. (Can be easily extended to add maxRetries)
###Extensions:
- Throttle can be extended by adding timestamp check when fetching from cache.
- Unit and Integrations tests can be better used for edge cases.
###Important - For Testing To run Unit test:
sbt test
or
Run test via IDETo run Integration test: Prerequisites:
- File-Server should be online or will throw 500 error.
- File-Client-Server should be online or will throw 500 error.
- Using single conf file so testing will also create file in the same central directory (will not work if central dir value is wrong in config.)
- File will be created and deleted after IT is completed. (File name: "t_e_s_t_file_dont_use.txt")
- Both services should have different file location.
sbt it:test
or
Run it test via IDE