Skip to content

Commit

Permalink
fix bug for common response processor
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeecn committed Feb 9, 2020
1 parent 9fccb38 commit ef73b66
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 129 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.13
require (
github.com/juju/errors v0.0.0-20190930114154-d42613fe1ab9
github.com/parnurzeal/gorequest v0.2.16
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.4.2
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect
moul.io/http2curl v1.0.0 // indirect
Expand Down
15 changes: 10 additions & 5 deletions modules/nlp/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nlp

import (
"github.com/juju/errors"
"github.com/rogeecn/aip"
"github.com/rogeecn/aip/modules"
"github.com/rogeecn/aip/utils"
Expand Down Expand Up @@ -37,11 +38,15 @@ func (m Address) Default(text string) (AddressResponse, error) {
body := utils.MustJson(AddressBody{text})
logrus.Debugf("[address] %s", body)

iresp, err := utils.CommonResponse(aip.Post(address).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(address).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(AddressResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}
15 changes: 10 additions & 5 deletions modules/nlp/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nlp

import (
"github.com/juju/errors"
"github.com/rogeecn/aip"
"github.com/rogeecn/aip/modules"
"github.com/rogeecn/aip/utils"
Expand Down Expand Up @@ -66,11 +67,15 @@ func (m CommentTag) doRequest(url, text string, mode int) (CommentTagResponse, e
body := utils.MustJson(CommentTagBody{text, mode})
logrus.Debugf("[comment_tag] %s", body)

iresp, err := utils.CommonResponse(aip.Post(url).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(url).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(CommentTagResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}
15 changes: 10 additions & 5 deletions modules/nlp/deparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nlp

import (
"github.com/juju/errors"
"github.com/rogeecn/aip"
"github.com/rogeecn/aip/modules"
"github.com/rogeecn/aip/utils"
Expand Down Expand Up @@ -46,11 +47,15 @@ func (m DepParser) Default(text string, mode int) (DepParserResponse, error) {
body := utils.MustJson(DepParserBody{text, mode})
logrus.Debugf("[depparser] %s", body)

iresp, err := utils.CommonResponse(aip.Post(depparser).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(depparser).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(DepParserResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}
15 changes: 10 additions & 5 deletions modules/nlp/dnnlm_cn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nlp

import (
"github.com/juju/errors"
"github.com/rogeecn/aip"
"github.com/rogeecn/aip/modules"
"github.com/rogeecn/aip/utils"
Expand Down Expand Up @@ -38,11 +39,15 @@ func (m Dnn) Default(text string) (DnnResponse, error) {
body := utils.MustJson(DnnBody{text})
logrus.Debugf("[word_emb_sim] %s", body)

iresp, err := utils.CommonResponse(aip.Post(dnnlm_cn).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(dnnlm_cn).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(DnnResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}
15 changes: 10 additions & 5 deletions modules/nlp/ecnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nlp

import (
"github.com/juju/errors"
"github.com/rogeecn/aip"
"github.com/rogeecn/aip/modules"
"github.com/rogeecn/aip/utils"
Expand Down Expand Up @@ -44,11 +45,15 @@ func (m Ecnet) Default(text string) (EcnetResponse, error) {
body := utils.MustJson(EcnetBody{text})
logrus.Debugf("[ecnet] %s", body)

iresp, err := utils.CommonResponse(aip.Post(ecnet).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(ecnet).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(EcnetResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}
15 changes: 10 additions & 5 deletions modules/nlp/emotion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nlp

import (
"github.com/juju/errors"
"github.com/rogeecn/aip"
"github.com/rogeecn/aip/modules"
"github.com/rogeecn/aip/utils"
Expand Down Expand Up @@ -52,11 +53,15 @@ func (m Emotion) Default(text, scene string) (EmotionResponse, error) {
body := utils.MustJson(EmotionBody{text, scene})
logrus.Debugf("[emotion] %s", body)

iresp, err := utils.CommonResponse(aip.Post(emotion).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(emotion).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(EmotionResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}
91 changes: 58 additions & 33 deletions modules/nlp/entity_level_sentiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nlp

import (
"github.com/juju/errors"
"github.com/rogeecn/aip"
"github.com/rogeecn/aip/modules"
"github.com/rogeecn/aip/utils"
Expand Down Expand Up @@ -46,13 +47,17 @@ func (m EntityLevelSentiment) Default(content string) (EntityLevelSentimentRespo
body := utils.MustJson(EntityLevelSentimentBody{content})
logrus.Debugf("[entity_level_sentiment] %s", body)

iresp, err := utils.CommonResponse(aip.Post(entity_level_sentiment).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(entity_level_sentiment).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(EntityLevelSentimentResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}

// 实体库新增接口
Expand All @@ -72,13 +77,17 @@ func (m EntityLevelSentiment) Add(repository string, entities []string) (EntityL
body := utils.MustJson(EntityLevelSentimentAddBody{repository, entities})
logrus.Debugf("[entity_level_sentiment_add] %s", body)

iresp, err := utils.CommonResponse(aip.Post(entity_level_sentiment_add).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(entity_level_sentiment_add).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

finalResp, _ := iresp.(EntityLevelSentimentAddResponse)
return finalResp, err
return resp, nil
}

// 实体库新增接口
Expand All @@ -98,13 +107,17 @@ func (m EntityLevelSentiment) List(repository string) (EntityLevelSentimentListR
body := utils.MustJson(EntityLevelSentimentListBody{repository})
logrus.Debugf("[entity_level_sentiment_list] %s", body)

iresp, err := utils.CommonResponse(aip.Post(entity_level_sentiment_list).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(entity_level_sentiment_list).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(EntityLevelSentimentListResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}

// 实体库删除接口请求说明
Expand All @@ -121,15 +134,19 @@ func (m EntityLevelSentiment) DeleteRepo(repository []string) (EntityLevelSentim
var resp EntityLevelSentimentDeleteRepoResponse

body := utils.MustJson(EntityLevelSentimentDeleteRepoBody{repository})
logrus.Debugf("[entity_level_sentiment_list] %s", body)
logrus.Debugf("[entity_level_sentiment_delete_repo] %s", body)

iresp, err := utils.CommonResponse(aip.Post(entity_level_sentiment_delete_repo).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(entity_level_sentiment_delete_repo).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(EntityLevelSentimentDeleteRepoResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}

// 实体名单查询接口请求说明
Expand All @@ -147,15 +164,19 @@ func (m EntityLevelSentiment) Query(repository string) (EntityLevelSentimentQuer
var resp EntityLevelSentimentQueryResponse

body := utils.MustJson(EntityLevelSentimentQueryBody{repository})
logrus.Debugf("[entity_level_sentiment_list] %s", body)
logrus.Debugf("[entity_level_sentiment_query] %s", body)

_, respBody, errs := aip.Post(entity_level_sentiment_query).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

iresp, err := utils.CommonResponse(aip.Post(entity_level_sentiment_query).Send(string(body)), resp)
if err != nil {
return resp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

finalResp, _ := iresp.(EntityLevelSentimentQueryResponse)
return finalResp, err
return resp, nil
}

// 实体名单删除接口请求说明
Expand All @@ -174,13 +195,17 @@ func (m EntityLevelSentiment) Delete(repository string, entities []string) (Enti
var resp EntityLevelSentimentDeleteResponse

body := utils.MustJson(EntityLevelSentimentDeleteBody{repository, entities})
logrus.Debugf("[entity_level_sentiment_list] %s", body)
logrus.Debugf("[entity_level_sentiment_delete] %s", body)

_, respBody, errs := aip.Post(entity_level_sentiment_delete).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

iresp, err := utils.CommonResponse(aip.Post(entity_level_sentiment_delete).Send(string(body)), resp)
if err != nil {
return resp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

finalResp, _ := iresp.(EntityLevelSentimentDeleteResponse)
return finalResp, err
return resp, nil
}
15 changes: 10 additions & 5 deletions modules/nlp/keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nlp

import (
"github.com/juju/errors"
"github.com/rogeecn/aip"
"github.com/rogeecn/aip/modules"
"github.com/rogeecn/aip/utils"
Expand Down Expand Up @@ -38,11 +39,15 @@ func (m Keyword) Default(title, content string) (KeywordResponse, error) {
body := utils.MustJson(KeywordBody{title, content})
logrus.Debugf("[keyword] %s", body)

iresp, err := utils.CommonResponse(aip.Post(keyword).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(address).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(KeywordResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}
15 changes: 10 additions & 5 deletions modules/nlp/lexer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nlp

import (
"github.com/juju/errors"
"github.com/rogeecn/aip"
"github.com/rogeecn/aip/modules"
"github.com/rogeecn/aip/utils"
Expand Down Expand Up @@ -52,11 +53,15 @@ func (m Lexer) doRequest(url, text string) (LexerResponse, error) {
body := utils.MustJson(LexerBody{Text: text})
logrus.Debugf("[lexer] %s", body)

iresp, err := utils.CommonResponse(aip.Post(url).Send(string(body)), resp)
if err != nil {
return resp, err
_, respBody, errs := aip.Post(url).Send(string(body)).EndStruct(&resp)
if len(errs) > 0 {
return resp, errs[0]
}
logrus.Debugf("response body: %s", respBody)

finalResp, _ := iresp.(LexerResponse)
return finalResp, err
if resp.ErrorCode > 0 {
return resp, errors.Errorf(resp.ErrorMsg)
}

return resp, nil
}
Loading

0 comments on commit ef73b66

Please sign in to comment.