-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouteOperator.go
103 lines (87 loc) · 2.74 KB
/
routeOperator.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
package main
import (
"encoding/json"
"github.com/ethereum/go-ethereum/common"
"github.com/julienschmidt/httprouter"
"net/http"
)
type RouteOperator struct {
ensOperator *EnsOperator
}
func (routeOperator RouteOperator) getName(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
name, err := routeOperator.ensOperator.ResolveAddress(ps.ByName("address"))
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
// request was performed successfully but the address does not have any ens name registered (returned empty string)
if name == "" {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(name)
}
func (routeOperator RouteOperator) getAddress(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
address, err := routeOperator.ensOperator.ResolveName(ps.ByName("name"))
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(address)
}
// for testing purposes - eth.ens has avatar set
func (routeOperator RouteOperator) getAvatar(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
avatar, err := routeOperator.ensOperator.GetAvatarByName(ps.ByName("name"))
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
// todo handle empty string
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(avatar)
}
type GetAllResponse struct {
address string
name string
avatar string
}
// param could be either address or ENS name
func (routeOperator RouteOperator) getAll(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
param := ps.ByName("param")
if common.IsHexAddress(param) {
name, err := routeOperator.ensOperator.ResolveAddress(param)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
var avatar string
avatar, err = routeOperator.ensOperator.GetAvatarByName(name)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(GetAllResponse{address: param, name: name, avatar: avatar})
} else {
address, err := routeOperator.ensOperator.ResolveName(param)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
var avatar string
avatar, err = routeOperator.ensOperator.GetAvatarByName(param)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(GetAllResponse{address: address.Hex(), name: param, avatar: avatar})
}
}