package icshttp
import (
"batchmodule/icsconf"
"batchmodule/icserror"
"batchmodule/icslog"
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
)
var (
conf icsconf . AppInfo
l * log . Logger
)
func init ( ) {
conf = icsconf . Getconfig ( )
l = icslog . InitializeLogger ( )
}
type TAInfoJSON struct {
Cmd string ` json:"cmd" `
ConnId string ` json:"connId" `
Tel string ` json:"tel" `
Ext string ` json:"ext" `
EmpId string ` json:"empId" `
DateTime string ` json:"dateTime" `
CallStartTime string ` json:"callStartTime" `
Index string ` json:"index" `
Stt string ` json:"stt" `
Dir string ` json:"dir" `
IoGbn string ` json:"ioGbn" `
}
type FailedList struct {
No int ` json:"no" `
ConnId string ` json:"connid" `
Ext string ` json:"ext" `
StartTime string ` json:"starttime" `
}
type FailedDataListReqBody struct {
StartTime string ` json:"starttime" `
Ext string ` json:"ext" `
ConnId string ` json:"connid" `
}
type FailedDataListResBody struct {
TotalCnt string ` json:"totalCnt" `
ReturnCode string ` json:"returnCode" `
ReturnMsg string ` json:"returnMsg" `
List [ ] FailedList ` json:"list" `
}
type FailedDataVoiceResBody struct {
VoiceFile string ` json:"voiceFile" `
ReturnCode string ` json:"returnCode" `
ReturnMsg string ` json:"returnMsg" `
}
type IcsHttpClient struct {
Method string
URL string
Request * http . Request
http . Client
}
type TAResp struct {
Success bool ` json:"success" `
ReturnCode string ` json:"returnCode" `
Message string ` json:"message" `
}
type NockChiReqBody struct {
Uid string ` json:"uid" `
Ext string ` json:"ext" `
SpeakerTag string ` json:"speackertag" `
Transcripts string ` json:"transcripts" `
}
func NewIcsHttpClient ( method , url string , body io . Reader ) * IcsHttpClient {
c := & IcsHttpClient { Method : method , URL : url }
r , err := http . NewRequest ( method , url , body )
if err != nil {
l . Println ( "HTTP Client error while http.NewRequest(). error: " , err )
return nil
}
c . Request = r
c . SetTimeout ( time . Millisecond * 1000 * 5 )
return c
}
func ( c * IcsHttpClient ) SetTimeout ( timeout time . Duration ) {
c . Timeout = timeout
}
func HttpRequest ( url , method , requestBody string ) ( * http . Response , error ) {
// TLS 설정
tr := & http . Transport {
TLSClientConfig : & tls . Config { InsecureSkipVerify : true } , // 인증서 검증 비활성화
}
client := & http . Client { Transport : tr }
// client := &http.Client{}
payload := strings . NewReader ( requestBody )
req , _ := http . NewRequest ( method , url , payload )
req . Header . Set ( "Content-Type" , "application/json" )
// 요청
res , err := client . Do ( req )
if err != nil {
return nil , fmt . Errorf ( "요청 오류: %w" , err )
}
if res . StatusCode != http . StatusOK {
return nil , fmt . Errorf ( "error: received non-200 response status: %s" , res . Status )
}
return res , nil
}
func PostReqForFailedDataList ( ) ( response * FailedDataListResBody , cerr * icserror . IcsError ) {
url := conf . Urls . FailedDataListUrl
currentDate := time . Now ( ) . Format ( "20060102" )
currentDateUint64 , _ := strconv . ParseUint ( currentDate , 10 , 64 )
starttime := currentDateUint64 - 1
starttimeStr := strconv . FormatUint ( starttime , 10 )
body := FailedDataListReqBody {
StartTime : starttimeStr ,
Ext : "" ,
ConnId : "" ,
}
jsonBody , err := json . Marshal ( body )
if err != nil {
return nil , icserror . ICSERRMarshal
}
reqBody := bytes . NewBuffer ( jsonBody )
l . Printf ( "client.go>PostReqForFailedDataList()> url: %s, reqBody: %+v" , url , reqBody . String ( ) )
client := NewIcsHttpClient ( "POST" , url , reqBody )
if client == nil {
return nil , icserror . ICSERRHTTPClientCreate
}
client . Request . Header . Add ( "Content-Type" , "application/json;charset=utf-8" )
client . Request . Header . Add ( "Accept" , "application/json" )
l . Printf ( "Error at client.go>PostReqForFailedDataList()> client.Request: %+v" , client )
resp , err := client . Do ( client . Request )
if err != nil {
l . Printf ( "Err at client.go>PostReqForFailedDataList()> client.Do failed. resp: %+v" , resp )
l . Printf ( "The err: %+v" , err )
return nil , icserror . ICSERRHTTPClientExcecution
}
l . Printf ( "client.go>PostReqForFailedDataList()> resp: %+v" , resp )
resBody , err := io . ReadAll ( resp . Body )
if err != nil {
l . Printf ( "client.go>PostReqForFailedDataList()> ReadAll err: %+v" , err )
return nil , icserror . ICSERRRead
}
defer resp . Body . Close ( )
if err := json . Unmarshal ( resBody , & response ) ; err != nil {
l . Printf ( "Error at client.go>PostReqForFailedDataList()> Unmarshal err: %+v" , err )
return nil , icserror . ICSERRUnmarshal
}
if strings . Contains ( response . ReturnCode , "9999" ) {
l . Printf ( "Error at client.go>PostReqForFailedDataList()> response returnCode is 9999 with the message: %s" , response . ReturnMsg )
return nil , icserror . ICSERRHTTPClientResponseFail
}
return response , nil
}
func PostReqForEachVoiceFile ( reqDataForVoiceFile FailedDataListReqBody ) ( response * FailedDataVoiceResBody , cerr * icserror . IcsError ) {
url := conf . Urls . FailedDataListUrl
jsonBody , err := json . Marshal ( reqDataForVoiceFile )
if err != nil {
return nil , icserror . ICSERRMarshal
}
reqBody := bytes . NewBuffer ( jsonBody )
l . Printf ( "client.go>PostReqForEachVoiceFile()> url: %s, reqBody: %+v" , url , reqBody . String ( ) )
client := NewIcsHttpClient ( "POST" , url , reqBody )
if client == nil {
return nil , icserror . ICSERRHTTPClientCreate
}
client . Request . Header . Add ( "Content-Type" , "application/json;charset=utf-8" )
client . Request . Header . Add ( "Accept" , "application/json" )
l . Printf ( "Error at client.go>PostReqForEachVoiceFile()> client.Request: %+v" , client )
resp , err := client . Do ( client . Request )
if err != nil {
l . Printf ( "Error at client.go>PostReqForEachVoiceFile()> client.Do failed. resp: %+v" , resp )
l . Printf ( "The err: %+v" , err )
return nil , icserror . ICSERRHTTPClientExcecution
}
l . Printf ( "client.go>PostReqForEachVoiceFile()> resp: %+v" , resp )
resBody , err := io . ReadAll ( resp . Body )
if err != nil {
l . Printf ( "client.go>PostReqForEachVoiceFile()> ReadAll err: %+v" , err )
return nil , icserror . ICSERRRead
}
defer resp . Body . Close ( )
if err := json . Unmarshal ( resBody , & response ) ; err != nil {
l . Printf ( "Error at client.go>PostReqForEachVoiceFile()> Unmarshal err: %+v" , err )
return nil , icserror . ICSERRUnmarshal
}
if strings . Contains ( response . ReturnCode , "9999" ) {
l . Printf ( "Error at client.go>PostReqForEachVoiceFile()> response returnCode is 9999 with the message: %s" , response . ReturnMsg )
return nil , icserror . ICSERRHTTPClientResponseFail
}
l . Printf ( "client.go>PostReqForEachVoiceFile()> response: %+v" , response )
return response , nil
}
func SendSTTResToNockChiServer ( parsedRes FailedDataListReqBody , sttRes string , dir string ) * icserror . IcsError {
url := conf . Urls . NockChiServerUrl
conn , _ , err := websocket . DefaultDialer . Dial ( url , nil )
if err != nil {
l . Println ( "[ERR] client.go>SendSTTResToNockChiServer()> connecting websocket failed. err: %v" , err )
return icserror . ICSERRWEBSOCKETConnectFailError
}
defer conn . Close ( )
reqData := NockChiReqBody {
Uid : parsedRes . ConnId ,
Ext : parsedRes . Ext ,
SpeakerTag : dir ,
Transcripts : sttRes ,
}
jsonData , _ := json . Marshal ( reqData )
reqBody := fmt . Sprintf ( ` { "data":"%s"} ` , jsonData )
err = conn . WriteMessage ( websocket . TextMessage , [ ] byte ( reqBody ) )
if err != nil {
l . Println ( "[ERR] client.go>SendSTTResToNockChiServer()> conn.WriteMessage() failed. err: " , err )
return icserror . ICSERRWEBSOCKETWriteError
}
l . Println ( "[LOG] client.go>SendSTTResToNockChiServer()> the stt result(connId: %s) was successfully sent via websocket. reqBody: %s" , parsedRes . ConnId , reqBody )
return nil
}
func PostProcessTA ( url , cmd string , connId string , tel string , ext string , empId string , dateTime string , callStartTime string , index string , stt string , dir string , ioGbn string ) ( tAResp * TAResp , cerr * icserror . IcsError ) {
tAInfoJSON := TAInfoJSON {
Cmd : cmd ,
ConnId : connId ,
Tel : tel ,
Ext : ext ,
EmpId : empId ,
DateTime : dateTime ,
CallStartTime : callStartTime ,
Index : index ,
Stt : stt ,
Dir : dir ,
IoGbn : ioGbn ,
}
b , err := json . Marshal ( tAInfoJSON )
if err != nil {
return nil , icserror . ICSERRMarshal
}
body := bytes . NewBuffer ( b )
client := NewIcsHttpClient ( "POST" , url , body )
if client == nil {
l . Printf ( "[ERR] client.go>PostProcessTA()> NewIcsHttpClient() failed. err: %v" , err )
l . Printf ( "[ERR] client.go>PostProcessTA()> NewIcsHttpClient() failed. url: %s, body: %s" , url , body )
return nil , icserror . ICSERRHTTPClientCreate
}
client . Request . Header . Add ( "Content-Type" , "application/json;charset=utf-8" )
client . Request . Header . Add ( "Accept" , "application/json" )
l . Printf ( "[LOG] client.go>PostProcessTA()> Post Request Body: %+v" , client . Request . Body )
resp , err := client . Do ( client . Request )
if err != nil {
l . Printf ( "[ERR] client.go>PostProcessTA()> client.Do failed. err: %v" , err )
l . Printf ( "[ERR] client.go>PostProcessTA()> client.Do failed. resp: %v" , resp )
return nil , icserror . ICSERRHTTPClientPostRequest
}
l . Printf ( "[LOG] client.go>PostProcessTA()> client.Do() resp: %v" , resp )
resBody , err := io . ReadAll ( resp . Body )
if err != nil {
l . Printf ( "[ERR] client.go>PostProcessTA()> ReadAll failed. err: %v" , err )
return nil , icserror . ICSERRRead
}
defer resp . Body . Close ( )
if err := json . Unmarshal ( resBody , & tAResp ) ; err != nil {
l . Printf ( "[ERR] client.go>PostProcessTA()> Unmarshal failed. err: %v" , err )
return nil , icserror . ICSERRUnmarshal
}
return tAResp , nil
}