Skip to content

Commit

Permalink
fix: add WithBearer
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Dec 19, 2023
1 parent a6f55a1 commit 3aa55c7
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 17 deletions.
38 changes: 38 additions & 0 deletions di/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2023 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package di

import (
DIE "github.com/IBM/fp-go/di/erasure"
F "github.com/IBM/fp-go/function"
IO "github.com/IBM/fp-go/io"
IOE "github.com/IBM/fp-go/ioeither"
)

var (
// InjMain is the [InjectionToken] for the main application
InjMain = MakeToken[any]("APP")

// Main is the resolver for the main application
Main = Resolve(InjMain)
)

// RunMain runs the main application from a set of [DIE.Provider]s
var RunMain = F.Flow3(
DIE.MakeInjector,
Main,
IOE.Fold(IO.Of[error], F.Constant1[any](IO.Of[error](nil))),
)
17 changes: 13 additions & 4 deletions di/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
//
// http://www.apache.org/licenses/LICENSE-2.0
//

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package implements functions and data types supporting dependency injection patterns
// Package di implements functions and data types supporting dependency injection patterns
//
// The fundamental building block is the concept of a [Dependency]. This describes the abstract concept of a function, service or value together with its type.
// Examples for dependencies can be as simple as configuration values such as the API URL for a service, the current username, a [Dependency] could be the map
Expand All @@ -23,12 +22,22 @@
//
// The implementation of a [Dependency] is called a [Provider], the dependency of an `API URL` could e.g. be realized by a provider that consults the environment to read the information
// or a config file or simply hardcode it.
// In many cases the implementation of a [DIE.Provider] depends in turn on other [Dependency]s (but never directly on other [DIE.Provider]s), a provider for an `API URL` that reads
// In many cases the implementation of a [Provider] depends in turn on other [Dependency] (but never directly on other [Provider]s), a provider for an `API URL` that reads
// the information from the environment would e.g. depend on a [Dependency] that represents this environment.
//
// It is the resposibility of the [DIE.InjectableFactory] to
// It is the resposibility of the [InjectableFactory] to create an instance of a [Dependency]. All instances are considered singletons. Create an [InjectableFactory] via the [MakeInjector] method. Use [Resolve] to create a strongly typed
// factory for a particular [InjectionToken].
//
// In most cases it is not required to use [InjectableFactory] directly, instead you would create a [Provider] via the [MakeProvider2] method (suffix indicates the number of dependencies). In this call
// you give a number of (strongly typed) [Dependency] identifiers and a (strongly typed) factory function for the implementation. The dependency injection framework makes
// sure to resolve the dependencies before calling the factory method.
//
// For convenience purposes it can be helpful to attach a default implementation of a [Dependency]. In this case use the [MakeTokenWithDefault2] method (suffix indicates the number of dependencies)
// to define the [InjectionToken].
//
// [Provider]: [github.com/IBM/fp-go/di/erasure.Provider]
// [InjectableFactory]: [github.com/IBM/fp-go/di/erasure.InjectableFactory]
// [MakeInjector]: [github.com/IBM/fp-go/di/erasure.MakeInjector]
package di

//go:generate go run .. di --count 10 --filename gen.go
1 change: 0 additions & 1 deletion di/erasure/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//
// http://www.apache.org/licenses/LICENSE-2.0
//

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
1 change: 0 additions & 1 deletion di/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//
// http://www.apache.org/licenses/LICENSE-2.0
//

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
1 change: 1 addition & 0 deletions di/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package di

import (
Expand Down
1 change: 1 addition & 0 deletions di/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package di

import (
Expand Down
1 change: 1 addition & 0 deletions di/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package di

import (
Expand Down
1 change: 1 addition & 0 deletions di/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package di

import (
Expand Down
22 changes: 22 additions & 0 deletions http/content/content.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2023 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package content

const (
TextPlain = "text/plain"
Json = "application/json"
FormEncoded = "application/x-www-form-urlencoded"
)
24 changes: 24 additions & 0 deletions http/headers/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2023 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package headers

// HTTP headers
const (
Accept = "Accept"
Authorization = "Authorization"
ContentType = "Content-Type"
ContentLength = "Content-Length"
)
3 changes: 2 additions & 1 deletion http/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function"
C "github.com/IBM/fp-go/http/content"
"github.com/stretchr/testify/assert"
)

Expand All @@ -38,7 +39,7 @@ func Error[A any](t *testing.T) func(E.Either[error, A]) bool {
func TestValidateJsonContentTypeString(t *testing.T) {

res := F.Pipe1(
validateJsonContentTypeString("application/json"),
validateJsonContentTypeString(C.Json),
NoError[ParsedMediaType](t),
)

Expand Down
20 changes: 15 additions & 5 deletions ioeither/http/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

ENDO "github.com/IBM/fp-go/endomorphism"
F "github.com/IBM/fp-go/function"
C "github.com/IBM/fp-go/http/content"
H "github.com/IBM/fp-go/http/headers"
IOE "github.com/IBM/fp-go/ioeither"
IOEH "github.com/IBM/fp-go/ioeither/http"
J "github.com/IBM/fp-go/json"
Expand Down Expand Up @@ -80,8 +82,10 @@ var (
O.Of[IOE.IOEither[error, []byte]],
Body.Set,
)
// WithContentType adds the content type header
WithContentType = WithHeader("Content-Type")
// WithContentType adds the [H.ContentType] header
WithContentType = WithHeader(H.ContentType)
// WithAuthorization adds the [H.Authorization] header
WithAuthorization = WithHeader(H.Authorization)

// WithGet adds the [http.MethodGet] method
WithGet = WithMethod(http.MethodGet)
Expand All @@ -92,6 +96,12 @@ var (
// WithDelete adds the [http.MethodDelete] method
WithDelete = WithMethod(http.MethodDelete)

// WithBearer creates a [BuilderBuilder] to add a Bearer [H.Authorization] header
WithBearer = F.Flow2(
S.Format[string]("Bearer %s"),
WithAuthorization,
)

// Requester creates a requester from a builder
Requester = (*Builder).Requester

Expand Down Expand Up @@ -187,7 +197,7 @@ func (builder *Builder) Requester() IOEH.Requester {
req.Header[name] = value
}
if rdr != nil {
req.Header.Set("Content-Length", strconv.FormatInt(rdr.Size(), 10))
req.Header.Set(H.ContentLength, strconv.FormatInt(rdr.Size(), 10))
}
}
return req, err
Expand Down Expand Up @@ -237,7 +247,7 @@ func WithFormData(value url.Values) Endomorphism {
IOE.Of[error, []byte],
WithBody,
),
WithContentType("application/x-www-form-urlencoded"),
WithContentType(C.FormEncoded),
)
}

Expand All @@ -250,6 +260,6 @@ func WithJson[T any](data T) Endomorphism {
IOE.FromEither[error, []byte],
WithBody,
),
WithContentType("application/json"),
WithContentType(C.Json),
)
}
12 changes: 7 additions & 5 deletions ioeither/http/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ import (
"testing"

F "github.com/IBM/fp-go/function"
C "github.com/IBM/fp-go/http/content"
H "github.com/IBM/fp-go/http/headers"
O "github.com/IBM/fp-go/option"
"github.com/stretchr/testify/assert"
)

func TestBuiler(t *testing.T) {

name := "Content-type"
name := H.ContentType
withContentType := WithHeader(name)
withoutContentType := WithoutHeader(name)

b1 := F.Pipe1(
Default,
withContentType("application/json"),
withContentType(C.Json),
)

b2 := F.Pipe1(
b1,
withContentType("text/plain"),
withContentType(C.TextPlain),
)

b3 := F.Pipe1(
Expand All @@ -45,7 +47,7 @@ func TestBuiler(t *testing.T) {
)

assert.Equal(t, O.None[string](), Default.GetHeader(name))
assert.Equal(t, O.Of("application/json"), b1.GetHeader(name))
assert.Equal(t, O.Of("text/plain"), b2.GetHeader(name))
assert.Equal(t, O.Of(C.Json), b1.GetHeader(name))
assert.Equal(t, O.Of(C.TextPlain), b2.GetHeader(name))
assert.Equal(t, O.None[string](), b3.GetHeader(name))
}

0 comments on commit 3aa55c7

Please sign in to comment.