feat(ooapi): add toplevel client and simplify API (#248)
* feat(ooapi): add toplevel client and simplify API This diff should simplify using ooapi from other packages by adding more abstraction that wraps the existing code. Part of https://github.com/ooni/probe/issues/1355. * fix(ooapi): use correct comment for cloners See https://github.com/ooni/probe-cli/pull/248#discussion_r590663843 * fix(ooapi): make sure the documentation is current See https://github.com/ooni/probe-cli/pull/248#discussion_r590665773 * fix(ooapi): automate copying APIs See https://github.com/ooni/probe-cli/pull/248#discussion_r590665837 * feat(ooapi): add unit tests for clientcall.go See https://github.com/ooni/probe-cli/pull/248#discussion_r590666297 * fix(ooapi): rewrite integration tests to use toplevel API See https://github.com/ooni/probe-cli/pull/248#discussion_r590665084
This commit is contained in:
parent
c22828d369
commit
28ce79eff1
33 changed files with 2090 additions and 795 deletions
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-02-26 15:45:52.62521737 +0100 CET m=+0.000161706
|
||||
// 2021-03-10 13:17:34.342751918 +0100 CET m=+0.000196026
|
||||
|
||||
package ooapi
|
||||
|
||||
|
|
@ -12,17 +12,17 @@ import (
|
|||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
// PsiphonConfigAPIWithLogin implements login for PsiphonConfigAPI.
|
||||
type PsiphonConfigAPIWithLogin struct {
|
||||
API PsiphonConfigCloner // mandatory
|
||||
JSONCodec JSONCodec // optional
|
||||
KVStore KVStore // mandatory
|
||||
RegisterAPI RegisterCaller // mandatory
|
||||
LoginAPI LoginCaller // mandatory
|
||||
// withLoginPsiphonConfigAPI implements login for simplePsiphonConfigAPI.
|
||||
type withLoginPsiphonConfigAPI struct {
|
||||
API clonerForPsiphonConfigAPI // mandatory
|
||||
JSONCodec JSONCodec // optional
|
||||
KVStore KVStore // mandatory
|
||||
RegisterAPI callerForRegisterAPI // mandatory
|
||||
LoginAPI callerForLoginAPI // mandatory
|
||||
}
|
||||
|
||||
// Call logins, if needed, then calls the API.
|
||||
func (api *PsiphonConfigAPIWithLogin) Call(ctx context.Context, req *apimodel.PsiphonConfigRequest) (apimodel.PsiphonConfigResponse, error) {
|
||||
func (api *withLoginPsiphonConfigAPI) Call(ctx context.Context, req *apimodel.PsiphonConfigRequest) (apimodel.PsiphonConfigResponse, error) {
|
||||
token, err := api.maybeLogin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -56,14 +56,14 @@ func (api *PsiphonConfigAPIWithLogin) Call(ctx context.Context, req *apimodel.Ps
|
|||
return resp, nil
|
||||
}
|
||||
|
||||
func (api *PsiphonConfigAPIWithLogin) jsonCodec() JSONCodec {
|
||||
func (api *withLoginPsiphonConfigAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *PsiphonConfigAPIWithLogin) readstate() (*loginState, error) {
|
||||
func (api *withLoginPsiphonConfigAPI) readstate() (*loginState, error) {
|
||||
data, err := api.KVStore.Get(loginKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -75,7 +75,7 @@ func (api *PsiphonConfigAPIWithLogin) readstate() (*loginState, error) {
|
|||
return &ls, nil
|
||||
}
|
||||
|
||||
func (api *PsiphonConfigAPIWithLogin) writestate(ls *loginState) error {
|
||||
func (api *withLoginPsiphonConfigAPI) writestate(ls *loginState) error {
|
||||
data, err := api.jsonCodec().Encode(*ls)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -83,7 +83,7 @@ func (api *PsiphonConfigAPIWithLogin) writestate(ls *loginState) error {
|
|||
return api.KVStore.Set(loginKey, data)
|
||||
}
|
||||
|
||||
func (api *PsiphonConfigAPIWithLogin) doRegister(ctx context.Context, password string) (string, error) {
|
||||
func (api *withLoginPsiphonConfigAPI) doRegister(ctx context.Context, password string) (string, error) {
|
||||
req := newRegisterRequest(password)
|
||||
ls := &loginState{}
|
||||
resp, err := api.RegisterAPI.Call(ctx, req)
|
||||
|
|
@ -95,7 +95,7 @@ func (api *PsiphonConfigAPIWithLogin) doRegister(ctx context.Context, password s
|
|||
return api.doLogin(ctx, ls)
|
||||
}
|
||||
|
||||
func (api *PsiphonConfigAPIWithLogin) forceRegister(ctx context.Context) (string, error) {
|
||||
func (api *withLoginPsiphonConfigAPI) forceRegister(ctx context.Context) (string, error) {
|
||||
var password string
|
||||
// If we already have a previous password, let us keep
|
||||
// using it. This will allow a new version of the API to
|
||||
|
|
@ -115,7 +115,7 @@ func (api *PsiphonConfigAPIWithLogin) forceRegister(ctx context.Context) (string
|
|||
return api.doRegister(ctx, password)
|
||||
}
|
||||
|
||||
func (api *PsiphonConfigAPIWithLogin) forceLogin(ctx context.Context) (string, error) {
|
||||
func (api *withLoginPsiphonConfigAPI) forceLogin(ctx context.Context) (string, error) {
|
||||
ls, err := api.readstate()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -123,7 +123,7 @@ func (api *PsiphonConfigAPIWithLogin) forceLogin(ctx context.Context) (string, e
|
|||
return api.doLogin(ctx, ls)
|
||||
}
|
||||
|
||||
func (api *PsiphonConfigAPIWithLogin) maybeLogin(ctx context.Context) (string, error) {
|
||||
func (api *withLoginPsiphonConfigAPI) maybeLogin(ctx context.Context) (string, error) {
|
||||
ls, _ := api.readstate()
|
||||
if ls == nil || !ls.credentialsValid() {
|
||||
return api.forceRegister(ctx)
|
||||
|
|
@ -134,7 +134,7 @@ func (api *PsiphonConfigAPIWithLogin) maybeLogin(ctx context.Context) (string, e
|
|||
return ls.Token, nil
|
||||
}
|
||||
|
||||
func (api *PsiphonConfigAPIWithLogin) doLogin(ctx context.Context, ls *loginState) (string, error) {
|
||||
func (api *withLoginPsiphonConfigAPI) doLogin(ctx context.Context, ls *loginState) (string, error) {
|
||||
req := &apimodel.LoginRequest{
|
||||
ClientID: ls.ClientID,
|
||||
Password: ls.Password,
|
||||
|
|
@ -151,19 +151,19 @@ func (api *PsiphonConfigAPIWithLogin) doLogin(ctx context.Context, ls *loginStat
|
|||
return ls.Token, nil
|
||||
}
|
||||
|
||||
var _ PsiphonConfigCaller = &PsiphonConfigAPIWithLogin{}
|
||||
var _ callerForPsiphonConfigAPI = &withLoginPsiphonConfigAPI{}
|
||||
|
||||
// TorTargetsAPIWithLogin implements login for TorTargetsAPI.
|
||||
type TorTargetsAPIWithLogin struct {
|
||||
API TorTargetsCloner // mandatory
|
||||
JSONCodec JSONCodec // optional
|
||||
KVStore KVStore // mandatory
|
||||
RegisterAPI RegisterCaller // mandatory
|
||||
LoginAPI LoginCaller // mandatory
|
||||
// withLoginTorTargetsAPI implements login for simpleTorTargetsAPI.
|
||||
type withLoginTorTargetsAPI struct {
|
||||
API clonerForTorTargetsAPI // mandatory
|
||||
JSONCodec JSONCodec // optional
|
||||
KVStore KVStore // mandatory
|
||||
RegisterAPI callerForRegisterAPI // mandatory
|
||||
LoginAPI callerForLoginAPI // mandatory
|
||||
}
|
||||
|
||||
// Call logins, if needed, then calls the API.
|
||||
func (api *TorTargetsAPIWithLogin) Call(ctx context.Context, req *apimodel.TorTargetsRequest) (apimodel.TorTargetsResponse, error) {
|
||||
func (api *withLoginTorTargetsAPI) Call(ctx context.Context, req *apimodel.TorTargetsRequest) (apimodel.TorTargetsResponse, error) {
|
||||
token, err := api.maybeLogin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -197,14 +197,14 @@ func (api *TorTargetsAPIWithLogin) Call(ctx context.Context, req *apimodel.TorTa
|
|||
return resp, nil
|
||||
}
|
||||
|
||||
func (api *TorTargetsAPIWithLogin) jsonCodec() JSONCodec {
|
||||
func (api *withLoginTorTargetsAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *TorTargetsAPIWithLogin) readstate() (*loginState, error) {
|
||||
func (api *withLoginTorTargetsAPI) readstate() (*loginState, error) {
|
||||
data, err := api.KVStore.Get(loginKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -216,7 +216,7 @@ func (api *TorTargetsAPIWithLogin) readstate() (*loginState, error) {
|
|||
return &ls, nil
|
||||
}
|
||||
|
||||
func (api *TorTargetsAPIWithLogin) writestate(ls *loginState) error {
|
||||
func (api *withLoginTorTargetsAPI) writestate(ls *loginState) error {
|
||||
data, err := api.jsonCodec().Encode(*ls)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -224,7 +224,7 @@ func (api *TorTargetsAPIWithLogin) writestate(ls *loginState) error {
|
|||
return api.KVStore.Set(loginKey, data)
|
||||
}
|
||||
|
||||
func (api *TorTargetsAPIWithLogin) doRegister(ctx context.Context, password string) (string, error) {
|
||||
func (api *withLoginTorTargetsAPI) doRegister(ctx context.Context, password string) (string, error) {
|
||||
req := newRegisterRequest(password)
|
||||
ls := &loginState{}
|
||||
resp, err := api.RegisterAPI.Call(ctx, req)
|
||||
|
|
@ -236,7 +236,7 @@ func (api *TorTargetsAPIWithLogin) doRegister(ctx context.Context, password stri
|
|||
return api.doLogin(ctx, ls)
|
||||
}
|
||||
|
||||
func (api *TorTargetsAPIWithLogin) forceRegister(ctx context.Context) (string, error) {
|
||||
func (api *withLoginTorTargetsAPI) forceRegister(ctx context.Context) (string, error) {
|
||||
var password string
|
||||
// If we already have a previous password, let us keep
|
||||
// using it. This will allow a new version of the API to
|
||||
|
|
@ -256,7 +256,7 @@ func (api *TorTargetsAPIWithLogin) forceRegister(ctx context.Context) (string, e
|
|||
return api.doRegister(ctx, password)
|
||||
}
|
||||
|
||||
func (api *TorTargetsAPIWithLogin) forceLogin(ctx context.Context) (string, error) {
|
||||
func (api *withLoginTorTargetsAPI) forceLogin(ctx context.Context) (string, error) {
|
||||
ls, err := api.readstate()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -264,7 +264,7 @@ func (api *TorTargetsAPIWithLogin) forceLogin(ctx context.Context) (string, erro
|
|||
return api.doLogin(ctx, ls)
|
||||
}
|
||||
|
||||
func (api *TorTargetsAPIWithLogin) maybeLogin(ctx context.Context) (string, error) {
|
||||
func (api *withLoginTorTargetsAPI) maybeLogin(ctx context.Context) (string, error) {
|
||||
ls, _ := api.readstate()
|
||||
if ls == nil || !ls.credentialsValid() {
|
||||
return api.forceRegister(ctx)
|
||||
|
|
@ -275,7 +275,7 @@ func (api *TorTargetsAPIWithLogin) maybeLogin(ctx context.Context) (string, erro
|
|||
return ls.Token, nil
|
||||
}
|
||||
|
||||
func (api *TorTargetsAPIWithLogin) doLogin(ctx context.Context, ls *loginState) (string, error) {
|
||||
func (api *withLoginTorTargetsAPI) doLogin(ctx context.Context, ls *loginState) (string, error) {
|
||||
req := &apimodel.LoginRequest{
|
||||
ClientID: ls.ClientID,
|
||||
Password: ls.Password,
|
||||
|
|
@ -292,4 +292,4 @@ func (api *TorTargetsAPIWithLogin) doLogin(ctx context.Context, ls *loginState)
|
|||
return ls.Token, nil
|
||||
}
|
||||
|
||||
var _ TorTargetsCaller = &TorTargetsAPIWithLogin{}
|
||||
var _ callerForTorTargetsAPI = &withLoginTorTargetsAPI{}
|
||||
|
|
|
|||
Loading…
Reference in a new issue