-
Notifications
You must be signed in to change notification settings - Fork 47
Springboot project structure with idempotency logic #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tparfaite
wants to merge
1
commit into
SheCanCODE-Capstone-Projects:main
Choose a base branch
from
Tparfaite:ft-implement-idempotency-gateway
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| target/ | ||
| .env | ||
| .ideal | ||
| *.iml | ||
| .vscode/ | ||
| .DS_Store | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>com.igirepay</groupId> | ||
| <artifactId>idempotency-gateway</artifactId> | ||
| <version>1.0.0</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <!-- This makes it a Spring Boot project --> | ||
| <parent> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-parent</artifactId> | ||
| <version>3.2.5</version> | ||
|
Tparfaite marked this conversation as resolved.
|
||
| <relativePath/> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <java.version>17</java.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <!-- Gives us REST API capability (like Flask in Python) --> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-web</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
|
Tparfaite marked this conversation as resolved.
|
||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-maven-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.igirepay; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
|
|
||
| @SpringBootApplication | ||
| public class Application { | ||
| public static void main(String[] args) { | ||
| SpringApplication.run(Application.class, args); | ||
| } | ||
|
|
||
| } |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/igirepay/controller/PaymentController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package com.igirepay.controller; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api") | ||
| public class PaymentController { | ||
| // Stores: idempotency_key -> saved response | ||
| private final ConcurrentHashMap<String, ResponseEntity<Map<String, Object>>> responseCache = new ConcurrentHashMap<>(); | ||
|
|
||
| // Stores: idempotency_key -> original request body (for conflict detection) | ||
| private final ConcurrentHashMap<String, Map<String, Object>> requestCache = new ConcurrentHashMap<>(); | ||
|
|
||
| // Stores: idempotency_key -> lock (for race condition handling) | ||
| private final ConcurrentHashMap<String, ReentrantLock> lockMap = new ConcurrentHashMap<>(); | ||
|
Tparfaite marked this conversation as resolved.
|
||
|
|
||
| @PostMapping("/process-payment") | ||
| public ResponseEntity<Map<String, Object>> processPayment( | ||
| @RequestHeader(value = "Idempotency-key", required = false) String idempotencyKey, | ||
| @RequestBody Map<String, Object> requestBody) throws InterruptedException { | ||
|
|
||
| // GUARD: Missing Idempotency-Key header | ||
| if (idempotencyKey == null || idempotencyKey.isBlank()) { | ||
| return ResponseEntity | ||
| .status(HttpStatus.BAD_REQUEST) | ||
| .body(Map.of("Error", "Missing Idempotency-key header")); | ||
| } | ||
|
|
||
| // Get or create a lock for this key | ||
| lockMap.putIfAbsent(idempotencyKey, new ReentrantLock()); | ||
| ReentrantLock lock = lockMap.get(idempotencyKey); | ||
|
|
||
| // Acquire the lock — blocks duplicate requests until we're done | ||
| lock.lock(); | ||
|
|
||
| try { | ||
| // Case 1: Key already exists | ||
| if(responseCache.containsKey(idempotencyKey)){ | ||
| //Check if request body is different | ||
| Map<String, Object> originalBody = requestCache.get(idempotencyKey); | ||
| if(!originalBody.equals(requestBody)) { | ||
| return ResponseEntity | ||
| .status(HttpStatus.CONFLICT) | ||
| .body(Map.of("error", "Idempotency key already used for different request body")); | ||
| } | ||
|
|
||
| //same body return cached response | ||
| ResponseEntity<Map<String,Object>> cached = responseCache.get(idempotencyKey); | ||
| return ResponseEntity | ||
| .status(cached.getStatusCode()) | ||
| .header("X-Cache-Hit", "true") | ||
| .body(cached.getBody()); | ||
| } | ||
|
|
||
| // Case 2: New request , process it | ||
| // save the request body first | ||
| requestCache.put(idempotencyKey, requestBody); | ||
|
|
||
| Thread.sleep(2000); | ||
|
|
||
| // EXtract amount and currency from request body | ||
| Object amount = requestBody.get("amount"); | ||
| Object currency = requestBody.get("currency"); | ||
|
Tparfaite marked this conversation as resolved.
|
||
|
|
||
| // Build the response | ||
| Map<String, Object> responseBody = Map.of( | ||
| "status", "success", | ||
| "message", String.format("Charged %s %s", amount, currency), | ||
| "idempotencyKey", idempotencyKey | ||
| ); | ||
|
|
||
| ResponseEntity<Map<String, Object>> response = ResponseEntity | ||
| .status(HttpStatus.CREATED) | ||
| .body(responseBody); | ||
| responseCache.put(idempotencyKey, response); | ||
|
|
||
| return response; | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| server.port=8080 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.