httpclient

package
v0.0.0-...-c858694 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package httpclient contains issues.Service implementation over HTTP.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewIssues

func NewIssues(httpClient *http.Client, scheme, host, path string) issues.Service

NewIssues creates a client that implements issues.Service remotely over HTTP. If a nil httpClient is provided, http.DefaultClient will be used. scheme and host can be empty strings to target local service. A trailing "/" is added to path if there isn't one.

Example
package main

import (
	"net/http"
	"net/http/httptest"

	"github.com/shurcooL/home/internal/exp/service/issue/httpclient"
)

func main() {
	issuesClient := httpclient.NewIssues(nil, "http", "localhost:8080", "/api/issue")

	// Now you can use any of issuesClient methods.

	_ = issuesClient
}

func init() {

	http.DefaultTransport.(*http.Transport).RegisterProtocol("", localRoundTripper{})
}

// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using http.DefaultServeMux directly, instead of going over an HTTP connection.
type localRoundTripper struct{}

func (localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	w := httptest.NewRecorder()
	http.DefaultServeMux.ServeHTTP(w, req)
	return w.Result(), nil
}
Output:

Example (Authenticated)
package main

import (
	"context"
	"net/http"
	"net/http/httptest"

	"github.com/shurcooL/home/internal/exp/service/issue/httpclient"
	"golang.org/x/oauth2"
)

func main() {
	// HTTP client with authentication.
	src := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: "... your access token ..."},
	)
	httpClient := oauth2.NewClient(context.Background(), src)

	issuesClient := httpclient.NewIssues(httpClient, "http", "localhost:8080", "/api/issue")

	// Now you can use any of issuesClient methods.

	_ = issuesClient
}

func init() {

	http.DefaultTransport.(*http.Transport).RegisterProtocol("", localRoundTripper{})
}

// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using http.DefaultServeMux directly, instead of going over an HTTP connection.
type localRoundTripper struct{}

func (localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	w := httptest.NewRecorder()
	http.DefaultServeMux.ServeHTTP(w, req)
	return w.Result(), nil
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL