github.com/uvalib/orcid-access-ws@v0.0.0-20250612130209-7d062dbabf9d/orcidaccessws/client/client.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/parnurzeal/gorequest" 7 "github.com/uvalib/orcid-access-ws/orcidaccessws/api" 8 "io" 9 "io/ioutil" 10 "net/http" 11 "time" 12 ) 13 14 var debugHTTP = false 15 var serviceTimeout = 30 16 17 // HealthCheck -- calls the service health check method 18 func HealthCheck(endpoint string) int { 19 20 url := fmt.Sprintf("%s/healthcheck", endpoint) 21 //fmt.Printf( "%s\n", url ) 22 23 resp, _, errs := gorequest.New(). 24 SetDebug(debugHTTP). 25 Get(url). 26 Timeout(time.Duration(serviceTimeout) * time.Second). 27 End() 28 29 if errs != nil { 30 return http.StatusInternalServerError 31 } 32 33 defer io.Copy(ioutil.Discard, resp.Body) 34 defer resp.Body.Close() 35 36 return resp.StatusCode 37 } 38 39 // VersionCheck -- calls the service version check method 40 func VersionCheck(endpoint string) (int, string) { 41 42 url := fmt.Sprintf("%s/version", endpoint) 43 //fmt.Printf( "%s\n", url ) 44 45 resp, body, errs := gorequest.New(). 46 SetDebug(debugHTTP). 47 Get(url). 48 Timeout(time.Duration(serviceTimeout) * time.Second). 49 End() 50 51 if errs != nil { 52 return http.StatusInternalServerError, "" 53 } 54 55 defer io.Copy(ioutil.Discard, resp.Body) 56 defer resp.Body.Close() 57 58 r := api.VersionResponse{} 59 err := json.Unmarshal([]byte(body), &r) 60 if err != nil { 61 return http.StatusInternalServerError, "" 62 } 63 64 return resp.StatusCode, r.Version 65 } 66 67 // MetricsCheck -- calls the service metrics method 68 func MetricsCheck(endpoint string) (int, string) { 69 70 url := fmt.Sprintf("%s/metrics", endpoint) 71 //fmt.Printf( "%s\n", url ) 72 73 resp, body, errs := gorequest.New(). 74 SetDebug(false). 75 Get(url). 76 Timeout(time.Duration(serviceTimeout) * time.Second). 77 End() 78 79 if errs != nil { 80 return http.StatusInternalServerError, "" 81 } 82 83 defer io.Copy(ioutil.Discard, resp.Body) 84 defer resp.Body.Close() 85 86 return resp.StatusCode, body 87 } 88 89 // GetOrcidAttributes -- call the get orcid attributes method on the service 90 func GetOrcidAttributes(endpoint string, cid string, token string) (int, []*api.OrcidAttributes) { 91 92 url := fmt.Sprintf("%s/cid/%s?auth=%s", endpoint, cid, token) 93 //fmt.Printf( "%s\n", url ) 94 95 resp, body, errs := gorequest.New(). 96 SetDebug(debugHTTP). 97 Get(url). 98 Timeout(time.Duration(serviceTimeout) * time.Second). 99 End() 100 101 if errs != nil { 102 return http.StatusInternalServerError, nil 103 } 104 105 defer io.Copy(ioutil.Discard, resp.Body) 106 defer resp.Body.Close() 107 108 r := api.OrcidAttributesResponse{} 109 err := json.Unmarshal([]byte(body), &r) 110 if err != nil { 111 return http.StatusInternalServerError, nil 112 } 113 114 return resp.StatusCode, r.Attributes 115 } 116 117 // GetAllOrcidAttributes -- call get all orcid attributes on the service 118 func GetAllOrcidAttributes(endpoint string, token string) (int, []*api.OrcidAttributes) { 119 120 url := fmt.Sprintf("%s/cid?auth=%s", endpoint, token) 121 //fmt.Printf( "%s\n", url ) 122 123 resp, body, errs := gorequest.New(). 124 SetDebug(debugHTTP). 125 Get(url). 126 Timeout(time.Duration(serviceTimeout) * time.Second). 127 End() 128 129 if errs != nil { 130 return http.StatusInternalServerError, nil 131 } 132 133 defer io.Copy(ioutil.Discard, resp.Body) 134 defer resp.Body.Close() 135 136 r := api.OrcidAttributesResponse{} 137 err := json.Unmarshal([]byte(body), &r) 138 if err != nil { 139 return http.StatusInternalServerError, nil 140 } 141 142 return resp.StatusCode, r.Attributes 143 } 144 145 // SetOrcidAttributes -- call set orcid attributes on the service 146 func SetOrcidAttributes(endpoint string, cid string, token string, attributes api.OrcidAttributes) int { 147 148 url := fmt.Sprintf("%s/cid/%s?auth=%s", endpoint, cid, token) 149 //fmt.Printf( "%s\n", url ) 150 151 resp, _, errs := gorequest.New(). 152 SetDebug(debugHTTP). 153 Put(url). 154 Send(attributes). 155 Timeout(time.Duration(serviceTimeout)*time.Second). 156 Set("Content-Type", "application/json"). 157 End() 158 159 if errs != nil { 160 return http.StatusInternalServerError 161 } 162 163 defer io.Copy(ioutil.Discard, resp.Body) 164 defer resp.Body.Close() 165 166 return resp.StatusCode 167 } 168 169 // DelOrcidAttributes -- call delete orcid attributes on the service 170 func DelOrcidAttributes(endpoint string, cid string, token string) int { 171 172 url := fmt.Sprintf("%s/cid/%s?auth=%s", endpoint, cid, token) 173 //fmt.Printf( "%s\n", url ) 174 175 resp, body, errs := gorequest.New(). 176 SetDebug(debugHTTP). 177 Delete(url). 178 Timeout(time.Duration(serviceTimeout) * time.Second). 179 End() 180 181 if errs != nil { 182 return http.StatusInternalServerError 183 } 184 185 defer io.Copy(ioutil.Discard, resp.Body) 186 defer resp.Body.Close() 187 188 r := api.StandardResponse{} 189 err := json.Unmarshal([]byte(body), &r) 190 if err != nil { 191 return http.StatusInternalServerError 192 } 193 194 return resp.StatusCode 195 } 196 197 // UpdateActivity -- call update activity on the service 198 func UpdateActivity(endpoint string, cid string, token string, activity api.ActivityUpdate) (int, string) { 199 200 url := fmt.Sprintf("%s/cid/%s/activity?auth=%s", endpoint, cid, token) 201 //fmt.Printf( "%s\n", url ) 202 203 resp, body, errs := gorequest.New(). 204 SetDebug(debugHTTP). 205 Put(url). 206 Send(activity). 207 Timeout(time.Duration(serviceTimeout)*time.Second). 208 Set("Content-Type", "application/json"). 209 End() 210 211 if errs != nil { 212 return http.StatusInternalServerError, "" 213 } 214 215 defer io.Copy(ioutil.Discard, resp.Body) 216 defer resp.Body.Close() 217 218 r := api.UpdateActivityResponse{} 219 err := json.Unmarshal([]byte(body), &r) 220 if err != nil { 221 return http.StatusInternalServerError, "" 222 } 223 224 return resp.StatusCode, r.UpdateCode 225 } 226 227 // GetOrcidDetails -- call get orcid details on the service 228 func GetOrcidDetails(endpoint string, orcid string, token string) (int, *api.OrcidDetails) { 229 230 url := fmt.Sprintf("%s/orcid/%s?auth=%s", endpoint, orcid, token) 231 //fmt.Printf( "%s\n", url ) 232 233 resp, body, errs := gorequest.New(). 234 SetDebug(debugHTTP). 235 Get(url). 236 Timeout(time.Duration(serviceTimeout) * time.Second). 237 End() 238 239 if errs != nil { 240 return http.StatusInternalServerError, nil 241 } 242 243 defer io.Copy(ioutil.Discard, resp.Body) 244 defer resp.Body.Close() 245 246 r := api.OrcidDetailsResponse{} 247 err := json.Unmarshal([]byte(body), &r) 248 if err != nil { 249 return http.StatusInternalServerError, nil 250 } 251 252 return resp.StatusCode, r.Details 253 } 254 255 // SearchOrcid -- call search orcid on the service 256 func SearchOrcid(endpoint string, search string, start string, max string, token string) (int, []*api.OrcidDetails, int) { 257 258 url := fmt.Sprintf("%s/orcid?q=%s&start=%s&max=%s&auth=%s", endpoint, search, start, max, token) 259 //fmt.Printf( "%s\n", url ) 260 261 resp, body, errs := gorequest.New(). 262 SetDebug(debugHTTP). 263 Get(url). 264 Timeout(time.Duration(serviceTimeout) * time.Second). 265 End() 266 267 if errs != nil { 268 return http.StatusInternalServerError, nil, 0 269 } 270 271 defer io.Copy(ioutil.Discard, resp.Body) 272 defer resp.Body.Close() 273 274 r := api.OrcidSearchResponse{} 275 err := json.Unmarshal([]byte(body), &r) 276 if err != nil { 277 return http.StatusInternalServerError, nil, 0 278 } 279 280 return resp.StatusCode, r.Results, r.Total 281 } 282 283 // 284 // end of file 285 //