Skip to content

Commit

Permalink
search for slug using the official api
Browse files Browse the repository at this point in the history
  • Loading branch information
froehlichA committed May 2, 2022
1 parent a7eb44b commit e50c187
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/api/cfapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import cfcore, http
const
## base url of the cfproxy endpoint
addonsBaseUrl = "https://cfproxy.bmpm.workers.dev"
## base url of the curse metadata api endpoint
## used for retrieving mods by their slug, which isn't possible with the curse api
addonsSlugBaseUrl = "https://curse.nikky.moe/graphql"

type
CfApiError* = object of HttpRequestError
Expand Down Expand Up @@ -52,15 +49,13 @@ proc fetchAddons*(projectIds: seq[int]): Future[seq[CfAddon]] {.async.} =

proc fetchAddon*(slug: string): Future[CfAddon] {.async.} =
## get the addon matching the `slug`.
let reqBody = %* {
"query": "{ addons(slug: \"" & slug & "\") { id }}"
}
let curseProxyInfo = await post(addonsSlugBaseUrl.Url, body = $reqBody)
let addons = curseProxyInfo.parseJson["data"]["addons"]
if addons.len == 0:
raise newException(CfApiError, "addon with slug '" & slug & "' not found")
let projectId = addons[0]["id"].getInt()
return await fetchAddon(projectId)
let url = addonsBaseUrl & "/v1/mods/search?gameId=432&pageSize=50&sortField=6&sortOrder=desc&slug=" & slug
try:
return get(url.Url).await.parseJson["data"][0].addonFromForgeSvc
except HttpRequestError:
raise newException(CfApiError, "addon with slug '" & $slug & "' not found.")
except IndexDefect:
raise newException(CfApiError, "addon with slug '" & $slug & "' not found. Please add the addon using the project id instead.")

proc fetchAddonFiles*(projectId: int): Future[seq[CfAddonFile]] {.async.} =
## get all addon files associated with the given `projectId`.
Expand Down

0 comments on commit e50c187

Please sign in to comment.