Skip to content

Commit

Permalink
feat: add GetUser & Me REST mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Karitham committed Jan 4, 2022
1 parent 6bcddeb commit f9e70f6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
6 changes: 3 additions & 3 deletions ack.go → internal/rest/verify.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package corde
package rest

import (
"bytes"
Expand All @@ -8,8 +8,8 @@ import (
"net/http"
)

// Validate is a middleware to validate Interaction payloads
func Validate(publicKey string) func(http.Handler) http.Handler {
// Verify is a middleware to verify Interaction payloads
func Verify(publicKey string) func(http.Handler) http.Handler {
pk, err := hex.DecodeString(publicKey)
if err != nil {
panic("invalid public key")
Expand Down
6 changes: 3 additions & 3 deletions ack_test.go → internal/rest/verify_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package corde
package rest

import (
"crypto/ed25519"
Expand All @@ -11,7 +11,7 @@ import (
"time"
)

func TestValidate(t *testing.T) {
func TestVerify(t *testing.T) {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
t.Log(err)
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestValidate(t *testing.T) {
},
}

s := httptest.NewServer(Validate(hex.EncodeToString(pub))(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
s := httptest.NewServer(Verify(hex.EncodeToString(pub))(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(nil)
})))
defer s.Close()
Expand Down
3 changes: 2 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

"github.com/Karitham/corde/internal/rest"
radix "github.com/akrennmair/go-radix"
)

Expand Down Expand Up @@ -139,7 +140,7 @@ type ResponseWriter interface {

// ListenAndServe starts the gateway listening to events
func (m *Mux) ListenAndServe(addr string) error {
validator := Validate(m.PublicKey)
validator := rest.Verify(m.PublicKey)
r := http.NewServeMux()
r.Handle(m.BasePath, validator(http.HandlerFunc(m.route)))

Expand Down
19 changes: 19 additions & 0 deletions users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package corde

import (
"github.com/Karitham/corde/internal/rest"
)

// Me returns the current user
func (m *Mux) Me() (User, error) {
var user User
_, err := rest.DoJson(m.Client, rest.Req("/users/@me").Get(m.authorize), &user)
return user, err
}

// GetUser returns a user by id
func (m *Mux) GetUser(id Snowflake) (User, error) {
var user User
_, err := rest.DoJson(m.Client, rest.Req("/users/", id).Get(m.authorize), &user)
return user, err
}

0 comments on commit f9e70f6

Please sign in to comment.