add other key operations and make search actually return something

This commit is contained in:
mr-bonerman 2023-03-07 14:24:18 +01:00
parent bbee431a31
commit 348d886cd4
3 changed files with 56 additions and 32 deletions

View file

@ -12,3 +12,6 @@ srcDir = "src"
requires "nim >= 1.6.10" requires "nim >= 1.6.10"
requires "puppy" requires "puppy"
requires "jsony" requires "jsony"
task testMs, "starts meilisearch in docker and runs tests":
discard

View file

@ -17,25 +17,22 @@ type
primaryKey*:string primaryKey*:string
kind*:T kind*:T
MeiliBadResponseCode = object of IOError
SearchResult*[T] = object SearchResult*[T] = object
hits*:seq[T] hits*:seq[T]
offset*, limit*, processingTimeMs*:int offset*, limit*, processingTimeMs*:int
estimatedTotalHits, totalHits*:Option[int] estimatedTotalHits, totalHits*:Option[int]
query*:string query*:string
MeiliKey = object MeiliKey* = object
name, description:Option[string] name*, description*:Option[string]
key, uid:string key*, uid*:Option[string]
actions, indexes:seq[string] actions*, indexes*:seq[string]
expiresAt:MeiliTime expiresAt*:Option[MeiliTime]
createdAt, updatedAt:MeiliTime createdAt*, updatedAt*:MeiliTime
KeysResp = object KeysResp* = object
results:seq[MeiliKey] results*:seq[MeiliKey]
offset, limit, total:int offset*, limit*, total*:int
const DefaultHeaders = @[Header(key:"Content-type", value:"application/json")] const DefaultHeaders = @[Header(key:"Content-type", value:"application/json")]
@ -60,6 +57,19 @@ proc newMeiliReq(
):Request= ):Request=
newMeiliReq(meili, $url, verb, headers, timeout) newMeiliReq(meili, $url, verb, headers, timeout)
proc newMeiliReq(
meili:Meili,
subaddresses:seq[string] = @[],
queryParams:seq[(string, string)] = @[],
verb = "get",
headers = emptyHttpHeaders(),
timeout: float32 = 60
):Request=
var url = meili.host
url.paths = subaddresses
url.query = queryParams.QueryParams
newMeiliReq(meili, $url, verb, headers, timeout)
proc fetchc*(req:Request):Response= proc fetchc*(req:Request):Response=
result = fetch(req) result = fetch(req)
@ -82,6 +92,12 @@ proc getIndex*[T](meili:Meili, name:string):MeiliIndex[T]=
let resp = meili.fetch([name]) let resp = meili.fetch([name])
fromJson(resp.body, MeiliIndex[T]) fromJson(resp.body, MeiliIndex[T])
proc deleteIndex*(meili:Meili, name:string)=
discard fetchc(meili, @["/indexes", name], "delete")
proc delete*(index:MeiliIndex)=
deleteIndex(index.client, index.name)
proc `[]`*[T](index:MeiliIndex[T], id:string):T= proc `[]`*[T](index:MeiliIndex[T], id:string):T=
fetchc(index.client, [index.name, "document", id]) fetchc(index.client, [index.name, "document", id])
.body .body
@ -109,15 +125,19 @@ proc addDocuments*[T](index:MeiliIndex[T], v:seq[T])=
proc addDocuments*[T](index:MeiliIndex[T], v:T)= proc addDocuments*[T](index:MeiliIndex[T], v:T)=
addDocuments(index, @[T]) addDocuments(index, @[T])
proc deleteDocuments*(index:MeiliIndex, ids:seq[int])=
discard fetchc(index.client, ["indexes", index.name, "documents", "delete-batch"], "post", ids.toJson())
proc deleteDocument*(index:MeiliIndex, id:int)=
discard deleteDocuments(index, @[int])
proc search*[T](index:MeiliIndex[T], queryParams:JsonNode):SearchResult[T]= proc search*[T](index:MeiliIndex[T], queryParams:JsonNode):SearchResult[T]=
let resp = fetchc( fetchc(
index.client, index.client,
["indexes", index.name, "search"], ["indexes", index.name, "search"],
"post", "post",
$queryParams $queryParams
) ).fromJson(SearchResult[T])
discard
proc search*[T](index:MeiliIndex[T], query:string, page=1, limit=20):SearchResult[T]= proc search*[T](index:MeiliIndex[T], query:string, page=1, limit=20):SearchResult[T]=
search(index, search(index,
@ -128,17 +148,20 @@ proc search*[T](index:MeiliIndex[T], query:string, page=1, limit=20):SearchResul
} }
) )
proc keys(meili:Meili, offset=0, limit=20):KeysResp= proc keys*(meili:Meili, offset=0, limit=20):KeysResp=
let body = %* { let req = newMeiliReq(meili, @["keys"], @[("offset", $offset), ("limit", $limit)])
"offset":offset,
"limit":limit
}
var u = meili.host
u.paths = @["/keys"]
let req = newMeiliReq(meili, "")
let resp = fetchc( let resp = fetchc(
req req
) )
resp.body.fromJson(KeysResp)
proc getKey*(meili:Meili, identifier:string):MeiliKey=
## A valid API key or uid is required.
fetchc(meili, @["keys", identifier]).body.fromJson(MeiliKey)
proc createKey*(meili:Meili, key:MeiliKey)=
discard fetchc(meili, @["keys"], "post", key.toJson())
proc deleteKey*(meili:Meili, identifier:string)=
## A valid API key or uid is required.
discard fetchc(meili, @["keys", identifier], "delete")

View file

@ -8,5 +8,3 @@
import unittest import unittest
import nimMs import nimMs
test "can add":
check add(5, 5) == 10