From 4d2c02e81cfaea8f358cc2ee795dd3732a60d70c Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 20 Jan 2018 19:11:28 -0500 Subject: [PATCH] http-service: split hiscore service from controller --- http-service/pom.xml | 8 ++ .../service/hiscore/HiscoreController.java | 82 +++++++++++++++++++ .../http/service/hiscore/HiscoreService.java | 66 +++------------ .../service/hiscore/HiscoreServiceTest.java | 2 +- .../service/hiscore/HiscoreTestService.java | 8 +- 5 files changed, 106 insertions(+), 60 deletions(-) create mode 100644 http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreController.java diff --git a/http-service/pom.xml b/http-service/pom.xml index 8b153f38a2..b24e62999a 100644 --- a/http-service/pom.xml +++ b/http-service/pom.xml @@ -37,6 +37,7 @@ 1.5.6.RELEASE + 1.16.18 @@ -63,6 +64,13 @@ true + + org.projectlombok + lombok + ${lombok.version} + provided + + net.runelite http-api diff --git a/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreController.java b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreController.java new file mode 100644 index 0000000000..faccbf06a3 --- /dev/null +++ b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreController.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.http.service.hiscore; + +import java.io.IOException; +import net.runelite.http.api.hiscore.HiscoreEndpoint; +import net.runelite.http.api.hiscore.HiscoreResult; +import net.runelite.http.api.hiscore.HiscoreSkill; +import net.runelite.http.api.hiscore.SingleHiscoreSkillResult; +import net.runelite.http.api.hiscore.Skill; +import net.runelite.http.service.util.HiscoreEndpointEditor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/hiscore") +public class HiscoreController +{ + @Autowired + private HiscoreService hiscoreService; + + @RequestMapping("/{endpoint}") + public HiscoreResult lookup(@PathVariable HiscoreEndpoint endpoint, @RequestParam String username) throws IOException + { + HiscoreResultBuilder resultBuilder = hiscoreService.lookupUsername(username, endpoint); + HiscoreResult result = resultBuilder.build(); + + return result; + } + + @RequestMapping("/{endpoint}/{skillName}") + public SingleHiscoreSkillResult singleSkillLookup(@PathVariable HiscoreEndpoint endpoint, @PathVariable String skillName, @RequestParam String username) throws IOException + { + HiscoreSkill skill = HiscoreSkill.valueOf(skillName.toUpperCase()); + + // RS api only supports looking up all stats + HiscoreResultBuilder result = hiscoreService.lookupUsername(username, endpoint); + + // Find the skill to return + Skill requested = result.getSkill(skill.ordinal()); + + SingleHiscoreSkillResult skillResult = new SingleHiscoreSkillResult(); + skillResult.setPlayer(username); + skillResult.setSkillName(skillName); + skillResult.setSkill(requested); + + return skillResult; + } + + @InitBinder + public void initBinder(WebDataBinder binder) + { + binder.registerCustomEditor(HiscoreEndpoint.class, new HiscoreEndpointEditor()); + } +} diff --git a/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreService.java b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreService.java index eab9a84428..e52c35d986 100644 --- a/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreService.java +++ b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreService.java @@ -25,13 +25,11 @@ package net.runelite.http.service.hiscore; import java.io.IOException; +import lombok.extern.slf4j.Slf4j; import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.hiscore.HiscoreEndpoint; -import net.runelite.http.api.hiscore.HiscoreResult; import net.runelite.http.api.hiscore.HiscoreSkill; -import net.runelite.http.api.hiscore.SingleHiscoreSkillResult; import net.runelite.http.api.hiscore.Skill; -import net.runelite.http.service.util.HiscoreEndpointEditor; import net.runelite.http.service.util.exception.InternalServerErrorException; import net.runelite.http.service.util.exception.NotFoundException; import okhttp3.HttpUrl; @@ -40,38 +38,29 @@ import okhttp3.Response; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.stereotype.Service; -@RestController -@RequestMapping("/hiscore") +@Service +@Slf4j public class HiscoreService { - private static final Logger logger = LoggerFactory.getLogger(HiscoreService.class); - - HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws IOException + public HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws IOException { return lookupUsername(username, endpoint.getHiscoreURL()); } - HiscoreResultBuilder lookupUsername(String username, HttpUrl hiscoreUrl) throws IOException + public HiscoreResultBuilder lookupUsername(String username, HttpUrl hiscoreUrl) throws IOException { HttpUrl url = hiscoreUrl.newBuilder() - .addQueryParameter("player", username) - .build(); + .addQueryParameter("player", username) + .build(); - logger.info("Built URL {}", url); + log.debug("Built URL {}", url); Request okrequest = new Request.Builder() - .url(url) - .build(); + .url(url) + .build(); String responseStr; @@ -102,7 +91,7 @@ public class HiscoreService { if (count++ >= HiscoreSkill.values().length) { - logger.warn("Jagex Hiscore API returned unexpected data"); + log.warn("Jagex Hiscore API returned unexpected data"); break; // rest is other things? } @@ -124,35 +113,4 @@ public class HiscoreService return hiscoreBuilder; } - @RequestMapping("/{endpoint}") - public HiscoreResult lookup(@PathVariable HiscoreEndpoint endpoint, @RequestParam String username) throws IOException - { - HiscoreResultBuilder result = lookupUsername(username, endpoint); - return result.build(); - } - - @RequestMapping("/{endpoint}/{skillName}") - public SingleHiscoreSkillResult singleSkillLookup(@PathVariable HiscoreEndpoint endpoint, @PathVariable String skillName, @RequestParam String username) throws IOException - { - HiscoreSkill skill = HiscoreSkill.valueOf(skillName.toUpperCase()); - - // RS api only supports looking up all stats - HiscoreResultBuilder result = lookupUsername(username, endpoint); - - // Find the skill to return - Skill requested = result.getSkill(skill.ordinal()); - - SingleHiscoreSkillResult skillResult = new SingleHiscoreSkillResult(); - skillResult.setPlayer(username); - skillResult.setSkillName(skillName); - skillResult.setSkill(requested); - - return skillResult; - } - - @InitBinder - public void initBinder(WebDataBinder binder) - { - binder.registerCustomEditor(HiscoreEndpoint.class, new HiscoreEndpointEditor()); - } } diff --git a/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreServiceTest.java b/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreServiceTest.java index 5fc1706dcf..152707a5ca 100644 --- a/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreServiceTest.java +++ b/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreServiceTest.java @@ -91,7 +91,7 @@ public class HiscoreServiceTest { HiscoreTestService hiscores = new HiscoreTestService(server.url("/")); - HiscoreResult result = hiscores.lookup(HiscoreEndpoint.NORMAL, "zezima"); + HiscoreResult result = hiscores.lookupUsername("zezima", HiscoreEndpoint.NORMAL).build(); Assert.assertEquals(50, result.getAttack().getLevel()); Assert.assertEquals(159727L, result.getFishing().getExperience()); diff --git a/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreTestService.java b/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreTestService.java index 2ef8b2dd56..4290cd1d1a 100644 --- a/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreTestService.java +++ b/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreTestService.java @@ -23,17 +23,15 @@ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - package net.runelite.http.service.hiscore; +import java.io.IOException; import net.runelite.http.api.hiscore.HiscoreEndpoint; import okhttp3.HttpUrl; -import java.io.IOException; - class HiscoreTestService extends HiscoreService { - private HttpUrl testUrl; + private final HttpUrl testUrl; HiscoreTestService(HttpUrl testUrl) { @@ -41,7 +39,7 @@ class HiscoreTestService extends HiscoreService } @Override - HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws IOException + public HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws IOException { return super.lookupUsername(username, testUrl); }