Skip to content

Commit

Permalink
[EDGEPATRON-132]-Added GET edge api for LOC patron (#114)
Browse files Browse the repository at this point in the history
* [EDGEPATRON-132]-Added GET edge api for LOC patron

* [EDGEPATRON-132]-Added GET edge api for LOC patron
  • Loading branch information
SinghAdes authored Jun 10, 2024
1 parent 4bc6bac commit 9d755bf
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
46 changes: 46 additions & 0 deletions ramls/edge-patron.raml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,52 @@ types:
body:
text/plain:
example: internal server error, contact administrator
/by-email/{emailId}:
displayName: Get Accounts By email
description: Service endpoints that manage accounts by an existing email
uriParameters:
emailId:
description: The email of external patron
type: string
get:
description: Return account details for the specified external patron email
queryParameters:
apikey:
description: "API Key"
type: string
responses:
200:
description: Returns the external patron account info
body:
application/json:
type: external_patron
example: !include examples/external_patron.json
400:
description: Bad request
body:
text/plain:
example: unable to process request -- constraint violation
401:
description: Not authorized to perform requested action
body:
text/plain:
example: unable to get account -- unauthorized
404:
description: Item with a given EMAIL not found
body:
text/plain:
example: account not found
403:
description: Access Denied
body:
text/plain:
example: Access Denied
500:
description: Internal server error, e.g. due to misconfiguration
body:
text/plain:
example: internal server error, contact administrator

/{id}:
displayName: Manage Accounts By Id
description: Service endpoints that manage accounts by an existing Id
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/folio/edge/patron/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Constants {
public static final String PARAM_ITEM_ID = "itemId";
public static final String PARAM_INSTANCE_ID = "instanceId";
public static final String PARAM_HOLD_ID = "holdId";
public static final String PARAM_EMAIL_ID = "emailId";
public static final String PARAM_REQUEST_ID = "requestId";

public static final String MSG_ACCESS_DENIED = "Access Denied";
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/folio/edge/patron/MainVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public Router defineRoutes() {
router.route(HttpMethod.GET, "/patron/account/:patronId")
.handler(patronHandler::handleGetAccount);

router.route(HttpMethod.GET, "/patron/account/:patronId/by-email/:emailId")
.handler(patronHandler::handleGetExtPatronAccountByEmail);

router.route(HttpMethod.POST, "/patron/account/:patronId/item/:itemId/renew")
.handler(patronHandler::handleRenew);

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/folio/edge/patron/PatronHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.folio.edge.patron.Constants.MSG_HOLD_NOBODY;
import static org.folio.edge.patron.Constants.MSG_INTERNAL_SERVER_ERROR;
import static org.folio.edge.patron.Constants.MSG_REQUEST_TIMEOUT;
import static org.folio.edge.patron.Constants.PARAM_EMAIL_ID;
import static org.folio.edge.patron.Constants.PARAM_HOLD_ID;
import static org.folio.edge.patron.Constants.PARAM_INCLUDE_CHARGES;
import static org.folio.edge.patron.Constants.PARAM_INCLUDE_HOLDS;
Expand Down Expand Up @@ -137,6 +138,17 @@ public void handleRenew(RoutingContext ctx) {

}

public void handleGetExtPatronAccountByEmail(RoutingContext ctx) {
handleCommon(ctx,
new String[] { PARAM_PATRON_ID, PARAM_EMAIL_ID },
new String[] {},
(client, params) -> ((PatronOkapiClient) client).getExtPatronAccountByEmail(
params.get(PARAM_EMAIL_ID),
resp -> handleProxyResponse(ctx, resp),
t -> handleProxyException(ctx, t)));
}


public void handlePlaceItemHold(RoutingContext ctx) {
if (ctx.body().asJsonObject() == null) {
badRequest(ctx, MSG_HOLD_NOBODY);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/folio/edge/patron/utils/PatronOkapiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public void getAccount(String patronId, boolean includeLoans, boolean includeCha
exceptionHandler);
}

public void getExtPatronAccountByEmail(String email, Handler<HttpResponse<Buffer>> responseHandler,
Handler<Throwable> exceptionHandler) {
String url = String.format("%s/patron/account/by-email/%s", okapiURL, email);
get(
url,
tenant,
null,
responseHandler,
exceptionHandler);
}

public void renewItem(String patronId, String itemId,
Handler<HttpResponse<Buffer>> responseHandler, Handler<Throwable> exceptionHandler) {
post(
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/folio/edge/patron/MainVerticleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ public void testGetAccountPatronNotFound(TestContext context) throws Exception {
assertEquals(expectedStatusCode, msg.httpStatusCode);
}

@Test
public void testGetAccountByEmail(TestContext context) {
logger.info("=== Test request for getting external_patron by email ===");

RestAssured
.get(String.format("/patron/account/%s/by-email/%s?apikey=%s", extPatronId, "fgh@mail", apiKey))
.then()
.statusCode(200)
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
}

@Test
public void testGetAccountNoApiKey(TestContext context) throws Exception {
logger.info("=== Test request with malformed apiKey ===");
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/folio/edge/patron/utils/PatronMockOkapi.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public Router defineRoutes() {
router.route(HttpMethod.GET, "/patron/account/:patronId")
.handler(this::getAccountHandler);

router.route(HttpMethod.GET, "/patron/account/by-email/:emailId")
.handler(this::getExtPatronAccountHandler);

router.route(HttpMethod.POST, "/patron/account/:patronId/item/:itemId/renew")
.handler(this::renewItemHandler);

Expand Down Expand Up @@ -230,6 +233,22 @@ public void getAccountHandler(RoutingContext ctx) {
}
}

public void getExtPatronAccountHandler(RoutingContext ctx) {
String token = ctx.request().getHeader(X_OKAPI_TOKEN);

if (token == null || !token.equals(MOCK_TOKEN)) {
ctx.response()
.setStatusCode(403)
.putHeader(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN)
.end("Access requires permission: patron.account.get");
} else {
ctx.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
.end(getPatron().toString());
}
}

public void renewItemHandler(RoutingContext ctx) {
String patronId = ctx.request().getParam(PARAM_PATRON_ID);
String itemId = ctx.request().getParam(PARAM_ITEM_ID);
Expand Down

0 comments on commit 9d755bf

Please sign in to comment.