Skip to content

Commit ccd9d0d

Browse files
Improperly embedded core.PageIterator2[T] in tableapi.TablePageIterator
1 parent 370b149 commit ccd9d0d

7 files changed

+145
-25
lines changed

changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1919

2020
### Fixed
2121

22+
- Improperly embedded `core.PageIterator2[T]` in `tableapi.TablePageIterator` ([Issue #105](https://github.com/michaeldcanady/servicenow-sdk-go/issues/105))
23+
2224
## [1.5.0] - (02/03/2024)
2325

2426
### Added

internal/authorization_provider.go

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
package internal
22

33
const (
4-
AuthorizationHeader = "Authorization"
4+
authorizationHeader = "Authorization"
55
)
66

7-
type Credential interface {
8-
GetAuthentication() (string, error)
9-
}
10-
117
type AuthorizationProvider interface {
128
AuthorizeRequest(request RequestInformation) error
139
}
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-
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package internal
2+
3+
import "net/http"
4+
5+
type BaseAuthorizationProvider struct {
6+
credential Credential
7+
}
8+
9+
func NewBaseAuthorizationProvider(credential Credential) *BaseAuthorizationProvider {
10+
return &BaseAuthorizationProvider{
11+
credential: credential,
12+
}
13+
}
14+
15+
func (b *BaseAuthorizationProvider) AuthorizeRequest(request RequestInformation) error {
16+
headerString, err := b.credential.GetAuthentication()
17+
if err != nil {
18+
return err
19+
}
20+
21+
newHeaders := http.Header{}
22+
newHeaders.Set(authorizationHeader, headerString)
23+
24+
err = request.AddHeaders(newHeaders)
25+
if err != nil {
26+
return err
27+
}
28+
29+
return nil
30+
}

internal/credential.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package internal
2+
3+
type Credential interface {
4+
GetAuthentication() (string, error)
5+
}

internal/request_information.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package internal
22

33
import (
4-
"bytes"
54
"context"
65
"net/http"
76
"net/url"
@@ -10,7 +9,6 @@ import (
109
type RequestInformation interface {
1110
SetStreamContent(content []byte)
1211
AddQueryParameters(source interface{}) error
13-
getContentReader() *bytes.Reader
1412
SetUri(url *url.URL) //nolint:stylecheck
1513
Url() (string, error) //nolint:stylecheck
1614
ToRequest() (*http.Request, error)

servicenow_client_test.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import (
66
"io"
77
"net/http"
88
"net/url"
9+
"reflect"
910
"strings"
1011
"testing"
1112

1213
"github.com/michaeldcanady/servicenow-sdk-go/core"
1314
"github.com/michaeldcanady/servicenow-sdk-go/credentials"
15+
"github.com/mozillazg/go-httpheader"
1416
"github.com/stretchr/testify/assert"
1517
)
1618

1719
type MockRequestInformation struct {
20+
Headers http.Header
1821
}
1922

2023
func (rI *MockRequestInformation) AddRequestOptions(options []core.RequestOption) {
@@ -44,10 +47,43 @@ func (rI *MockRequestInformation) ToRequest() (*http.Request, error) {
4447
}
4548

4649
func (rI *MockRequestInformation) ToRequestWithContext(ctx context.Context) (*http.Request, error) {
47-
return http.NewRequestWithContext(ctx, "GET", "https://www.example.com", nil)
50+
51+
request, err := http.NewRequestWithContext(ctx, "GET", "https://www.example.com", nil)
52+
if err != nil {
53+
return nil, err
54+
}
55+
request.Header = rI.Headers
56+
57+
return request, nil
4858
}
4959

5060
func (rI *MockRequestInformation) AddHeaders(rawHeaders interface{}) error {
61+
var headers http.Header
62+
var err error
63+
64+
val := reflect.ValueOf(rawHeaders)
65+
66+
if val.Kind() == reflect.Struct {
67+
// use the httpheader.Encode function from the httpheader package
68+
// to encode the pointer value into an http.Header map
69+
headers, err = httpheader.Encode(rawHeaders)
70+
if err != nil {
71+
return err
72+
}
73+
} else if val.Type() == reflect.TypeOf(http.Header{}) {
74+
// if the value is already an http.Header map, just assign it to headers
75+
headers = rawHeaders.(http.Header)
76+
} else {
77+
// otherwise, return an error
78+
return core.ErrInvalidHeaderType
79+
}
80+
81+
// iterate over the headers map and add each key-value pair to rI.Headers
82+
for key, values := range headers {
83+
for _, value := range values {
84+
rI.Headers.Add(key, value)
85+
}
86+
}
5187
return nil
5288
}
5389

@@ -141,7 +177,9 @@ func TestClientUnmarshallError(t *testing.T) {
141177
}
142178

143179
func TestClientToRequestWithContext(t *testing.T) {
144-
requestInfo := &MockRequestInformation{}
180+
requestInfo := &MockRequestInformation{
181+
Headers: http.Header{},
182+
}
145183

146184
cred := credentials.NewUsernamePasswordCredential("username", "password")
147185

table-api/table_page_iterator.go

+67-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,48 @@ import (
77
"github.com/michaeldcanady/servicenow-sdk-go/internal"
88
)
99

10-
// TablePageIterato2[T] is an iterator over pages of table entries.
10+
// TablePageIterator is a generic struct in Golang which is used to iterate over pages of entries.
11+
// It embeds the core.PageIterator2 struct and can be used with any type that satisfies the Entry interface.
12+
//
13+
// Fields:
14+
// - core.PageIterator2[T]: This is an embedded field of type PageIterator2 from the core package.
15+
// The type T represents the type of Entry that the iterator will return.
16+
//
17+
// Usage:
18+
// You can use this struct to iterate over pages of entries in a table.
19+
// The specific type of Entry depends on what you specify for T when you create an instance of TablePageIterator.
1120
type TablePageIterator[T Entry] struct {
12-
// pageIterator is the core page iterator that this table page iterator wraps.
13-
pageIterator core.PageIterator2[T]
21+
*core.PageIterator2[T]
1422
}
1523

24+
// constructTableCollection is a generic function in Golang used to construct a collection of table entries.
25+
// It takes an http.Response pointer as input and returns a CollectionResponse of the specified Entry type and an error.
26+
//
27+
// Parameters:
28+
// * response: This is a pointer to an http.Response that contains the server's response to an HTTP request.
29+
//
30+
// Returns:
31+
// * core.CollectionResponse[T]: This is a CollectionResponse of the specified Entry type. It represents the collection of table entries.
32+
// * error: This is an error that will be returned if there is any error while parsing the response.
33+
//
34+
// Usage:
35+
// You can use this function to construct a collection of table entries from an *http.Response.
36+
//
37+
// func ExampleConstructTableCollectionF() {
38+
// response, err := http.Get("http://example.com")
39+
//
40+
// if err != nil {
41+
// log.Fatal(err)
42+
// }
43+
//
44+
// collection, err := constructTableCollection[MyEntryType](response)
45+
//
46+
// if err != nil {
47+
// log.Fatal(err)
48+
// }
49+
//
50+
// // Process collection
51+
// }
1652
func constructTableCollection[T Entry](response *http.Response) (core.CollectionResponse[T], error) {
1753
resp := &TableCollectionResponse2[T]{}
1854

@@ -23,14 +59,40 @@ func constructTableCollection[T Entry](response *http.Response) (core.Collection
2359
return resp, nil
2460
}
2561

26-
// NewTablePageIterator[T] creates a new TablePageIterator2 instance.
62+
// NewTablePageIterator is a function that creates a new instance of TablePageIterator.
63+
// It takes a TableCollectionResponse and a Client as input and returns a pointer to a TablePageIterator and an error.
64+
//
65+
// Parameters:
66+
//
67+
// - collection: This is a pointer to a TableCollectionResponse that contains the collection of table entries.
68+
// - client: This is a Client that will be used to make requests to the server.
69+
//
70+
// Returns:
71+
//
72+
// - *TablePageIterator[T]: This is a pointer to a TablePageIterator that can be used to iterate over the pages of entries in the collection.
73+
// - error: This is an error that will be returned if there is any error while creating the PageIterator.
74+
//
75+
// Usage:
76+
// You can use this function to create a new instance of TablePageIterator.
77+
//
78+
// func ExampleNewTablePageIteratorF() {
79+
// // use an existing collection
80+
// var collection TableCollectionResponse2[T]
81+
// // use a new or existing client
82+
// var client core.Client
83+
// iter, err := NewTablePageIterator[T](collection, client)
84+
// if err != nil {
85+
// log.Fatal(err)
86+
// }
87+
// // Use iter to iterate over the pages of entries
88+
// }
2789
func NewTablePageIterator[T Entry](collection *TableCollectionResponse2[T], client core.Client) (*TablePageIterator[T], error) {
2890
pageIterator, err := core.NewPageIterator2[T](collection, client, constructTableCollection[T])
2991
if err != nil {
3092
return nil, err
3193
}
3294

3395
return &TablePageIterator[T]{
34-
pageIterator: (*pageIterator),
96+
pageIterator,
3597
}, nil
3698
}

0 commit comments

Comments
 (0)