generated from okp4/template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from okp4/feat/resolve-identity
Feat/resolve identity
- Loading branch information
Showing
12 changed files
with
176 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package keybase | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/patrickmn/go-cache" | ||
) | ||
|
||
type Client struct { | ||
client *resty.Client | ||
picturesCache *cache.Cache | ||
} | ||
|
||
func NewClient() *Client { | ||
return &Client{ | ||
client: resty.New(), | ||
picturesCache: cache.New(time.Hour, time.Hour), | ||
} | ||
} | ||
|
||
type lookupPayload struct { | ||
Them []struct { | ||
Pictures *struct { | ||
Primary *struct { | ||
URL *string | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *Client) LookupPicture(ctx context.Context, kid string) (*url.URL, error) { | ||
if picture, ok := c.picturesCache.Get(kid); ok { | ||
return picture.(*url.URL), nil | ||
} | ||
|
||
lookup, err := c.lookup(ctx, kid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var picture *url.URL | ||
if len(lookup.Them) > 0 && | ||
lookup.Them[0].Pictures != nil && | ||
lookup.Them[0].Pictures.Primary != nil && | ||
lookup.Them[0].Pictures.Primary.URL != nil { | ||
picture, err = url.Parse(*lookup.Them[0].Pictures.Primary.URL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.picturesCache.SetDefault(kid, picture) | ||
} | ||
return picture, err | ||
} | ||
|
||
func (c *Client) lookup(ctx context.Context, kid string) (*lookupPayload, error) { | ||
var lookup lookupPayload | ||
res, err := c.client.R(). | ||
SetContext(ctx). | ||
SetHeader("Accept", "application/json"). | ||
SetQueryParam("key_suffix", kid). | ||
SetQueryParam("fields", "pictures"). | ||
SetResult(&lookup). | ||
Get("https://keybase.io/_/api/1.0/user/lookup.json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if res.StatusCode() != http.StatusOK { | ||
return nil, fmt.Errorf("got non 200 http code: '%d'", res.StatusCode()) | ||
} | ||
return &lookup, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package scalar | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/99designs/gqlgen/graphql" | ||
) | ||
|
||
func UnmarshalKID(v interface{}) (string, error) { | ||
kid, ok := v.(string) | ||
if !ok { | ||
return "", fmt.Errorf("expect type 'string' and got type '%T'", v) | ||
} | ||
|
||
raw, err := hex.DecodeString(kid) | ||
if err != nil { | ||
return "", err | ||
} | ||
if len(raw) != 35 { | ||
return "", fmt.Errorf("expect a 35 bytes hex encoded key") | ||
} | ||
|
||
return kid, nil | ||
} | ||
|
||
func MarshalKID(kid string) graphql.Marshaler { | ||
return graphql.WriterFunc(func(w io.Writer) { | ||
_, _ = w.Write([]byte(`"`)) | ||
_, _ = w.Write([]byte(kid)) | ||
_, _ = w.Write([]byte(`"`)) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.