Skip to content

Commit

Permalink
refactor to use options pattern throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
matoszz committed Sep 2, 2024
1 parent 661ceaa commit a413c67
Show file tree
Hide file tree
Showing 55 changed files with 5,752 additions and 5,409 deletions.
32 changes: 14 additions & 18 deletions .buildkite/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@ env:
IMAGE_REPO: ghcr.io/theopenlane/${APP_NAME}
SONAR_HOST: "https://sonarcloud.io"
steps:
- group: ":test_tube: tests"
- group: ":test_tube: Tests"
key: "tests"
steps:
- label: ":golangci-lint: lint :lint-roller:"
cancel_on_build_failing: true
key: "lint"
cancel_on_build_failing: true
plugins:
- docker#v5.11.0:
image: "registry.hub.docker.com/golangci/golangci-lint:v1.60.3"
command: ["golangci-lint", "run", "-v", "--timeout", "10m", "--config", ".golangci.yaml", "--concurrency", "0"]
image: "ghcr.io/theopenlane/build-image:latest"
always-pull: true
command: ["task", "go:lint"]
environment:
- "GOTOOLCHAIN=auto"
- label: ":golang: go test"
key: "go_test"
retry:
automatic:
- exit_status: "*"
limit: 2
cancel_on_build_failing: true
plugins:
- docker#v5.11.0:
image: golang:1.23.0
command: ["go", "test", "-coverprofile=coverage.out", "./..."]
artifact_paths: ["coverage.out"]
image: "ghcr.io/theopenlane/build-image:latest"
always-pull: true
command: ["task", "go:test"]
environment:
- "GOTOOLCHAIN=auto"
- group: ":closed_lock_with_key: Security Checks"
depends_on: "go_test"
key: "security"
Expand All @@ -35,8 +30,9 @@ steps:
key: "gosec"
plugins:
- docker#v5.11.0:
image: "registry.hub.docker.com/securego/gosec:2.20.0"
image: "registry.hub.docker.com/securego/gosec:latest"
command: ["-no-fail", "-exclude-generated", "-fmt sonarqube", "-out", "results.txt", "./..."]
always-pull: true
environment:
- "GOTOOLCHAIN=auto"
artifact_paths: ["results.txt"]
Expand All @@ -51,7 +47,7 @@ steps:
download: "coverage.out"
step: "go_test"
- docker#v5.11.0:
image: "sonarsource/sonar-scanner-cli:5"
image: "sonarsource/sonar-scanner-cli:11"
environment:
- "SONAR_TOKEN"
- "SONAR_HOST_URL=$SONAR_HOST"
Expand All @@ -67,7 +63,7 @@ steps:
download: coverage.out
step: "go_test"
- docker#v5.11.0:
image: "sonarsource/sonar-scanner-cli:5"
image: "sonarsource/sonar-scanner-cli:11"
environment:
- "SONAR_TOKEN"
- "SONAR_HOST_URL=$SONAR_HOST"
25 changes: 12 additions & 13 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
# Contributing

Please read the [contributing](.github/CONTRIBUTING.md) guide as well as the [Developer Certificate of Origin](https://developercertificate.org/). You will be required to sign all commits to the OpenLane project, so if you're unfamiliar with how to set that up, see [github's documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification).

Given external users will not have write to the branches in this repository, you'll need to follow the forking process to open a PR - [here](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork) is a guide from github on how to do so.

Please also read our main [contributing guide](https://github.com/theopenlane/.github/blob/main/CONTRIBUTING.md) in addition to this one; the main guide mostly says that we'd like for you to open an issue first but it's not hard-required, and that we accept all forms of proposed changes given the state of this code base (in it's infancy, still!)
## Licensing

This repository contains open source software that comprises the OpenLane stack which is open source software under [Apache 2.0](LICENSE). OpenLane's SaaS / Cloud Services are products produced from this open source software exclusively by OpenLane Technology, Inc. This product is produced under our published commercial terms (which are subject to change). Any logos or trademarks in our repositories in [theopenlane](https://github.com/theopenlane) organization are not covered under the Apache License and are trademarks of OpenLane Technology, Inc.

## Pre-requisites to a PR
Others are allowed to make their own distribution of this software or include this software in other commercial offerings, but cannot use any of the OpenLane logos, trademarks, cloud services, etc.

This repository contains a number of code generating functions / utilities which take schema modifications and scaffold out resolvers, graphql API schemas, openAPI specifications, among other things. To ensure you've generated all the necessary dependencies run `task pr`; this will run the entirety of the commands required to safely generate a PR. If for some reason one of the commands fails / encounters an error, you will need to debug the individual steps. It should be decently easy to follow the `Taskfile` in the root of this repository.
## Security

### Pre-Commit Hooks
We take the security of our software products and services seriously, including our commercial services and all of the open source code repositories managed through our Github Organizations, such as [theopenlane](https://github.com/theopenlane). If you believe you have found a security vulnerability in any of our repositories or in our SaaS offering(s), please report it to us through coordinated disclosure.

We have several `pre-commit` hooks that should be run before pushing a commit. Make sure this is installed:
**Please do NOT report security vulnerabilities through public github issues, discussions, or pull requests!**

```bash
brew install pre-commit
pre-commit install
```
Instead, please send an email to `security@theopenlane.io` with as much information as possible to best help us understand and resolve the issues. See the security policy attached to this repository for more details.

You can optionally run against all files:
## Questions?

```bash
pre-commit run --all-files
```
You can email us at `info@theopenlane.io`, open a github issue in this repository, or reach out to [matoszz](https://github.com/matoszz) directly.
127 changes: 0 additions & 127 deletions .github/workflows/releaser.yml

This file was deleted.

3 changes: 2 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
run:
timeout: 10m
allow-serial-runners: true
concurrency: 0
linters-settings:
goimports:
local-prefixes: github.com/theopenlane/httpsling
Expand Down Expand Up @@ -38,4 +39,4 @@ issues:
exclude-use-default: true
exclude-dirs:
- totp/testing/*
exclude-files:
exclude-files: []
5 changes: 1 addition & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
default_stages: [pre-commit]
fail_fast: true
default_language_version:
golang: system

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
Expand All @@ -14,6 +11,6 @@ repos:
hooks:
- id: yamlfmt
- repo: https://github.com/crate-ci/typos
rev: v1.24.1
rev: v1.22.7
hooks:
- id: typos
Empty file removed .trivyignore
Empty file.
35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
[![Build status](https://badge.buildkite.com/f74a461120ffcadbf7796d5aac8ae8c03a1cbcfda142220074.svg)](https://buildkite.com/theopenlane/httpsling)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=theopenlane_httpsling&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=theopenlane_httpsling)
[![Go Report Card](https://goreportcard.com/badge/github.com/theopenlane/httpsling)](https://goreportcard.com/report/github.com/theopenlane/httpsling)
[![Go Reference](https://pkg.go.dev/badge/github.com/theopenlane/httpsling.svg)](https://pkg.go.dev/github.com/theopenlane/httpsling)
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache2.0-brightgreen.svg)](https://opensource.org/licenses/Apache-2.0)


# Slinging HTTP

The `httpsling` library simplifies the way you make HTTP httpsling. It's intended to provide an easy-to-use interface for sending requests and handling responses, reducing the boilerplate code typically associated with the `net/http` package.
Expand Down Expand Up @@ -36,9 +43,9 @@ func main() {
}
```

## Client
## SlingClient

The `Client` struct is your gateway to making HTTP requests. You can configure it to your needs, setting default headers, cookies, timeout durations, etc.
The `SlingClient` struct is your gateway to making HTTP requests. You can configure it to your needs, setting default headers, cookies, timeout durations, etc.

```go
client := httpsling.URL("http://mattisthebest.com")
Expand All @@ -53,9 +60,9 @@ client = httpsling.Create(&httpsling.Config{
})
```

### Initializing the Client
### Initializing the SlingClient

You can start by creating a `Client` with specific configurations using the `Create` method:
You can start by creating a `SlingClient` with specific configurations using the `Create` method:

```go
client := httpsling.Create(&httpsling.Config{
Expand All @@ -77,7 +84,7 @@ client := httpsling.Create(&httpsling.Config{
})
```

This setup creates a `Client` tailored for your API communication, including base URL, request timeout, default headers, and cookies
This setup creates a `SlingClient` tailored for your API communication, including base URL, request timeout, default headers, and cookies

### Configuring with Set Methods

Expand Down Expand Up @@ -332,7 +339,7 @@ request.Headers(http.Header{HeaderContentType: []string{ContentTypeJSON}})
// Convenient methods for common headers
request.ContentType(ContentTypeJSON)
request.Accept(ContentTypeJSON)
request.UserAgent("MyCustomClient/1.0")
request.UserAgent("MyCustomSlingClient/1.0")
request.Referer("https://example.com")
```

Expand Down Expand Up @@ -412,13 +419,13 @@ if errors.Is(err, context.Canceled) {
}
```

#### HTTP Client Customization
#### HTTP SlingClient Customization

Directly customize the underlying `http.Client`:
Directly customize the underlying `http.SlingClient`:

```go
customHTTPClient := &http.Client{Timeout: 20 * time.Second}
client.SetHTTPClient(customHTTPClient)
customHTTPSlingClient := &http.SlingClient{Timeout: 20 * time.Second}
client.SetHTTPSlingClient(customHTTPSlingClient)
```

#### Path Parameters
Expand Down Expand Up @@ -494,11 +501,11 @@ request.AddMiddleware(func(next httpsling.MiddlewareHandlerFunc) httpsling.Middl

Middleware functions wrap around HTTP requests, allowing pre- and post-processing of requests and responses. They can modify requests before they are sent, examine responses, and decide whether to modify them, retry the request, or take other actions.

### Client-Level Middleware
### SlingClient-Level Middleware

Client-level middleware is applied to all requests made by a client. It's ideal for cross-cutting concerns like logging, error handling, and metrics collection.
SlingClient-level middleware is applied to all requests made by a client. It's ideal for cross-cutting concerns like logging, error handling, and metrics collection.

**Adding Middleware to a Client:**
**Adding Middleware to a SlingClient:**

```go
client := httpsling.Create(&httpsling.Config{BaseURL: "https://the.cats.meow.com"})
Expand Down Expand Up @@ -864,6 +871,6 @@ This library was inspired by and built upon the work of several other HTTP clien
- [Dghubble/sling](https://github.com/dghubble/sling)
- [Monaco-io/request](https://github.com/monaco-io/request)
- [Go-resty/resty](https://github.com/go-resty/resty)
- [Fiber Client](https://github.com/gofiber/fiber)
- [Fiber SlingClient](https://github.com/gofiber/fiber)
Props to dghubble for a great name with `sling`, which was totally ripped off to make `httpsling` <3. I chose not to use any of these directly because I wanted to have layers of control we may need within our services echosystem.
7 changes: 7 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ tasks:
cmds:
- go mod tidy

go:test:cover:
desc: runs and outputs results of created go tests with coverage
aliases: [cover]
cmds:
- go test -v ./... -coverprofile=coverage.out
- go tool cover -html=coverage.out

go:all:
aliases: [go]
desc: Runs all go test and lint related tasks
Expand Down
Loading

0 comments on commit a413c67

Please sign in to comment.