Skip to content

Commit 70820c4

Browse files
committed
Add API method '/grid/session/info'. Returns session info( address, port, etc.) by session id
1 parent c918968 commit 70820c4

File tree

3 files changed

+59
-1
lines changed

3 files changed

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

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)