-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NABI-105--feat : 외부api를 이용한 닉네임 랜덤생성 구현
- Loading branch information
1 parent
7e4fad9
commit 2a47dda
Showing
3 changed files
with
54 additions
and
6 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/org/prgrms/nabimarketbe/domain/user/service/RandomNicknameGenerator.java
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,39 @@ | ||
package org.prgrms.nabimarketbe.domain.user.service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponents; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class RandomNicknameGenerator { | ||
private static final String GENERATOR_URL = "https://nickname.hwanmoo.kr/?format=json&count=1"; | ||
|
||
private static final int NICKNAME_MAX_LENGTH = 6; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public String generateRandomNickname() throws JsonProcessingException { | ||
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(GENERATOR_URL) | ||
.queryParam("max_length", NICKNAME_MAX_LENGTH).build(); | ||
|
||
String apiResult = restTemplate.getForObject(uriComponents.toString(), String.class); | ||
Map<String, Object> wordMap = objectMapper.readValue(apiResult, Map.class); | ||
|
||
List<String> randomNicknames = (List<String>) wordMap.get("words"); | ||
String randomNickname = randomNicknames.get(0); | ||
|
||
return randomNickname; | ||
} | ||
} |
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