Skip to content

Commit

Permalink
Publish client package (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
ostcar authored Oct 1, 2022
1 parent 79274c3 commit 8a2125f
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 33 deletions.
5 changes: 2 additions & 3 deletions backendaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (
"strconv"
"strings"

"github.com/OpenSlides/openslides-performance/internal/client"
"github.com/OpenSlides/openslides-performance/internal/config"
"github.com/OpenSlides/openslides-performance/client"
"github.com/google/uuid"
)

// Run runs the command.
func (o Options) Run(ctx context.Context, cfg config.Config) error {
func (o Options) Run(ctx context.Context, cfg client.Config) error {
c, err := client.New(cfg)
if err != nil {
return fmt.Errorf("creating client: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions browser/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"net/http/httputil"
"net/url"

"github.com/OpenSlides/openslides-performance/internal/config"
"github.com/OpenSlides/openslides-performance/client"
)

const prefix = "request:"

func (o record) Run(ctx context.Context, cfg config.Config) error {
func (o record) Run(ctx context.Context, cfg client.Config) error {
target, err := url.Parse(cfg.Addr())
if err != nil {
return fmt.Errorf("parse url: %w", err)
Expand Down
7 changes: 3 additions & 4 deletions browser/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import (
"net/http"
"strings"

"github.com/OpenSlides/openslides-performance/internal/client"
"github.com/OpenSlides/openslides-performance/internal/config"
"github.com/OpenSlides/openslides-performance/client"
tea "github.com/charmbracelet/bubbletea"
"github.com/ostcar/topic"
"golang.org/x/sync/errgroup"
)

func (o replay) Run(ctx context.Context, cfg config.Config) error {
func (o replay) Run(ctx context.Context, cfg client.Config) error {
app := tea.NewProgram(initialModel(o.Amount))

go func() {
Expand Down Expand Up @@ -51,7 +50,7 @@ type sender interface {
// to be sent.
//
// The function blocks until an error happens or the context get closed.
func multiBrowser(ctx context.Context, cfg config.Config, amount int, send sender, r io.Reader) error {
func multiBrowser(ctx context.Context, cfg client.Config, amount int, send sender, r io.Reader) error {
cli, err := client.New(cfg)
if err != nil {
return fmt.Errorf("create client: %w", err)
Expand Down
25 changes: 19 additions & 6 deletions internal/client/client.go → client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"time"

"github.com/OpenSlides/openslides-performance/internal/config"
)

// Client holds the connection to the OpenSlides server.
type Client struct {
cfg config.Config
cfg Config
httpClient *http.Client

authCookie *http.Cookie
Expand All @@ -27,7 +26,7 @@ type Client struct {
}

// New initializes a new client.
func New(cfg config.Config) (*Client, error) {
func New(cfg Config) (*Client, error) {
var dialContext func(ctx context.Context, network, addr string) (net.Conn, error)
if cfg.IPv4 {
var zeroDialer net.Dialer
Expand Down Expand Up @@ -91,9 +90,12 @@ func (c *Client) LoginWithCredentials(ctx context.Context, username, password st
var resp *http.Response
for retry := 0; retry < 100; retry++ {
resp, err = checkStatus(c.httpClient.Do(req))
if err == nil {

var errStatus HTTPStatusError
if errors.As(err, &errStatus) && errStatus.StatusCode == 403 || err == nil {
break
}

time.Sleep(time.Second)
}
if err != nil {
Expand Down Expand Up @@ -158,7 +160,18 @@ func checkStatus(resp *http.Response, err error) (*http.Response, error) {
body = []byte("[can not read body]")
}
resp.Body.Close()
return nil, fmt.Errorf("got status %s: %s", resp.Status, body)
return nil, HTTPStatusError{StatusCode: resp.StatusCode, Body: body}
}
return resp, nil
}

// HTTPStatusError is returned, when the http status of a client request is
// something else then in the 200er.
type HTTPStatusError struct {
StatusCode int
Body []byte
}

func (err HTTPStatusError) Error() string {
return fmt.Sprintf("got status %s: %s", http.StatusText(err.StatusCode), err.Body)
}
2 changes: 1 addition & 1 deletion internal/config/config.go → client/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package client

// Config for all commands.
type Config struct {
Expand Down
5 changes: 2 additions & 3 deletions connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import (
"strings"
"time"

"github.com/OpenSlides/openslides-performance/internal/client"
"github.com/OpenSlides/openslides-performance/internal/config"
"github.com/OpenSlides/openslides-performance/client"
"github.com/vbauerster/mpb/v7"
"github.com/vbauerster/mpb/v7/decor"
)

// Run runs the command.
func (o Options) Run(ctx context.Context, cfg config.Config) error {
func (o Options) Run(ctx context.Context, cfg client.Config) error {
if o.Body == "" {
o.Body = `[{"collection":"organization","ids":[1],"fields":{"committee_ids":{"type":"relation-list","collection":"committee","fields":{"name":null}}}}]`
}
Expand Down
5 changes: 2 additions & 3 deletions createusers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"strconv"
"strings"

"github.com/OpenSlides/openslides-performance/internal/client"
"github.com/OpenSlides/openslides-performance/internal/config"
"github.com/OpenSlides/openslides-performance/client"
"github.com/vbauerster/mpb/v7"
"golang.org/x/sync/errgroup"
)

// Run runs the command.
func (o Options) Run(ctx context.Context, cfg config.Config) error {
func (o Options) Run(ctx context.Context, cfg client.Config) error {
c, err := client.New(cfg)
if err != nil {
return fmt.Errorf("creating client: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

"github.com/OpenSlides/openslides-performance/backendaction"
"github.com/OpenSlides/openslides-performance/browser"
"github.com/OpenSlides/openslides-performance/client"
"github.com/OpenSlides/openslides-performance/connect"
"github.com/OpenSlides/openslides-performance/createusers"
"github.com/OpenSlides/openslides-performance/internal/config"
"github.com/OpenSlides/openslides-performance/request"
"github.com/OpenSlides/openslides-performance/vote"
"github.com/OpenSlides/openslides-performance/work"
Expand Down Expand Up @@ -52,7 +52,7 @@ func interruptContext() (context.Context, context.CancelFunc) {
}

var cli struct {
config.Config
client.Config

BackendAction backendaction.Options `cmd:"" help:"Calls a backend action multiple times."`
Browser browser.Options `cmd:"" help:"Simulates a browser."`
Expand Down
5 changes: 2 additions & 3 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import (
"os"
"strings"

"github.com/OpenSlides/openslides-performance/internal/client"
"github.com/OpenSlides/openslides-performance/internal/config"
"github.com/OpenSlides/openslides-performance/client"
)

// Run sends the request.
func (o Options) Run(ctx context.Context, cfg config.Config) error {
func (o Options) Run(ctx context.Context, cfg client.Config) error {
c, err := client.New(cfg)
if err != nil {
return fmt.Errorf("creating client: %w", err)
Expand Down
5 changes: 2 additions & 3 deletions vote/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (
"sync"
"time"

"github.com/OpenSlides/openslides-performance/internal/client"
"github.com/OpenSlides/openslides-performance/internal/config"
"github.com/OpenSlides/openslides-performance/client"
"github.com/vbauerster/mpb/v7"
)

// Run runs the command.
func (o Options) Run(ctx context.Context, cfg config.Config) error {
func (o Options) Run(ctx context.Context, cfg client.Config) error {
admin, err := client.New(cfg)
if err != nil {
return fmt.Errorf("create admin user: %w", err)
Expand Down
5 changes: 2 additions & 3 deletions work/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"net/http"
"strings"

"github.com/OpenSlides/openslides-performance/internal/client"
"github.com/OpenSlides/openslides-performance/internal/config"
"github.com/OpenSlides/openslides-performance/client"
"golang.org/x/sync/errgroup"
)

// Run runs the command.
func (o Options) Run(ctx context.Context, cfg config.Config) error {
func (o Options) Run(ctx context.Context, cfg client.Config) error {
workFunc := topicDone
switch o.Strategy {
case "topic-done":
Expand Down

0 comments on commit 8a2125f

Please sign in to comment.