httpclient

package
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpclient demonstrates how to build a business client SDK on top of component-base's HTTP client SDK (pkg/client-sdk/http/v1). It is the client counterpart of examples/apiserver and follows the same four-layer layout as the eir apiserver client: transport -> endpoint -> service -> client factory.

Package httpclient demonstrates how to build a self-contained business client SDK on top of component-base's HTTP client SDK (pkg/client-sdk/http/v1). It is the client counterpart of examples/apiserver and mirrors the four-layer layout used by the eir apiserver client.

Four-layer architecture (each layer is one package):

Transport layer  transport/  -- builds one httpclientv1.Client per operation, owns *http.Client and base URL
Endpoint layer   endpoint/   -- adapts Client into domain-typed httpclientv1.Endpoint functions
Service layer    service/    -- business methods that assemble HTTPRequest and call endpoints
Client layer     client.go   -- NewClient wires all layers and exposes the service Factory

Assembly chain (see client.go):

NewTransport -> NewEndpoint -> NewService -> Client

The domain types (User, CreateUserRequest) are shared with the server through examples/apiserver/api/v1, so the wire contract has exactly one definition. Path parameters (":id") are declared inside the request path and filled at call time through HTTPRequest.PathVars (see transport/user.go, service/…).

Client-side middlewares are supported through the fluent builder: the create client composes a logging middleware via NewClientBuilder(...).Use(...).

Reusing this example in a business project

  1. Copy these directories (transport, endpoint, service, client.go) and rename the packages; point the apiv1 import at your own shared api package.
  2. Replace each transport operation to point at your real routes and request /response types; add or remove client-side middlewares with Use/UseMany.
  3. Keep the layer contracts and the NewTransport -> NewEndpoint -> NewService assembly chain unchanged; each layer only delegates to the one below.
  4. Naming conventions: each layer entry point is XxxFactory; a resource transport exposes one method per remote operation returning a Client.

Trying the client

Boot examples/apiserver (it listens on 127.0.0.1:8080 by default), then:

c := httpclient.NewClient(context.Background(), "http://127.0.0.1:8080", nil)
defer c.Close()

u, err := c.Users().Create(&apiv1.CreateUserRequest{Name: "alice"})
got, err := c.Users().Get(u.ID)
list, err := c.Users().List()

Testing

The accompanying test boots examples/apiserver on a free port and exercises the full Create -> Get -> List round trip through this client SDK:

go test ./examples/httpclient/...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	svcclientv1.Factory
}

Client is the example HTTP client. It embeds the service-layer Factory so callers use business methods directly (e.g. client.Users().Get("1")).

func NewClient

func NewClient(ctx context.Context, baseURL string, httpClient *http.Client) *Client

NewClient creates a Client for the given base URL. A nil httpClient falls back to http.DefaultClient; ctx seeds every request's context.

func (*Client) Close

func (c *Client) Close() error

Close releases client resources. The shared http.Client needs no explicit shutdown here, so it is a no-op kept for API symmetry.

Directories

Path Synopsis
Package endpoint is the endpoint layer of the HTTP client SDK example.
Package endpoint is the endpoint layer of the HTTP client SDK example.
Package service is the service layer of the HTTP client SDK example.
Package service is the service layer of the HTTP client SDK example.
Package transport is the transport layer of the HTTP client SDK example.
Package transport is the transport layer of the HTTP client SDK example.

Jump to

Keyboard shortcuts

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