Skip to content

Commit b27f8fa

Browse files
author
Andrei Skomorkhov
authored
Merge pull request #45 from OlgaMinyaeva/master
Add API method '/grid/session/info'
2 parents c918968 + 1cd952f commit b27f8fa

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,6 @@ Configurations are stored in json files. Example:
120120
| node_list.[].params.port | string | Port of selenium. |
121121

122122
## API
123-
- `/grid/status` - a method returns a status of a grid
123+
- `/grid/status` - a method returns a status of a grid
124+
- `/grid/session/info` - a returns a session info by session id.
125+
Еxample: `curl -X http://localhost:4444/grid/session/info?sessionid=9fc185d2-7a3d-4660-877f-cd4ca2a2f5c3`

handlers/sessionInfo.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package handlers
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
8+
log "github.com/sirupsen/logrus"
9+
10+
"github.com/qa-dev/jsonwire-grid/pool"
11+
)
12+
13+
// SessionInfo - Returns a session info (node address, status, etc)
14+
type SessionInfo struct {
15+
Pool *pool.Pool
16+
}
17+
18+
type sessionInfoResponse struct {
19+
NodeAddress string `json:"node_address"`
20+
NodeType pool.NodeType `json:"node_type"`
21+
Status pool.NodeStatus `json:"node_status"`
22+
SessionID string `json:"session_id"`
23+
Updated int64 `json:"updated"`
24+
Registered int64 `json:"registered"`
25+
}
26+
27+
func (h *SessionInfo) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
28+
sessionId := r.URL.Query().Get("sessionid")
29+
if sessionId == "" {
30+
http.Error(rw, fmt.Sprint("session id must be specified,"), http.StatusBadRequest)
31+
return
32+
}
33+
node, err := h.Pool.GetNodeBySessionID(sessionId)
34+
if err != nil {
35+
http.Error(rw, fmt.Sprint("trying to get a session data,", err), http.StatusInternalServerError)
36+
return
37+
}
38+
39+
resp := sessionInfoResponse{
40+
node.Address,
41+
node.Type,
42+
node.Status,
43+
node.SessionID,
44+
node.Updated,
45+
node.Registered,
46+
}
47+
respJSON, err := json.Marshal(resp)
48+
if err != nil {
49+
http.Error(rw, fmt.Sprint("trying to encode a response,", err), http.StatusInternalServerError)
50+
return
51+
}
52+
53+
_, err = rw.Write(respJSON)
54+
if err != nil {
55+
log.Error("session/info: write a response,", err)
56+
}
57+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func main() {
117117
http.Handle("/session", middlewareWrap.Do(&handlers.CreateSession{Pool: poolInstance, ClientFactory: clientFactory})) //wda
118118
http.Handle("/grid/register", middlewareWrap.Do(&handlers.RegisterNode{Pool: poolInstance}))
119119
http.Handle("/grid/status", middlewareWrap.Do(&handlers.GridStatus{Pool: poolInstance, Config: *cfg}))
120+
http.Handle("/grid/session/info", middlewareWrap.Do(&handlers.SessionInfo{Pool: poolInstance}))
120121
http.Handle("/grid/api/proxy", &handlers.APIProxy{Pool: poolInstance})
121122
http.HandleFunc("/_info", heartbeat)
122123
http.Handle("/", middlewareWrap.Do(&handlers.UseSession{Pool: poolInstance, Cache: cache}))

0 commit comments

Comments
 (0)