🚚 Renamed util package to httputil
This commit is contained in:
parent
3266fb1e57
commit
f97d0ba272
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
"source.toby3d.me/toby3d/auth/internal/client"
|
||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
||||
"source.toby3d.me/toby3d/auth/internal/util"
|
||||
"source.toby3d.me/toby3d/auth/internal/httputil"
|
||||
)
|
||||
|
||||
type httpClientRepository struct {
|
||||
|
@ -64,20 +64,20 @@ func (repo *httpClientRepository) Get(ctx context.Context, cid *domain.ClientID)
|
|||
|
||||
//nolint: gocognit, cyclop
|
||||
func extract(dst *domain.Client, src *http.Response) {
|
||||
for _, endpoint := range util.ExtractEndpoints(src, relRedirectURI) {
|
||||
for _, endpoint := range httputil.ExtractEndpoints(src, relRedirectURI) {
|
||||
if !containsURL(dst.RedirectURI, endpoint) {
|
||||
dst.RedirectURI = append(dst.RedirectURI, endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
for _, itemType := range []string{hXApp, hApp} {
|
||||
for _, name := range util.ExtractProperty(src, itemType, propertyName) {
|
||||
for _, name := range httputil.ExtractProperty(src, itemType, propertyName) {
|
||||
if n, ok := name.(string); ok && !containsString(dst.Name, n) {
|
||||
dst.Name = append(dst.Name, n)
|
||||
}
|
||||
}
|
||||
|
||||
for _, logo := range util.ExtractProperty(src, itemType, propertyLogo) {
|
||||
for _, logo := range httputil.ExtractProperty(src, itemType, propertyLogo) {
|
||||
var (
|
||||
uri *domain.URL
|
||||
err error
|
||||
|
@ -99,7 +99,7 @@ func extract(dst *domain.Client, src *http.Response) {
|
|||
dst.Logo = append(dst.Logo, uri)
|
||||
}
|
||||
|
||||
for _, property := range util.ExtractProperty(src, itemType, propertyURL) {
|
||||
for _, property := range httputil.ExtractProperty(src, itemType, propertyURL) {
|
||||
prop, ok := property.(string)
|
||||
if !ok {
|
||||
continue
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package util
|
||||
package httputil
|
||||
|
||||
import (
|
||||
"bytes"
|
|
@ -1,11 +1,11 @@
|
|||
package util_test
|
||||
package httputil_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
http "github.com/valyala/fasthttp"
|
||||
|
||||
"source.toby3d.me/toby3d/auth/internal/util"
|
||||
"source.toby3d.me/toby3d/auth/internal/httputil"
|
||||
)
|
||||
|
||||
const testBody = `<html>
|
||||
|
@ -23,7 +23,7 @@ func TestExtractProperty(t *testing.T) {
|
|||
defer http.ReleaseResponse(resp)
|
||||
resp.SetBodyString(testBody)
|
||||
|
||||
results := util.ExtractProperty(resp, "h-card", "name")
|
||||
results := httputil.ExtractProperty(resp, "h-card", "name")
|
||||
if results == nil || results[0] != "Sample Name" {
|
||||
t.Errorf(`ExtractProperty(resp, "h-card", "name") = %+s, want %+s`, results, []string{"Sample Name"})
|
||||
}
|
|
@ -8,8 +8,8 @@ import (
|
|||
http "github.com/valyala/fasthttp"
|
||||
|
||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
||||
"source.toby3d.me/toby3d/auth/internal/httputil"
|
||||
"source.toby3d.me/toby3d/auth/internal/metadata"
|
||||
"source.toby3d.me/toby3d/auth/internal/util"
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -57,7 +57,7 @@ func (repo *httpMetadataRepository) Get(ctx context.Context, me *domain.Me) (*do
|
|||
return nil, fmt.Errorf("failed to make a request to the client: %w", err)
|
||||
}
|
||||
|
||||
endpoints := util.ExtractEndpoints(resp, "indieauth-metadata")
|
||||
endpoints := httputil.ExtractEndpoints(resp, "indieauth-metadata")
|
||||
if len(endpoints) == 0 {
|
||||
return nil, metadata.ErrNotExist
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ import (
|
|||
http "github.com/valyala/fasthttp"
|
||||
|
||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
||||
"source.toby3d.me/toby3d/auth/internal/httputil"
|
||||
"source.toby3d.me/toby3d/auth/internal/profile"
|
||||
"source.toby3d.me/toby3d/auth/internal/util"
|
||||
)
|
||||
|
||||
type httpProfileRepository struct {
|
||||
|
@ -48,13 +48,13 @@ func (repo *httpProfileRepository) Get(ctx context.Context, me *domain.Me) (*dom
|
|||
|
||||
result := domain.NewProfile()
|
||||
|
||||
for _, name := range util.ExtractProperty(resp, hCard, propertyName) {
|
||||
for _, name := range httputil.ExtractProperty(resp, hCard, propertyName) {
|
||||
if n, ok := name.(string); ok {
|
||||
result.Name = append(result.Name, n)
|
||||
}
|
||||
}
|
||||
|
||||
for _, rawEmail := range util.ExtractProperty(resp, hCard, propertyEmail) {
|
||||
for _, rawEmail := range httputil.ExtractProperty(resp, hCard, propertyEmail) {
|
||||
email, ok := rawEmail.(string)
|
||||
if !ok {
|
||||
continue
|
||||
|
@ -65,7 +65,7 @@ func (repo *httpProfileRepository) Get(ctx context.Context, me *domain.Me) (*dom
|
|||
}
|
||||
}
|
||||
|
||||
for _, rawURL := range util.ExtractProperty(resp, hCard, propertyURL) {
|
||||
for _, rawURL := range httputil.ExtractProperty(resp, hCard, propertyURL) {
|
||||
url, ok := rawURL.(string)
|
||||
if !ok {
|
||||
continue
|
||||
|
@ -76,7 +76,7 @@ func (repo *httpProfileRepository) Get(ctx context.Context, me *domain.Me) (*dom
|
|||
}
|
||||
}
|
||||
|
||||
for _, rawPhoto := range util.ExtractProperty(resp, hCard, propertyPhoto) {
|
||||
for _, rawPhoto := range httputil.ExtractProperty(resp, hCard, propertyPhoto) {
|
||||
photo, ok := rawPhoto.(string)
|
||||
if !ok {
|
||||
continue
|
||||
|
|
|
@ -10,8 +10,8 @@ import (
|
|||
|
||||
"source.toby3d.me/toby3d/auth/internal/common"
|
||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
||||
"source.toby3d.me/toby3d/auth/internal/httputil"
|
||||
"source.toby3d.me/toby3d/auth/internal/ticket"
|
||||
"source.toby3d.me/toby3d/auth/internal/util"
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -62,10 +62,10 @@ func (useCase *ticketUseCase) Generate(ctx context.Context, tkt *domain.Ticket)
|
|||
var ticketEndpoint *domain.URL
|
||||
|
||||
// NOTE(toby3d): find metadata first
|
||||
if metadata, err := util.ExtractMetadata(resp, useCase.client); err == nil && metadata != nil {
|
||||
if metadata, err := httputil.ExtractMetadata(resp, useCase.client); err == nil && metadata != nil {
|
||||
ticketEndpoint = metadata.TicketEndpoint
|
||||
} else { // NOTE(toby3d): fallback to old links searching
|
||||
if endpoints := util.ExtractEndpoints(resp, "ticket_endpoint"); len(endpoints) > 0 {
|
||||
if endpoints := httputil.ExtractEndpoints(resp, "ticket_endpoint"); len(endpoints) > 0 {
|
||||
ticketEndpoint = endpoints[len(endpoints)-1]
|
||||
}
|
||||
}
|
||||
|
@ -110,10 +110,10 @@ func (useCase *ticketUseCase) Redeem(ctx context.Context, tkt *domain.Ticket) (*
|
|||
var tokenEndpoint *domain.URL
|
||||
|
||||
// NOTE(toby3d): find metadata first
|
||||
if metadata, err := util.ExtractMetadata(resp, useCase.client); err == nil && metadata != nil {
|
||||
if metadata, err := httputil.ExtractMetadata(resp, useCase.client); err == nil && metadata != nil {
|
||||
tokenEndpoint = metadata.TokenEndpoint
|
||||
} else { // NOTE(toby3d): fallback to old links searching
|
||||
if endpoints := util.ExtractEndpoints(resp, "token_endpoint"); len(endpoints) > 0 {
|
||||
if endpoints := httputil.ExtractEndpoints(resp, "token_endpoint"); len(endpoints) > 0 {
|
||||
tokenEndpoint = endpoints[len(endpoints)-1]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ import (
|
|||
http "github.com/valyala/fasthttp"
|
||||
|
||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
||||
"source.toby3d.me/toby3d/auth/internal/httputil"
|
||||
"source.toby3d.me/toby3d/auth/internal/user"
|
||||
"source.toby3d.me/toby3d/auth/internal/util"
|
||||
)
|
||||
|
||||
type httpUserRepository struct {
|
||||
|
@ -64,7 +64,7 @@ func (repo *httpUserRepository) Get(ctx context.Context, me *domain.Me) (*domain
|
|||
TokenEndpoint: nil,
|
||||
}
|
||||
|
||||
if metadata, err := util.ExtractMetadata(resp, repo.client); err == nil {
|
||||
if metadata, err := httputil.ExtractMetadata(resp, repo.client); err == nil {
|
||||
user.AuthorizationEndpoint = metadata.AuthorizationEndpoint
|
||||
user.Micropub = metadata.MicropubEndpoint
|
||||
user.Microsub = metadata.MicrosubEndpoint
|
||||
|
@ -81,37 +81,37 @@ func (repo *httpUserRepository) Get(ctx context.Context, me *domain.Me) (*domain
|
|||
//nolint: cyclop
|
||||
func extractUser(dst *domain.User, src *http.Response) {
|
||||
if dst.IndieAuthMetadata != nil {
|
||||
if endpoints := util.ExtractEndpoints(src, relIndieAuthMetadata); len(endpoints) > 0 {
|
||||
if endpoints := httputil.ExtractEndpoints(src, relIndieAuthMetadata); len(endpoints) > 0 {
|
||||
dst.IndieAuthMetadata = endpoints[len(endpoints)-1]
|
||||
}
|
||||
}
|
||||
|
||||
if dst.AuthorizationEndpoint == nil {
|
||||
if endpoints := util.ExtractEndpoints(src, relAuthorizationEndpoint); len(endpoints) > 0 {
|
||||
if endpoints := httputil.ExtractEndpoints(src, relAuthorizationEndpoint); len(endpoints) > 0 {
|
||||
dst.AuthorizationEndpoint = endpoints[len(endpoints)-1]
|
||||
}
|
||||
}
|
||||
|
||||
if dst.Micropub == nil {
|
||||
if endpoints := util.ExtractEndpoints(src, relMicropub); len(endpoints) > 0 {
|
||||
if endpoints := httputil.ExtractEndpoints(src, relMicropub); len(endpoints) > 0 {
|
||||
dst.Micropub = endpoints[len(endpoints)-1]
|
||||
}
|
||||
}
|
||||
|
||||
if dst.Microsub == nil {
|
||||
if endpoints := util.ExtractEndpoints(src, relMicrosub); len(endpoints) > 0 {
|
||||
if endpoints := httputil.ExtractEndpoints(src, relMicrosub); len(endpoints) > 0 {
|
||||
dst.Microsub = endpoints[len(endpoints)-1]
|
||||
}
|
||||
}
|
||||
|
||||
if dst.TicketEndpoint == nil {
|
||||
if endpoints := util.ExtractEndpoints(src, relTicketEndpoint); len(endpoints) > 0 {
|
||||
if endpoints := httputil.ExtractEndpoints(src, relTicketEndpoint); len(endpoints) > 0 {
|
||||
dst.TicketEndpoint = endpoints[len(endpoints)-1]
|
||||
}
|
||||
}
|
||||
|
||||
if dst.TokenEndpoint == nil {
|
||||
if endpoints := util.ExtractEndpoints(src, relTokenEndpoint); len(endpoints) > 0 {
|
||||
if endpoints := httputil.ExtractEndpoints(src, relTokenEndpoint); len(endpoints) > 0 {
|
||||
dst.TokenEndpoint = endpoints[len(endpoints)-1]
|
||||
}
|
||||
}
|
||||
|
@ -119,13 +119,13 @@ func extractUser(dst *domain.User, src *http.Response) {
|
|||
|
||||
//nolint: cyclop
|
||||
func extractProfile(dst *domain.Profile, src *http.Response) {
|
||||
for _, name := range util.ExtractProperty(src, hCard, propertyName) {
|
||||
for _, name := range httputil.ExtractProperty(src, hCard, propertyName) {
|
||||
if n, ok := name.(string); ok {
|
||||
dst.Name = append(dst.Name, n)
|
||||
}
|
||||
}
|
||||
|
||||
for _, rawEmail := range util.ExtractProperty(src, hCard, propertyEmail) {
|
||||
for _, rawEmail := range httputil.ExtractProperty(src, hCard, propertyEmail) {
|
||||
email, ok := rawEmail.(string)
|
||||
if !ok {
|
||||
continue
|
||||
|
@ -136,7 +136,7 @@ func extractProfile(dst *domain.Profile, src *http.Response) {
|
|||
}
|
||||
}
|
||||
|
||||
for _, rawURL := range util.ExtractProperty(src, hCard, propertyURL) {
|
||||
for _, rawURL := range httputil.ExtractProperty(src, hCard, propertyURL) {
|
||||
url, ok := rawURL.(string)
|
||||
if !ok {
|
||||
continue
|
||||
|
@ -147,7 +147,7 @@ func extractProfile(dst *domain.Profile, src *http.Response) {
|
|||
}
|
||||
}
|
||||
|
||||
for _, rawPhoto := range util.ExtractProperty(src, hCard, propertyPhoto) {
|
||||
for _, rawPhoto := range httputil.ExtractProperty(src, hCard, propertyPhoto) {
|
||||
photo, ok := rawPhoto.(string)
|
||||
if !ok {
|
||||
continue
|
||||
|
|
Loading…
Reference in New Issue