Last active
          February 23, 2021 07:29 
        
      - 
      
- 
        Save myoungpyohong/2f4e9aa3c3ccc3691c7f4f2f41c6f34e to your computer and use it in GitHub Desktop. 
    CoolSMS Retrofit Example
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | // create 6-digit | |
| public int create6digit() { | |
| SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); | |
| int verificationNumber = sr.nextInt(900000) + 100000; | |
| } | |
| @Configuration | |
| public class RetrofitConfig { | |
| @Bean | |
| public CoolSMSService coolSMSService(ObjectMapper objectMapper) { | |
| Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.coolsms.co.kr/") | |
| .addConverterFactory(JacksonConverterFactory.create(objectMapper)) | |
| .build(); | |
| return retrofit.create(CoolSMSService.class); | |
| } | |
| } | |
| @Service | |
| public interface CoolSMSService { | |
| @POST("sms/2/send") | |
| Call<CoolSMSResponse> sendSms(@Body CoolSMSRequest coolSMSRequest); | |
| } | |
| public class CoolSMSRequest { | |
| @JsonProperty("api_key") | |
| String apiKey; | |
| String timestamp; | |
| String salt; | |
| String signature; | |
| final String encoding = "base64"; | |
| String to; | |
| final String from = "<phoneNumber>"; | |
| String text; | |
| public static CoolSMSRequest of() { | |
| CoolSMSRequest coolSMSRequest = new CoolSMSRequest(); | |
| try { | |
| coolSMSRequest.apiKey = "<key>"; | |
| String apiSecret = "<secret>"; | |
| coolSMSRequest.timestamp = String.valueOf(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); | |
| coolSMSRequest.salt = UUID.randomUUID().toString(); | |
| String hmacData = coolSMSRequest.timestamp + coolSMSRequest.salt; | |
| SecretKeySpec key = new SecretKeySpec(apiSecret.getBytes(), "HmacMD5"); | |
| Mac mac = Mac.getInstance("HmacMD5"); | |
| mac.init(key); | |
| coolSMSRequest.signature = Base64.encodeBase64String(mac.doFinal(hmacData.getBytes())); | |
| } catch (Exception ex) { | |
| } | |
| return coolSMSRequest; | |
| } | |
| public String getApiKey() { | |
| return this.apiKey; | |
| } | |
| public String getTimestamp() { | |
| return this.timestamp; | |
| } | |
| public String getSalt() { | |
| return this.salt; | |
| } | |
| public String getSignature() { | |
| return this.signature; | |
| } | |
| public String getEncoding() { | |
| return this.encoding; | |
| } | |
| public String getTo() { | |
| return this.to; | |
| } | |
| public void setTo(String to) { | |
| this.to = to; | |
| } | |
| public String getFrom() { | |
| return this.from; | |
| } | |
| public String getText() { | |
| return this.text; | |
| } | |
| public void setText(String text) { | |
| this.text = text; | |
| } | |
| } | |
| @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) | |
| public class CoolSMSResponse { | |
| Boolean successful; | |
| Integer successCount; | |
| Integer errorCount; | |
| String groupId; | |
| String resultCode; | |
| String resultMessage; | |
| LinkedHashMap errorList; | |
| public Boolean getSuccessful() { | |
| return this.successful; | |
| } | |
| public Integer getSuccessCount() { | |
| return this.successCount; | |
| } | |
| public Integer getErrorCount() { | |
| return this.errorCount; | |
| } | |
| public String getGroupId() { | |
| return this.groupId; | |
| } | |
| public String getResultCode() { | |
| return this.resultCode; | |
| } | |
| public String getResultMessage() { | |
| return this.resultMessage; | |
| } | |
| public LinkedHashMap getErrorList() { | |
| return this.errorList; | |
| } | |
| } | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment