-
Notifications
You must be signed in to change notification settings - Fork 2
/
application.go
144 lines (129 loc) · 5.48 KB
/
application.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
//"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/jl987-Jie/structomap"
"github.com/pjebs/restgate"
"github.com/wcmc-its/pubmed-retrieval-service/model"
"github.com/wcmc-its/pubmed-retrieval-service/querybuilder"
"github.com/wcmc-its/pubmed-retrieval-service/xmlretriever"
"net/http"
//"strings"
)
var r *gin.Engine
func main() {
r = gin.Default()
// Initialize Restgate
rg := restgate.New("X-Auth-Key", "X-Auth-Secret", restgate.Static, restgate.Config{
Key: []string{"12345"},
Secret: []string{"secret"},
HTTPSProtectionOff: true})
// Create Gin middleware - integrate Restgate with Gin
rgAdapter := func(c *gin.Context) {
nextCalled := false
nextAdapter := func(http.ResponseWriter, *http.Request) {
nextCalled = true
c.Next()
}
rg.ServeHTTP(c.Writer, c.Request, nextAdapter)
if nextCalled == false {
c.AbortWithStatus(401)
}
}
// Use Restgate with Gin
r.Use(rgAdapter)
v1 := r.Group("v1/article/pmid=:pmid/")
{
// v1.GET("/articles/*term", GetArticlesByTerm)
// v1.GET("/count/:term", GetCountByTerm)
// v1.GET("/article/pmid/:pmid", GetArticleByPmid)
// v1.GET("/article/grantlist/:pmid", GetArticleGrantList)
// v1.GET("/article/meshheadinglist/:pmid", GetArticleMeshHeadingList)
// v1.GET("/article/otherid/:pmid", GetArticleOtherID)
// v1.GET("/article/medlinecitationstatus/:pmid", GetArticleMedlineCitationStatus)
// v1.GET("/article/publicationtypelist/:pmid", GetArticlePublicationTypeList)
v1.GET("fields=:fields", GetArticleByFields)
}
r.Run(":5000")
}
// localhost:5000/api/v1/article/pmid/1234567/fields=pubmedarticle(medlinecitation(pmid))
func GetArticleByFields(c *gin.Context) {
term := c.Params.ByName("pmid")
fields := c.Params.ByName("fields")
fmt.Println("fields=" + fields)
var esearchResult model.ESearchResult = xmlretriever.EsearchRetrieve(querybuilder.BuildESearchQuery(term))
var efetchurl string = querybuilder.BuildEFetchQuery(esearchResult.WebEnv, 0)
var result model.PubmedArticleSet = xmlretriever.EfetchRetrieve(efetchurl)
if len(result.PubmedArticles) > 0 {
userSerializer := structomap.New().Pick(fields)
userMap := userSerializer.Transform(result.PubmedArticles[0])
c.JSON(200, userMap)
}
}
func GetArticlesByTerm(c *gin.Context) {
term := c.Params.ByName("term")
var esearchResult model.ESearchResult = xmlretriever.EsearchRetrieve(querybuilder.BuildESearchQuery(term))
var efetchurl string = querybuilder.BuildEFetchQuery(esearchResult.WebEnv, 0)
var result model.PubmedArticleSet = xmlretriever.EfetchRetrieve(efetchurl)
c.JSON(200, result)
}
func GetCountByTerm(c *gin.Context) {
term := c.Params.ByName("term")
var esearchResult model.ESearchResult = xmlretriever.EsearchRetrieve(querybuilder.BuildESearchQuery(term))
c.JSON(200, esearchResult.Count)
}
func GetArticleByPmid(c *gin.Context) {
term := c.Params.ByName("pmid")
var esearchResult model.ESearchResult = xmlretriever.EsearchRetrieve(querybuilder.BuildESearchQuery(term))
var efetchurl string = querybuilder.BuildEFetchQuery(esearchResult.WebEnv, 0)
var result model.PubmedArticleSet = xmlretriever.EfetchRetrieve(efetchurl)
if len(result.PubmedArticles) > 0 {
c.JSON(200, result.PubmedArticles[0])
}
}
func GetArticleMeshHeadingList(c *gin.Context) {
term := c.Params.ByName("pmid")
var esearchResult model.ESearchResult = xmlretriever.EsearchRetrieve(querybuilder.BuildESearchQuery(term))
var efetchurl string = querybuilder.BuildEFetchQuery(esearchResult.WebEnv, 0)
var result model.PubmedArticleSet = xmlretriever.EfetchRetrieve(efetchurl)
if len(result.PubmedArticles) > 0 {
c.JSON(200, result.PubmedArticles[0].MedlineCitation.MeshHeadingList)
}
}
func GetArticleGrantList(c *gin.Context) {
term := c.Params.ByName("pmid")
var esearchResult model.ESearchResult = xmlretriever.EsearchRetrieve(querybuilder.BuildESearchQuery(term))
var efetchurl string = querybuilder.BuildEFetchQuery(esearchResult.WebEnv, 0)
var result model.PubmedArticleSet = xmlretriever.EfetchRetrieve(efetchurl)
if len(result.PubmedArticles) > 0 {
c.JSON(200, result.PubmedArticles[0].MedlineCitation.Article.GrantList)
}
}
func GetArticleOtherID(c *gin.Context) {
term := c.Params.ByName("pmid")
var esearchResult model.ESearchResult = xmlretriever.EsearchRetrieve(querybuilder.BuildESearchQuery(term))
var efetchurl string = querybuilder.BuildEFetchQuery(esearchResult.WebEnv, 0)
var result model.PubmedArticleSet = xmlretriever.EfetchRetrieve(efetchurl)
if len(result.PubmedArticles) > 0 {
c.JSON(200, result.PubmedArticles[0].MedlineCitation.OtherID)
}
}
func GetArticleMedlineCitationStatus(c *gin.Context) {
term := c.Params.ByName("pmid")
var esearchResult model.ESearchResult = xmlretriever.EsearchRetrieve(querybuilder.BuildESearchQuery(term))
var efetchurl string = querybuilder.BuildEFetchQuery(esearchResult.WebEnv, 0)
var result model.PubmedArticleSet = xmlretriever.EfetchRetrieve(efetchurl)
if len(result.PubmedArticles) > 0 {
c.JSON(200, result.PubmedArticles[0].MedlineCitation.Status)
}
}
func GetArticlePublicationTypeList(c *gin.Context) {
term := c.Params.ByName("pmid")
var esearchResult model.ESearchResult = xmlretriever.EsearchRetrieve(querybuilder.BuildESearchQuery(term))
var efetchurl string = querybuilder.BuildEFetchQuery(esearchResult.WebEnv, 0)
var result model.PubmedArticleSet = xmlretriever.EfetchRetrieve(efetchurl)
if len(result.PubmedArticles) > 0 {
c.JSON(200, result.PubmedArticles[0].MedlineCitation.Article.PublicationTypeList)
}
}