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:51.194159684 +0100 CET m=+0.000175181
|
||||
// 2021-03-10 13:17:32.455000394 +0100 CET m=+0.000119352
|
||||
|
||||
package ooapi
|
||||
|
||||
|
|
@ -12,20 +12,20 @@ import (
|
|||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
// MeasurementMetaCache implements caching for MeasurementMetaAPI.
|
||||
type MeasurementMetaCache struct {
|
||||
API MeasurementMetaCaller // mandatory
|
||||
GobCodec GobCodec // optional
|
||||
KVStore KVStore // mandatory
|
||||
// withCacheMeasurementMetaAPI implements caching for simpleMeasurementMetaAPI.
|
||||
type withCacheMeasurementMetaAPI struct {
|
||||
API callerForMeasurementMetaAPI // mandatory
|
||||
GobCodec GobCodec // optional
|
||||
KVStore KVStore // mandatory
|
||||
}
|
||||
|
||||
type cacheEntryForMeasurementMeta struct {
|
||||
type cacheEntryForMeasurementMetaAPI struct {
|
||||
Req *apimodel.MeasurementMetaRequest
|
||||
Resp *apimodel.MeasurementMetaResponse
|
||||
}
|
||||
|
||||
// Call calls the API and implements caching.
|
||||
func (c *MeasurementMetaCache) Call(ctx context.Context, req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error) {
|
||||
func (c *withCacheMeasurementMetaAPI) Call(ctx context.Context, req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error) {
|
||||
if resp, _ := c.readcache(req); resp != nil {
|
||||
return resp, nil
|
||||
}
|
||||
|
|
@ -39,26 +39,26 @@ func (c *MeasurementMetaCache) Call(ctx context.Context, req *apimodel.Measureme
|
|||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *MeasurementMetaCache) gobCodec() GobCodec {
|
||||
func (c *withCacheMeasurementMetaAPI) gobCodec() GobCodec {
|
||||
if c.GobCodec != nil {
|
||||
return c.GobCodec
|
||||
}
|
||||
return &defaultGobCodec{}
|
||||
}
|
||||
|
||||
func (c *MeasurementMetaCache) getcache() ([]cacheEntryForMeasurementMeta, error) {
|
||||
func (c *withCacheMeasurementMetaAPI) getcache() ([]cacheEntryForMeasurementMetaAPI, error) {
|
||||
data, err := c.KVStore.Get("MeasurementMeta.cache")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []cacheEntryForMeasurementMeta
|
||||
var out []cacheEntryForMeasurementMetaAPI
|
||||
if err := c.gobCodec().Decode(data, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *MeasurementMetaCache) setcache(in []cacheEntryForMeasurementMeta) error {
|
||||
func (c *withCacheMeasurementMetaAPI) setcache(in []cacheEntryForMeasurementMetaAPI) error {
|
||||
data, err := c.gobCodec().Encode(in)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -66,7 +66,7 @@ func (c *MeasurementMetaCache) setcache(in []cacheEntryForMeasurementMeta) error
|
|||
return c.KVStore.Set("MeasurementMeta.cache", data)
|
||||
}
|
||||
|
||||
func (c *MeasurementMetaCache) readcache(req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error) {
|
||||
func (c *withCacheMeasurementMetaAPI) readcache(req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error) {
|
||||
cache, err := c.getcache()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -79,9 +79,9 @@ func (c *MeasurementMetaCache) readcache(req *apimodel.MeasurementMetaRequest) (
|
|||
return nil, errCacheNotFound
|
||||
}
|
||||
|
||||
func (c *MeasurementMetaCache) writecache(req *apimodel.MeasurementMetaRequest, resp *apimodel.MeasurementMetaResponse) error {
|
||||
func (c *withCacheMeasurementMetaAPI) writecache(req *apimodel.MeasurementMetaRequest, resp *apimodel.MeasurementMetaResponse) error {
|
||||
cache, _ := c.getcache()
|
||||
out := []cacheEntryForMeasurementMeta{{Req: req, Resp: resp}}
|
||||
out := []cacheEntryForMeasurementMetaAPI{{Req: req, Resp: resp}}
|
||||
const toomany = 64
|
||||
for idx, cur := range cache {
|
||||
if reflect.DeepEqual(req, cur.Req) {
|
||||
|
|
@ -95,4 +95,4 @@ func (c *MeasurementMetaCache) writecache(req *apimodel.MeasurementMetaRequest,
|
|||
return c.setcache(out)
|
||||
}
|
||||
|
||||
var _ MeasurementMetaCaller = &MeasurementMetaCache{}
|
||||
var _ callerForMeasurementMetaAPI = &withCacheMeasurementMetaAPI{}
|
||||
|
|
|
|||
Loading…
Reference in a new issue