Skip to content

Commit

Permalink
Merge pull request #5 from Hubtwork/feat/patch
Browse files Browse the repository at this point in the history
Feat/patch
  • Loading branch information
hubtwork authored May 14, 2021
2 parents ab909c5 + c44b419 commit e95f2dd
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 119 deletions.
26 changes: 24 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
## Changes

#### v1.1.2 ( 2021. 5. 1)
#### v1.2.0 ( 2021. 5. 14 )

- Add preprocessed data ([#4](https://github.com/Hubtwork/NCPClient/issues/4))

- preprocessed data will deliver simple, essentials to client

- add SENS sms services
- add PAPAGO services

- Add test with bug fixed

- bug fix with mocked response for test
- test with preprocessed data
- test with latest

- Update readme

- update readme with preprocessed data exmaple
- update usage functions with parameters



#### v1.1.2 ( 2021. 5. 1 )

- can import SMS, PAPAGO class for more typescrypty usage

#### v1.1.1 ( 2021. 4. 26)
#### v1.1.1 ( 2021. 4. 26 )

- Readme typing error fixed

Expand Down
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ const smsService = sens.smsService(ncpAuthKey, smsAuthKey)

async function sendMessage() {
// if you don't pass countryCode, default countryCode is 82.
const {isSuccess, data, errorMessage } = await smsService.sendSMS( sendSMSparam, countryCode )
const {isSuccess, data, preprocessed, errorMessage } = await smsService.sendSMS( sendSMSparam, countryCode )
// write something after async function
if (isSuccess) {
const { result, requestId } = preprocessed
// do something with data
} else {
// handle with errorMessage
Expand All @@ -93,10 +94,12 @@ const smsService = sens.smsService(ncpAuthKey, smsAuthKey)
- **Search message delivery request**

~~~javascript
async function searchMessageDeliveryRequest() {
const {isSuccess, data, errorMessage } = await smsService.searchMessageRequest('requestId')
async function searchMessageDeliveryRequest(requestId: string) {
const {isSuccess, data, preprocessed, errorMessage } = await smsService.searchMessageRequest(requestId)
// write something after async function
if (isSuccess) {
// messageIds: string[]
const { result, requestId, messageIds } = preprocessed
// do something with data
} else {
// handle with errorMessage
Expand All @@ -107,10 +110,12 @@ const smsService = sens.smsService(ncpAuthKey, smsAuthKey)
- **Search message delivery results**

~~~javascript
async function searchMessageDeliveryResults() {
const {isSuccess, data, errorMessage } = await smsService.searchMessageResult('messageId')
async function searchMessageDeliveryResults(messageId: string) {
const {isSuccess, data, preprocessed, errorMessage } = await smsService.searchMessageResult(messageId)
// write something after async function
if (isSuccess) {
// messages: MessageResultType[]
const { result, messages } = preprocessed
// do something with data
} else {
// handle with errorMessage
Expand All @@ -136,29 +141,27 @@ const papagoClient = naverOpenAPI.papagoService(openApiClientAuth)

- **Translation**

~~~javascript
async function translation() {
const src = 'sourceLanguage'
const target = 'targetLanguage'
const text = 'text want to translate'
const { isSuccess, data, errorMessage } = await papagoClient.translation(src, target, text)
~~~typescript
async function translation(source: string, target: string, text: string) {
const { isSuccess, data, preprocessed, errorMessage } = await papagoClient.translation(src, target, text)
// write something after async function
if (isSuccess) {
const { source, target, translated } = preprocessed
// do something with data
} else {
// handle with errorMessage
}
}
~~~

- **Language Detection**

~~~javascript
async function detectLanguage() {
const text = 'text want to detect language'
const { isSuccess, data, errorMessage } = await papagoClient.detectLanguage(text)
async function detectLanguage(text: string) {
const { isSuccess, data, preprocessed, errorMessage } = await papagoClient.detectLanguage(text)
// write something after async function
if (isSuccess) {
const { detected } = preprocessed
// do something with data
} else {
// handle with errorMessage
Expand All @@ -169,21 +172,20 @@ const papagoClient = naverOpenAPI.papagoService(openApiClientAuth)
- **Korean Name Romanizer**

~~~javascript
async function koreanNameRomanizer() {
const name = 'korean name'
const { isSuccess, data, errorMessage } = await papagoClient.koreanNameRominizer(name)
async function koreanNameRomanizer(koreanName: string) {
const { isSuccess, data, preprocessed, errorMessage } = await papagoClient.koreanNameRominizer(name)
// write something after async function
if (isSuccess) {
console.log(data.aResult[0].sFirstName) // firstName
console.log(data.aResult[0].aItems) // aItems
const { firstName, bestMatched } = preprocessed
// do something with data
} else {
// handle with errorMessage
}
}
~~~




## Types

**Note)** Introduced types are what you have to create or handle in use *NCP-Client*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ncp-client",
"version": "1.1.2a",
"version": "1.2.0",
"description": "Naver Cloud Platform Applications' OpenAPI Wrapper built in TS",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions src/__test__/apiclient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('ApiClient TestSuite', () => {
'Content-Type': 'application/json; charset=utf-8'
}
}
const response = await client.request<testDataType>(apiRequest)
const response = await client.request<testDataType, object>(apiRequest)
expect(response.isSuccess).toEqual(true)
if (response.data) {
const data: testDataType = response.data || undefined
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('ApiClient TestSuite', () => {
'Content-Type': 'application/json; charset=utf-8'
}
}
const response = await client.request<testDataType>(apiRequest)
const response = await client.request<testDataType, object>(apiRequest)
expect(response.isSuccess).toEqual(false)
expect(response.errorMessage).toEqual('Unexpected HTTP Status Code : 404')
})
Expand All @@ -95,7 +95,7 @@ describe('ApiClient TestSuite', () => {
'Content-Type': 'application/json; charset=utf-8'
}
}
const response = await client.request<testDataType>(apiRequest)
const response = await client.request<testDataType, object>(apiRequest)
expect(response.isSuccess).toEqual(false)
expect(response.errorMessage).toEqual('No response from the server')
})
Expand All @@ -115,7 +115,7 @@ describe('ApiClient TestSuite', () => {
'Content-Type': 'application/json; charset=utf-8'
}
}
const response = await client.request<testDataType>(apiRequest)
const response = await client.request<testDataType, object>(apiRequest)
expect(response.isSuccess).toEqual(false)
expect(response.errorMessage).toEqual('Error occured during setup request')
})
Expand Down
34 changes: 31 additions & 3 deletions src/__test__/mock/mock_apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios, { AxiosResponse, Method } from 'axios'
import { ApiClientResponse } from '../../types/return_types'
import { ApiClientResponse, PapagoDetectLanguageReturnType, PapagoKoreanNameRomanizerReturnType, PapagoTranslationReturnType, SearchMessageRequestReturnType, SearchMessageResultReturnType, SendSMSReturnType } from '../../types/return_types'
import { ResponseTranslator, SupportedServices } from '../../types/service_translator'
import { ServiceError } from '../../utils/errors'

export class MockApiClient {
Expand All @@ -16,13 +17,17 @@ export class MockApiClient {
this.timeout = timeout
}

public async request<T>(apiRequest: ApiRequest, serviceError?: ServiceError): Promise<ApiClientResponse<T>> {
public async request<T extends object, P extends object>(apiRequest: ApiRequest, serviceError?: ServiceError): Promise<ApiClientResponse<T, P>> {
try {
if (serviceError) throw serviceError
const val = await this.createRequest<T>(apiRequest)
console.log(val)
const preprocessed = this.preprocessingServerResponse(val, apiRequest) as P
console.log(val, preprocessed)
return {
isSuccess: true,
data: val
data: val,
preprocessed: preprocessed
}
} catch (error) {
return {
Expand Down Expand Up @@ -74,13 +79,36 @@ export class MockApiClient {
var res = url.match(/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/)
return (res !== null)
}

private preprocessingServerResponse(val: object, apiRequest: ApiRequest) {
switch(apiRequest.service) {
// SMS
case SupportedServices.SENS_SEND_SMS:
return ResponseTranslator.sensSendSMS(val as SendSMSReturnType)
case SupportedServices.SENS_SEARCH_MESSAGE_REQUEST:
return ResponseTranslator.sensSearchMessageRequest(val as SearchMessageRequestReturnType)
case SupportedServices.SENS_SEARCH_MESSAGE_RESULT:
return ResponseTranslator.sensSearchMessageResult(val as SearchMessageResultReturnType)
// PAPAGO
case SupportedServices.PAPAGO_TRANSLATION:
return ResponseTranslator.papagoTranslation(val as PapagoTranslationReturnType)
case SupportedServices.PAPAGO_LANGUAGE_DETECTION:
return ResponseTranslator.papagoLanguageDetection(val as PapagoDetectLanguageReturnType)
case SupportedServices.PAPAGO_KOREAN_NAME_ROMANIZER:
return ResponseTranslator.papagoKoreanNameRomanizer(val as PapagoKoreanNameRomanizerReturnType)
default :
return {}
}
}
}

export interface ApiRequest {
path: string
method: Method
headers: { [key: string]: string }
body?: { [key: string]: any }

service?: SupportedServices
}


Expand Down
27 changes: 17 additions & 10 deletions src/__test__/mock/mock_papago.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import { ApiRequest, MockApiClient } from "./mock_apiClient"
import { Method } from "axios"
import { NaverOpenApiAuthType } from "../../types/auth_types"
import { PAPAGOlanguages, PAPAGOlanguageSupports } from '../../types/processing_types'
import { PAPAGOlanguages, PAPAGOlanguageSupports, PAPAGO_preprocessed_KoreanNameRomanizer, PAPAGO_preprocessed_LanguageDetction, PAPAGO_preprocessed_Translation } from '../../types/processing_types'
import { ApiClientResponse, PapagoDetectLanguageReturnType, PapagoKoreanNameRomanizerReturnType, PapagoTranslationReturnType } from "../../types/return_types"
import { ServiceError } from "../../utils/errors"
import { SupportedServices } from "../../types/service_translator"


export class MockPAPAGO {
Expand All @@ -21,7 +22,7 @@ export class MockPAPAGO {
this.openApiAuth = openApiAuth
}

public async translation(source: string, target: string, text: string): Promise<ApiClientResponse<PapagoTranslationReturnType>> {
public async translation(source: string, target: string, text: string): Promise<ApiClientResponse<PapagoTranslationReturnType, PAPAGO_preprocessed_Translation>> {
const path = 'nmt/v1/translation'
const method: Method = 'POST'
// construct signature and headers
Expand All @@ -39,14 +40,16 @@ export class MockPAPAGO {
path: path,
method: method,
headers: headers,
body: body
body: body,

service: SupportedServices.PAPAGO_TRANSLATION
}

let serviceError: ServiceError | undefined = this.languageSupportValidation(source, target, text)
return this.client.request<PapagoTranslationReturnType>(apiRequest, serviceError)
return this.client.request<PapagoTranslationReturnType, PAPAGO_preprocessed_Translation>(apiRequest, serviceError)
}

public async detectLanguage(text: string): Promise<ApiClientResponse<PapagoDetectLanguageReturnType>> {
public async detectLanguage(text: string): Promise<ApiClientResponse<PapagoDetectLanguageReturnType, PAPAGO_preprocessed_LanguageDetction>> {
const path = 'langs/v1/dect'
const method: Method = 'POST'
// construct signature and headers
Expand All @@ -62,14 +65,16 @@ export class MockPAPAGO {
path: path,
method: method,
headers: headers,
body: body
body: body,

service: SupportedServices.PAPAGO_LANGUAGE_DETECTION
}

let serviceError: ServiceError | undefined = this.detectLanguageValidation(text)
return this.client.request<PapagoDetectLanguageReturnType>(apiRequest, serviceError)
return this.client.request<PapagoDetectLanguageReturnType, PAPAGO_preprocessed_LanguageDetction>(apiRequest, serviceError)
}

public async koreanNameRominizer(koreanName: string): Promise<ApiClientResponse<PapagoKoreanNameRomanizerReturnType>> {
public async koreanNameRominizer(koreanName: string): Promise<ApiClientResponse<PapagoKoreanNameRomanizerReturnType, PAPAGO_preprocessed_KoreanNameRomanizer>> {
const path = `krdict/v1/romanization?query=${encodeURI(koreanName)}`
const method: Method = 'GET'
// construct signature and headers
Expand All @@ -81,11 +86,13 @@ export class MockPAPAGO {
const apiRequest: ApiRequest = {
path: path,
method: method,
headers: headers
headers: headers,

service: SupportedServices.PAPAGO_KOREAN_NAME_ROMANIZER
}

let serviceError: ServiceError | undefined = this.koreanNameRomanizerValidation(koreanName)
return this.client.request<PapagoKoreanNameRomanizerReturnType>(apiRequest, serviceError)
return this.client.request<PapagoKoreanNameRomanizerReturnType, PAPAGO_preprocessed_KoreanNameRomanizer>(apiRequest, serviceError)
}

private languageSupportValidation(source: string, target: string, text: string): ServiceError | undefined {
Expand Down
26 changes: 17 additions & 9 deletions src/__test__/mock/mock_smsClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Method } from "axios"
import { NCPAuthKeyType, SMSserviceAuthType } from "../../types/auth_types"
import { SendSMSParamType } from "../../types/param_types"
import { SENS_preprocessed_SearchMessageRequest, SENS_preprocessed_SearchMessageResult, SENS_preprocessed_SendSMS } from "../../types/processing_types"
import { ApiClientResponse, SearchMessageRequestReturnType, SearchMessageResultReturnType, SendSMSReturnType } from "../../types/return_types"
import { SupportedServices } from "../../types/service_translator"
import { generateApiSignature } from "../../utils/helper"
import { ApiRequest, MockApiClient } from "./mock_apiClient"

Expand All @@ -23,7 +25,7 @@ export class MockSMS {
this.client = new MockApiClient(baseUrl)
}

public async sendSMS(smsParam: SendSMSParamType | SendSMSParamType[], countryCode?: number): Promise<ApiClientResponse<SendSMSReturnType>> {
public async sendSMS(smsParam: SendSMSParamType | SendSMSParamType[], countryCode?: number): Promise<ApiClientResponse<SendSMSReturnType, SENS_preprocessed_SendSMS>> {
// construct path and method
const path = `/sms/v2/services/${this.smsAuth.serviceId}/messages`
const method: Method = 'POST'
Expand All @@ -49,12 +51,14 @@ export class MockSMS {
path: path,
method: method,
headers: headers,
body: body
body: body,

service: SupportedServices.SENS_SEND_SMS
}
return this.client.request<SendSMSReturnType>(apiRequest)
return this.client.request<SendSMSReturnType, SENS_preprocessed_SendSMS>(apiRequest)
}

public async searchMessageRequest(requestId: string): Promise<ApiClientResponse<SearchMessageRequestReturnType>> {
public async searchMessageRequest(requestId: string): Promise<ApiClientResponse<SearchMessageRequestReturnType, SENS_preprocessed_SearchMessageRequest>> {
// construct path and method
const path = `/sms/v2/services/${this.smsAuth.serviceId}/messages?requestId=${requestId}`
const method: Method = 'GET'
Expand All @@ -69,12 +73,14 @@ export class MockSMS {
const apiRequest: ApiRequest = {
path: path,
method: method,
headers: headers
headers: headers,

service: SupportedServices.SENS_SEARCH_MESSAGE_REQUEST
}
return this.client.request<SearchMessageRequestReturnType>(apiRequest)
return this.client.request<SearchMessageRequestReturnType, SENS_preprocessed_SearchMessageRequest>(apiRequest)
}

public async searchMessageResult(messageId: string): Promise<ApiClientResponse<SearchMessageResultReturnType>> {
public async searchMessageResult(messageId: string): Promise<ApiClientResponse<SearchMessageResultReturnType, SENS_preprocessed_SearchMessageResult>> {
// construct path and method
const path = `/sms/v2/services/${this.smsAuth.serviceId}/messages/${messageId}`
const method: Method = 'GET'
Expand All @@ -89,9 +95,11 @@ export class MockSMS {
const apiRequest: ApiRequest = {
path: path,
method: method,
headers: headers
headers: headers,

service: SupportedServices.SENS_SEARCH_MESSAGE_RESULT
}
return this.client.request<SearchMessageResultReturnType>(apiRequest)
return this.client.request<SearchMessageResultReturnType, SENS_preprocessed_SearchMessageResult>(apiRequest)
}


Expand Down
Loading

0 comments on commit e95f2dd

Please sign in to comment.