Skip to content

Commit

Permalink
feat(eSMS): Add https base url; Add option to set optional BASE_URL
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Nov 14, 2018
1 parent 82dee16 commit a853c14
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/__mocks__/esms.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const eSMSMockAPI = async (url: string) => {
// Calls sendMessage() with good data
if (
url ===
'http://rest.esms.vn/MainService.svc/json/SendMultipleMessage_V4_get?Phone=0979477635&Content=Test%20message%20jest&SmsType=2&IsUnicode=0&Brandname=STORELAMMOC&RequestId=&Sandbox=1&ApiKey=__SUCCESS_API_KEY__&SecretKey=__SUCCESS_SECRET_KEY__'
`${BASE_URL}/SendMultipleMessage_V4_get?Phone=0979477635&Content=Test%20message%20jest&SmsType=2&IsUnicode=0&Brandname=STORELAMMOC&RequestId=&Sandbox=1&ApiKey=__SUCCESS_API_KEY__&SecretKey=__SUCCESS_SECRET_KEY__`
) {
return {
data: {
Expand All @@ -72,7 +72,7 @@ export const eSMSMockAPI = async (url: string) => {
// Calls sendMessage() with empty brand name
if (
url ===
'http://rest.esms.vn/MainService.svc/json/SendMultipleMessage_V4_get?Phone=0979477635&Content=Test%20message%20jest&SmsType=2&IsUnicode=0&Brandname=&RequestId=&Sandbox=0&ApiKey=__SUCCESS_API_KEY__&SecretKey=__SUCCESS_SECRET_KEY__'
`${BASE_URL}/SendMultipleMessage_V4_get?Phone=0979477635&Content=Test%20message%20jest&SmsType=2&IsUnicode=0&Brandname=&RequestId=&Sandbox=0&ApiKey=__SUCCESS_API_KEY__&SecretKey=__SUCCESS_SECRET_KEY__`
) {
return {
data: {
Expand Down
7 changes: 5 additions & 2 deletions src/constants/esms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const BASE_URL = 'http://rest.esms.vn/MainService.svc/json'
export const BASE_URL_HTTP = 'http://rest.esms.vn/MainService.svc/json'
export const BASE_URL_HTTPS = 'https://restapi.esms.vn/MainService.svc/json'
export const BASE_URL = BASE_URL_HTTPS
export const ERROR_CODES = {
// 99: 'Something went wrong. Please try again.',
100: 'Request succeeded.',
Expand All @@ -14,6 +16,7 @@ export const BRAND_NAME_TYPES = {
}

export default {
BASE_URL,
BASE_URL: BASE_URL_HTTPS,
BASE_URL_HTTP,
ERROR_CODES,
}
File renamed without changes.
7 changes: 7 additions & 0 deletions src/services/esms/esms-interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { ServiceOptions } from 'services'

export interface ESMSAuthConfig {
API_KEY: string
SECRET_KEY: string
}

export interface ESMSServiceOptions extends ServiceOptions {
baseUrl?: string
useHttp?: boolean
}

export interface ESMSResponse {
CodeResponse: string
ErrorMessage?: string
Expand Down
36 changes: 27 additions & 9 deletions src/services/esms/esms.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
import axios from 'axios'
import { stringify } from 'querystring'

import SMSService, { ServiceOptions } from '../index'
import SMSService from '../index'

import { BASE_URL, BRAND_NAME_TYPES, ERROR_CODES } from '../../constants/esms'
import { BrandName } from '../../types/brand-name'
import {
BASE_URL,
BASE_URL_HTTP,
BASE_URL_HTTPS,
BRAND_NAME_TYPES,
ERROR_CODES,
} from '../../constants/esms'
import { BrandName } from '../../interfaces/brand-name'
import {
ESMSAuthConfig,
ESMSGetBalanceResponse,
ESMSGetBrandNameListResponse,
ESMSSendMessageArgs,
ESMSSendMessageResponse,
ESMSServiceOptions,
} from './esms-interfaces'

/**
* @see Documentation at https://drive.google.com/file/d/0ByZdl9Zt3D_fbWFkQ3pHc0I3Q0xSelAxMjFXWXRtTWdETGVr/view
*/
class ESMS extends SMSService<ESMSAuthConfig> {
constructor(authConfig: ESMSAuthConfig, serviceOptions?: ServiceOptions) {
BASE_URL = BASE_URL
constructor(
authConfig: ESMSAuthConfig,
serviceOptions: ESMSServiceOptions = { loglevel: 'silent' },
) {
super(authConfig, serviceOptions)

if (serviceOptions.baseUrl) this.BASE_URL = serviceOptions.baseUrl
else {
this.BASE_URL = serviceOptions.useHttp ? BASE_URL_HTTP : BASE_URL_HTTPS
}

this.logger.info('eSMS service initialized with', authConfig)
}

Expand All @@ -29,7 +45,7 @@ class ESMS extends SMSService<ESMSAuthConfig> {
*/

const { API_KEY, SECRET_KEY } = this.authConfig
const getBalanceURL = `${BASE_URL}/GetBalance/${API_KEY}/${SECRET_KEY}`
const getBalanceURL = `${this.BASE_URL}/GetBalance/${API_KEY}/${SECRET_KEY}`

this.logger.debug(`Getting eSMS balance by GET ${getBalanceURL}`)

Expand Down Expand Up @@ -58,7 +74,9 @@ class ESMS extends SMSService<ESMSAuthConfig> {
*/

const { API_KEY, SECRET_KEY } = this.authConfig
const getBrandNameListURL = `${BASE_URL}/GetListBrandname/${API_KEY}/${SECRET_KEY}`
const getBrandNameListURL = `${
this.BASE_URL
}/GetListBrandname/${API_KEY}/${SECRET_KEY}`

this.logger.debug(
`Getting eSMS brand name list by GET ${getBrandNameListURL}`,
Expand Down Expand Up @@ -115,9 +133,9 @@ class ESMS extends SMSService<ESMSAuthConfig> {
SecretKey: SECRET_KEY,
}

const sendMessageURL = `${BASE_URL}/SendMultipleMessage_V4_get?${stringify(
messageData,
)}`
const sendMessageURL = `${
this.BASE_URL
}/SendMultipleMessage_V4_get?${stringify(messageData)}`

this.logger.debug(`Sending message by GET`, sendMessageURL)

Expand Down

0 comments on commit a853c14

Please sign in to comment.