Skip to content

Commit

Permalink
Merge pull request #117 from microsoft/feature/code-reduction-generators
Browse files Browse the repository at this point in the history
feature/code reduction generators
  • Loading branch information
baywet authored Nov 9, 2023
2 parents e7cbf65 + aec3264 commit 849156c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.5.0] - 2023-11-08

### Added

- Added request information methods to reduce the amount of generated code.

## [1.4.0] - 2023-11-01

### Added
Expand Down
15 changes: 15 additions & 0 deletions request_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package abstractions

// RequestConfiguration represents a set of options to be used when making HTTP requests.
type RequestConfiguration[T any] struct {
// Request headers
Headers *RequestHeaders
// Request options
Options []RequestOption
// Query parameters
QueryParameters *T
}

// DefaultQueryParameters is a placeholder for operations without any query parameter documented.
type DefaultQueryParameters struct {
}
26 changes: 24 additions & 2 deletions request_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ func NewRequestInformation() *RequestInformation {
}
}

// NewRequestInformationWithMethodAndUrlTemplateAndPathParameters creates a new RequestInformation object with the specified method and URL template and path parameters.
func NewRequestInformationWithMethodAndUrlTemplateAndPathParameters(method HttpMethod, urlTemplate string, pathParameters map[string]string) *RequestInformation {
value := NewRequestInformation()
value.Method = method
value.UrlTemplate = urlTemplate
value.PathParameters = pathParameters
return value
}
func ConfigureRequestInformation[T any](request *RequestInformation, config *RequestConfiguration[T]) {
if request == nil {
return
}
if config == nil {
return
}
if config.QueryParameters != nil {
request.AddQueryParameters(*(config.QueryParameters))
}
request.Headers.AddAll(config.Headers)
request.AddRequestOptions(config.Options)
}

// GetUri returns the URI of the request.
func (request *RequestInformation) GetUri() (*u.URL, error) {
if request.uri != nil {
Expand Down Expand Up @@ -445,7 +467,7 @@ func (request *RequestInformation) SetContentFromScalarCollection(ctx context.Co
}

// AddQueryParameters adds the query parameters to the request by reading the properties from the provided object.
func (request *RequestInformation) AddQueryParameters(source interface{}) {
func (request *RequestInformation) AddQueryParameters(source any) {
if source == nil || request == nil {
return
}
Expand Down Expand Up @@ -478,7 +500,7 @@ func (request *RequestInformation) AddQueryParameters(source interface{}) {
}
strArr, ok := value.([]string)
if ok && len(strArr) > 0 {
// populating both query parameter fields to avoid breaking compatibility with code reading this field
// populating both query parameter fields to avoid breaking compatibility with code reading this field
request.QueryParameters[fieldName] = strings.Join(strArr, ",")

tmp := make([]any, len(strArr))
Expand Down

0 comments on commit 849156c

Please sign in to comment.