add other key operations and make search actually return something
This commit is contained in:
parent
bbee431a31
commit
348d886cd4
3 changed files with 56 additions and 32 deletions
|
|
@ -12,3 +12,6 @@ srcDir = "src"
|
|||
requires "nim >= 1.6.10"
|
||||
requires "puppy"
|
||||
requires "jsony"
|
||||
|
||||
task testMs, "starts meilisearch in docker and runs tests":
|
||||
discard
|
||||
|
|
@ -17,25 +17,22 @@ type
|
|||
primaryKey*:string
|
||||
kind*:T
|
||||
|
||||
MeiliBadResponseCode = object of IOError
|
||||
|
||||
|
||||
SearchResult*[T] = object
|
||||
hits*:seq[T]
|
||||
offset*, limit*, processingTimeMs*:int
|
||||
estimatedTotalHits, totalHits*:Option[int]
|
||||
query*:string
|
||||
|
||||
MeiliKey = object
|
||||
name, description:Option[string]
|
||||
key, uid:string
|
||||
actions, indexes:seq[string]
|
||||
expiresAt:MeiliTime
|
||||
createdAt, updatedAt:MeiliTime
|
||||
MeiliKey* = object
|
||||
name*, description*:Option[string]
|
||||
key*, uid*:Option[string]
|
||||
actions*, indexes*:seq[string]
|
||||
expiresAt*:Option[MeiliTime]
|
||||
createdAt*, updatedAt*:MeiliTime
|
||||
|
||||
KeysResp = object
|
||||
results:seq[MeiliKey]
|
||||
offset, limit, total:int
|
||||
KeysResp* = object
|
||||
results*:seq[MeiliKey]
|
||||
offset*, limit*, total*:int
|
||||
|
||||
|
||||
const DefaultHeaders = @[Header(key:"Content-type", value:"application/json")]
|
||||
|
|
@ -60,6 +57,19 @@ proc newMeiliReq(
|
|||
):Request=
|
||||
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=
|
||||
result = fetch(req)
|
||||
|
|
@ -82,6 +92,12 @@ proc getIndex*[T](meili:Meili, name:string):MeiliIndex[T]=
|
|||
let resp = meili.fetch([name])
|
||||
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=
|
||||
fetchc(index.client, [index.name, "document", id])
|
||||
.body
|
||||
|
|
@ -109,15 +125,19 @@ proc addDocuments*[T](index:MeiliIndex[T], v:seq[T])=
|
|||
proc addDocuments*[T](index:MeiliIndex[T], v: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]=
|
||||
let resp = fetchc(
|
||||
fetchc(
|
||||
index.client,
|
||||
["indexes", index.name, "search"],
|
||||
"post",
|
||||
$queryParams
|
||||
)
|
||||
|
||||
discard
|
||||
).fromJson(SearchResult[T])
|
||||
|
||||
proc search*[T](index:MeiliIndex[T], query:string, page=1, limit=20):SearchResult[T]=
|
||||
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=
|
||||
let body = %* {
|
||||
"offset":offset,
|
||||
"limit":limit
|
||||
}
|
||||
|
||||
var u = meili.host
|
||||
u.paths = @["/keys"]
|
||||
|
||||
let req = newMeiliReq(meili, "")
|
||||
|
||||
proc keys*(meili:Meili, offset=0, limit=20):KeysResp=
|
||||
let req = newMeiliReq(meili, @["keys"], @[("offset", $offset), ("limit", $limit)])
|
||||
let resp = fetchc(
|
||||
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")
|
||||
|
|
@ -8,5 +8,3 @@
|
|||
import unittest
|
||||
|
||||
import nimMs
|
||||
test "can add":
|
||||
check add(5, 5) == 10
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue