diff --git a/go.mod b/go.mod index ad0f4e4..0b5009c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/modules/nlp/address.go b/modules/nlp/address.go index 611b4f4..0d8e7ed 100644 --- a/modules/nlp/address.go +++ b/modules/nlp/address.go @@ -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" @@ -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 } diff --git a/modules/nlp/comment.go b/modules/nlp/comment.go index ed5748a..a52ca23 100644 --- a/modules/nlp/comment.go +++ b/modules/nlp/comment.go @@ -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" @@ -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 } diff --git a/modules/nlp/deparser.go b/modules/nlp/deparser.go index e3aa5db..c5faceb 100644 --- a/modules/nlp/deparser.go +++ b/modules/nlp/deparser.go @@ -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" @@ -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 } diff --git a/modules/nlp/dnnlm_cn.go b/modules/nlp/dnnlm_cn.go index b430770..f70a045 100644 --- a/modules/nlp/dnnlm_cn.go +++ b/modules/nlp/dnnlm_cn.go @@ -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" @@ -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 } diff --git a/modules/nlp/ecnet.go b/modules/nlp/ecnet.go index 90089c9..6b1c514 100644 --- a/modules/nlp/ecnet.go +++ b/modules/nlp/ecnet.go @@ -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" @@ -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 } diff --git a/modules/nlp/emotion.go b/modules/nlp/emotion.go index 101d568..c5044c7 100644 --- a/modules/nlp/emotion.go +++ b/modules/nlp/emotion.go @@ -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" @@ -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 } diff --git a/modules/nlp/entity_level_sentiment.go b/modules/nlp/entity_level_sentiment.go index 4b5c09b..044977f 100644 --- a/modules/nlp/entity_level_sentiment.go +++ b/modules/nlp/entity_level_sentiment.go @@ -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" @@ -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 } // 实体库新增接口 @@ -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 } // 实体库新增接口 @@ -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 } // 实体库删除接口请求说明 @@ -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 } // 实体名单查询接口请求说明 @@ -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 } // 实体名单删除接口请求说明 @@ -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 } diff --git a/modules/nlp/keyword.go b/modules/nlp/keyword.go index 8fd2467..4098084 100644 --- a/modules/nlp/keyword.go +++ b/modules/nlp/keyword.go @@ -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" @@ -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 } diff --git a/modules/nlp/lexer.go b/modules/nlp/lexer.go index 894af2f..482c321 100644 --- a/modules/nlp/lexer.go +++ b/modules/nlp/lexer.go @@ -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" @@ -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 } diff --git a/modules/nlp/news_summary.go b/modules/nlp/news_summary.go index eeb1864..9f3c51d 100644 --- a/modules/nlp/news_summary.go +++ b/modules/nlp/news_summary.go @@ -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" @@ -35,11 +36,15 @@ func (m NewsSummary) Default(title, content string, maxSummaryLen int) (NewsSumm body := utils.MustJson(NewsSummaryBody{title, content, maxSummaryLen}) logrus.Debugf("[news_summary] %s", body) - iresp, err := utils.CommonResponse(aip.Post(news_summary).Send(string(body)), resp) - if err != nil { - return resp, err + _, respBody, errs := aip.Post(news_summary).Send(string(body)).EndStruct(&resp) + if len(errs) > 0 { + return resp, errs[0] } + logrus.Debugf("response body: %s", respBody) - finalResp, _ := iresp.(NewsSummaryResponse) - return finalResp, err + if resp.ErrorCode > 0 { + return resp, errors.Errorf(resp.ErrorMsg) + } + + return resp, nil } diff --git a/modules/nlp/sentiment_classify.go b/modules/nlp/sentiment_classify.go index f3ad34c..b5cb1a4 100644 --- a/modules/nlp/sentiment_classify.go +++ b/modules/nlp/sentiment_classify.go @@ -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" @@ -48,11 +49,15 @@ func (m SentimentClassify) doRequest(url, text string) (SentimentClassifyRespons body := utils.MustJson(SentimentClassifyBody{text}) logrus.Debugf("[sentiment_classify] %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.(SentimentClassifyResponse) - return finalResp, err + if resp.ErrorCode > 0 { + return resp, errors.Errorf(resp.ErrorMsg) + } + + return resp, nil } diff --git a/modules/nlp/simnet.go b/modules/nlp/simnet.go index c312ccd..019d697 100644 --- a/modules/nlp/simnet.go +++ b/modules/nlp/simnet.go @@ -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" @@ -39,11 +40,15 @@ func (m Simnet) Default(first, second string) (SimnetResponse, error) { body := utils.MustJson(SimnetBody{first, second}) logrus.Debugf("[simnet] %s", body) - iresp, err := utils.CommonResponse(aip.Post(simnet).Send(string(body)), resp) - if err != nil { - return resp, err + _, respBody, errs := aip.Post(simnet).Send(string(body)).EndStruct(&resp) + if len(errs) > 0 { + return resp, errs[0] } + logrus.Debugf("response body: %s", respBody) - finalResp, _ := iresp.(SimnetResponse) - return finalResp, err + if resp.ErrorCode > 0 { + return resp, errors.Errorf(resp.ErrorMsg) + } + + return resp, nil } diff --git a/modules/nlp/topic.go b/modules/nlp/topic.go index 34e0e8f..2711f05 100644 --- a/modules/nlp/topic.go +++ b/modules/nlp/topic.go @@ -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" @@ -25,8 +26,7 @@ type TopicBody struct { type TopicResponse struct { modules.BaseResp - LogID int64 `json:"log_id"` - Item struct { + Item struct { Lv2TagList []struct { Score float64 `json:"score"` Tag string `json:"tag"` @@ -44,11 +44,15 @@ func (m Topic) Default(title, content string) (TopicResponse, error) { body := utils.MustJson(TopicBody{title, content}) logrus.Debugf("[topic] %s", body) - iresp, err := utils.CommonResponse(aip.Post(topic).Send(string(body)), resp) - if err != nil { - return resp, err + _, respBody, errs := aip.Post(topic).Send(string(body)).EndStruct(&resp) + if len(errs) > 0 { + return resp, errs[0] } + logrus.Debugf("response body: %s", respBody) - finalResp, _ := iresp.(TopicResponse) - return finalResp, err + if resp.ErrorCode > 0 { + return resp, errors.Errorf(resp.ErrorMsg) + } + + return resp, nil } diff --git a/modules/nlp/word_emb_sim.go b/modules/nlp/word_emb_sim.go index 3ef852f..75ce725 100644 --- a/modules/nlp/word_emb_sim.go +++ b/modules/nlp/word_emb_sim.go @@ -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" @@ -38,11 +39,15 @@ func (m WordEmbSim) Default(first, second string) (WordEmbSimResponse, error) { body := utils.MustJson(WordEmbSimBody{first, second}) logrus.Debugf("[word_emb_sim] %s", body) - iresp, err := utils.CommonResponse(aip.Post(word_emb_sim).Send(string(body)), resp) - if err != nil { - return resp, err + _, respBody, errs := aip.Post(word_emb_sim).Send(string(body)).EndStruct(&resp) + if len(errs) > 0 { + return resp, errs[0] } + logrus.Debugf("response body: %s", respBody) - finalResp, _ := iresp.(WordEmbSimResponse) - return finalResp, err + if resp.ErrorCode > 0 { + return resp, errors.Errorf(resp.ErrorMsg) + } + + return resp, nil } diff --git a/modules/nlp/word_emb_vec.go b/modules/nlp/word_emb_vec.go index b42ead2..3d52d3d 100644 --- a/modules/nlp/word_emb_vec.go +++ b/modules/nlp/word_emb_vec.go @@ -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" @@ -34,11 +35,15 @@ func (m WordEmbVec) Default(word string) (WordEmbVecResponse, error) { body := utils.MustJson(WordEmbVecBody{Word: word}) logrus.Debugf("[word_emb_vec] %s", body) - iresp, err := utils.CommonResponse(aip.Post(word_emb_vec).Send(string(body)), resp) - if err != nil { - return resp, err + _, respBody, errs := aip.Post(word_emb_vec).Send(string(body)).EndStruct(&resp) + if len(errs) > 0 { + return resp, errs[0] } + logrus.Debugf("response body: %s", respBody) - finalResp, _ := iresp.(WordEmbVecResponse) - return finalResp, err + if resp.ErrorCode > 0 { + return resp, errors.Errorf(resp.ErrorMsg) + } + + return resp, nil } diff --git a/utils/response.go b/utils/response.go deleted file mode 100644 index 6dc913a..0000000 --- a/utils/response.go +++ /dev/null @@ -1,23 +0,0 @@ -package utils - -import ( - "github.com/parnurzeal/gorequest" - "github.com/rogeecn/aip/modules" - "github.com/sirupsen/logrus" - "github.com/juju/errors" -) - -func CommonResponse(agent *gorequest.SuperAgent, resp interface{}) (interface{}, error) { - _, respBody, errs := agent.EndStruct(&resp) - if len(errs) > 0 { - return resp, errs[0] - } - logrus.Debugf("response body: %s", respBody) - - baseResp, _ := resp.(modules.BaseResp) - if baseResp.ErrorCode != 0 { - return resp, errors.Errorf("lexer response error: %s", baseResp.ErrorMsg) - } - - return resp, nil -}