Skip to content
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

Code Quality: Replace deprecated constructor call and fix unchecked assignment warning #1000

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions wiremock/src/main/java/PatientDemographicsServiceClient.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import org.apache.hc.client5.http.utils.Base64;
import org.json.JSONObject;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

Expand All @@ -37,7 +38,7 @@ public Map<String, Object> patient(String nhsNumber) throws Exception {

String patientData = getPatientData(nhsNumber, accessToken);

return new ObjectMapper().readValue(patientData, HashMap.class);
return new ObjectMapper().readValue(patientData, new TypeReference<>() {});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The part which is new TypeReference<>() {} is actually new TypeReference<Map<String, Object>>() {} but you can omit the Map<String, Object>> if it can be inferred.

This is a way to provide full type information to Jackson when deserializing, avoiding unchecked warnings. It's a generic safe way to tell the object mapper what it is being deserialized into

}

private String generateJwtToken() throws Exception {
Expand All @@ -52,16 +53,17 @@ private String generateJwtToken() throws Exception {
.sign(algorithm);
}

private static String getPatientData(String nhsNumber, String accessToken) throws IOException {
var connection = (HttpURLConnection) (new URL("https://int.api.service.nhs.uk/personal-demographics/FHIR/R4/Patient/" + nhsNumber)).openConnection();
private static String getPatientData(String nhsNumber, String accessToken) throws IOException, URISyntaxException {
var connection = (HttpURLConnection) new URI("https://int.api.service.nhs.uk/personal-demographics/FHIR/R4/Patient/" + nhsNumber)
.toURL().openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("X-Request-Id", UUID.randomUUID().toString());
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
return new String(connection.getInputStream().readAllBytes(), Charsets.UTF_8);
}

private static String getOauthToken(String token) throws IOException {
var connection = (HttpURLConnection) new URL(OAUTH_ENDPOINT).openConnection();
private static String getOauthToken(String token) throws IOException, URISyntaxException {
var connection = (HttpURLConnection) new URI(OAUTH_ENDPOINT).toURL().openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
Expand Down