-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for middleware (#308)
* feat: add support for middleware * feat: add middleware logic and test * feat: fixes based on comments * fix: nits fix * fix: interal function improve * test: isolate middlewareRunner test from amplitude client
- Loading branch information
1 parent
841f9ce
commit 781a3d0
Showing
8 changed files
with
289 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,5 @@ | ||
package com.amplitude.api; | ||
|
||
public interface Middleware { | ||
void run(MiddlewarePayload payload, MiddlewareNext next); | ||
} |
This file contains 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,7 @@ | ||
package com.amplitude.api; | ||
|
||
import org.json.JSONObject; | ||
|
||
public class MiddlewareExtra extends JSONObject { | ||
|
||
} |
This file contains 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,5 @@ | ||
package com.amplitude.api; | ||
|
||
public interface MiddlewareNext { | ||
public void run(MiddlewarePayload curPayload); | ||
} |
This file contains 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,17 @@ | ||
package com.amplitude.api; | ||
|
||
import org.json.JSONObject; | ||
|
||
public class MiddlewarePayload { | ||
public JSONObject event; | ||
public MiddlewareExtra extra; | ||
|
||
public MiddlewarePayload(JSONObject event, MiddlewareExtra extra) { | ||
this.event = event; | ||
this.extra = extra; | ||
} | ||
|
||
public MiddlewarePayload(JSONObject event) { | ||
this(event, null); | ||
} | ||
} |
This file contains 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,48 @@ | ||
package com.amplitude.api; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class MiddlewareRunner { | ||
private final ConcurrentLinkedQueue<Middleware> middlewares; | ||
|
||
public MiddlewareRunner() { | ||
middlewares = new ConcurrentLinkedQueue<>(); | ||
} | ||
|
||
public void add(Middleware middleware) { | ||
this.middlewares.add(middleware); | ||
} | ||
|
||
private void runMiddlewares(List<Middleware> middlewares, MiddlewarePayload payload, MiddlewareNext next) { | ||
if (middlewares.size() == 0 ){ | ||
next.run(payload); | ||
return; | ||
} | ||
middlewares.get(0).run(payload, new MiddlewareNext() { | ||
@Override | ||
public void run(MiddlewarePayload curPayload) { | ||
runMiddlewares((middlewares.subList(1, middlewares.size())), curPayload, next); | ||
} | ||
}); | ||
} | ||
|
||
public boolean run(MiddlewarePayload payload) { | ||
AtomicBoolean middlewareCompleted = new AtomicBoolean(false); | ||
this.run(payload, new MiddlewareNext() { | ||
@Override | ||
public void run(MiddlewarePayload curPayload) { | ||
middlewareCompleted.set(true); | ||
} | ||
}); | ||
return middlewareCompleted.get(); | ||
} | ||
|
||
public void run(MiddlewarePayload payload, MiddlewareNext next) { | ||
List<Middleware> middlewareList = new ArrayList<>(this.middlewares); | ||
runMiddlewares(middlewareList, payload, next); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.