Skip to content

Commit 370b149

Browse files
designing authorization provider
1 parent 2141736 commit 370b149

6 files changed

+94
-0
lines changed

internal/authorization_handler.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package internal
2+
3+
type AuthorizationHandler struct {
4+
handler BaseHandler
5+
provider AuthorizationProvider
6+
}
7+
8+
func (a *AuthorizationHandler) Handle(request RequestInformation) error {
9+
err := a.provider.AuthorizeRequest(request)
10+
if err != nil {
11+
return err
12+
}
13+
return a.handler.Handle(request)
14+
}

internal/authorization_provider.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package internal
2+
3+
const (
4+
AuthorizationHeader = "Authorization"
5+
)
6+
7+
type Credential interface {
8+
GetAuthentication() (string, error)
9+
}
10+
11+
type AuthorizationProvider interface {
12+
AuthorizeRequest(request RequestInformation) error
13+
}
14+
15+
type BasicAuthorizationProvider struct {
16+
credential Credential
17+
}
18+
19+
func (b *BasicAuthorizationProvider) AuthorizeRequest(request RequestInformation) error {
20+
header, err := b.credential.GetAuthentication()
21+
if err != nil {
22+
return err
23+
}
24+
}

internal/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ var (
2121
ErrWrongResponseType = errors.New("incorrect Response Type")
2222
ErrParsing = errors.New("parsing nextLink url failed")
2323
ErrNilCallback = errors.New("callback can't be nil")
24+
25+
//Authorization Provider
26+
ErrNilRequest = errors.New("request can't be nil")
2427
)

internal/helper.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"io"
66
"net/http"
7+
"reflect"
78
)
89

910
// FromJSON[T] Unmarshalls response body into v
@@ -40,3 +41,9 @@ func ParseResponse[T Response](response *http.Response, value T) error {
4041

4142
return nil
4243
}
44+
45+
// IsNil checks if a value is nil or a nil interface
46+
func IsNil(a interface{}) bool {
47+
defer func() { _ = recover() }()
48+
return a == nil || reflect.ValueOf(a).IsNil()
49+
}

internal/request_handlers.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package internal
2+
3+
type RequestHandler interface {
4+
Handle(RequestInformation) error
5+
SetNext(RequestHandler)
6+
Next() RequestHandler
7+
}
8+
9+
type BaseHandler struct {
10+
next RequestHandler
11+
}
12+
13+
// SetNext method for BaseHandler
14+
func (b *BaseHandler) SetNext(handler RequestHandler) {
15+
b.next = handler
16+
}
17+
18+
func (b *BaseHandler) Next() RequestHandler {
19+
return b.next
20+
}
21+
22+
func (b *BaseHandler) Handle(request RequestInformation) error {
23+
if !IsNil(b.Next()) {
24+
return b.Next().Handle(request)
25+
}
26+
return nil
27+
}

internal/request_information.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package internal
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"net/http"
7+
"net/url"
8+
)
9+
10+
type RequestInformation interface {
11+
SetStreamContent(content []byte)
12+
AddQueryParameters(source interface{}) error
13+
getContentReader() *bytes.Reader
14+
SetUri(url *url.URL) //nolint:stylecheck
15+
Url() (string, error) //nolint:stylecheck
16+
ToRequest() (*http.Request, error)
17+
ToRequestWithContext(ctx context.Context) (*http.Request, error)
18+
AddHeaders(rawHeaders interface{}) error
19+
}

0 commit comments

Comments
 (0)