Skip to content

Commit

Permalink
Merge #137
Browse files Browse the repository at this point in the history
137: Add dumps endpoint support r=eskombro a=claudiunicolaa

Implementation for /dumps #82

- [x] Implement dump creation route
- [x] Implement get dump status route
- [x] Add tests
- [x] Add code samples

Co-authored-by: Claudiu Nicola <claudiu.nicola@complyadvantage.com>
  • Loading branch information
bors[bot] and Claudiu Nicola authored Feb 24, 2021
2 parents dff681e + dca38d2 commit 6ee52f8
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 96 deletions.
4 changes: 4 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,7 @@ faceted_search_walkthrough_facets_distribution_1: |-
"genres",
},
})
post_dump_1: |-
results, error := client.Dumps().Create()
get_dump_status_1: |-
results, error := client.Dumps().GetStatus("dump-uid")
10 changes: 10 additions & 0 deletions apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ type APIHealth interface {
Get() error
}

// APIDumps handle the creation of database dumps from MeiliSearch server.
//
// Documentation: https://docs.meilisearch.com/reference/api/dump.html
type APIDumps interface {
// Create a Dump.
Create() (*Dump, error)
// Get Dump status.
GetStatus(dumpUID string) (*Dump, error)
}

// APIVersion retrieve the version of MeiliSearch.
//
// Documentation: https://docs.meilisearch.com/reference/api/version.html
Expand Down
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ClientInterface interface {
Keys() APIKeys
Stats() APIStats
Health() APIHealth
Dumps() APIDumps
}

// Client is a structure that give you the power for interacting with an high-level api with meilisearch.
Expand All @@ -48,6 +49,7 @@ type Client struct {
apiStats APIStats
apiHealth APIHealth
apiVersion APIVersion
apiDumps APIDumps
}

// Indexes return an APIIndexes client.
Expand Down Expand Up @@ -95,6 +97,11 @@ func (c *Client) Health() APIHealth {
return c.apiHealth
}

// Dumps return an APIDumps client.
func (c *Client) Dumps() APIDumps {
return c.apiDumps
}

// NewFastHTTPCustomClient creates Meilisearch with custom fasthttp.Client
func NewFastHTTPCustomClient(config Config, client *fasthttp.Client) ClientInterface {
c := &Client{
Expand All @@ -107,6 +114,7 @@ func NewFastHTTPCustomClient(config Config, client *fasthttp.Client) ClientInter
c.apiHealth = newClientHealth(c)
c.apiStats = newClientStats(c)
c.apiVersion = newClientVersion(c)
c.apiDumps = newClientDumps(c)

return c
}
Expand All @@ -127,6 +135,7 @@ func NewClient(config Config) ClientInterface {
c.apiHealth = newClientHealth(c)
c.apiStats = newClientStats(c)
c.apiVersion = newClientVersion(c)
c.apiDumps = newClientDumps(c)

return c
}
Expand Down
47 changes: 47 additions & 0 deletions client_dumps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package meilisearch

import (
"net/http"
)

type clientDumps struct {
client *Client
}

func newClientDumps(client *Client) clientDumps {
return clientDumps{client: client}
}

func (c clientDumps) Create() (resp *Dump, err error) {
resp = &Dump{}
req := internalRequest{
endpoint: "/dumps",
method: http.MethodPost,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "Create",
apiName: "Dumps",
}
if err := c.client.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

func (c clientDumps) GetStatus(dumpUID string) (resp *Dump, err error) {
resp = &Dump{}
req := internalRequest{
endpoint: "/dumps/" + dumpUID + "/status",
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetStatus",
apiName: "Dumps",
}
if err := c.client.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
39 changes: 39 additions & 0 deletions client_dumps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package meilisearch

import (
"testing"
)

func contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}

func TestClientDumps_CreateAndGetStatus(t *testing.T) {
resp, err := client.Dumps().Create()

if err != nil {
t.Fatal(err)
}

if resp.Status != "in_progress" {
t.Fatal("response create dump does not have the 'in_progress' status")
}
var dumpUID = resp.UID
resp, err = client.Dumps().GetStatus(dumpUID)
if err != nil {
t.Fatal(err)
}
if resp.UID != dumpUID {
t.Fatal("response get dump status does not have the same UID")
}

var possibleStatuses = []string{"in_progress", "failed", "done"}
if !contains(possibleStatuses, resp.Status) {
t.Fatalf("response get dump status must be from %q", possibleStatuses)
}
}
8 changes: 8 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ type Keys struct {
Private string `json:"private,omitempty"`
}

// Dump indicate information about an dump
//
// Documentation: https://docs.meilisearch.com/reference/api/dump.html
type Dump struct {
UID string `json:"uid"`
Status string `json:"status"`
}

//
// Request/Response
//
Expand Down
Loading

0 comments on commit 6ee52f8

Please sign in to comment.