Skip to content

Commit

Permalink
allow adding only mods or resource packs - fixes #53
Browse files Browse the repository at this point in the history
  • Loading branch information
froehlichA committed Feb 28, 2022
1 parent ef5e938 commit ca940a0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/api/cfclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ const
## used for retrieving mods by their slug, which isn't possible with the curse api
addonsSlugBaseUrl = "https://curse.nikky.moe/graphql"

proc fetchAddonsByQuery*(query: string, category = CfAddonGameCategory.Mod): Future[seq[CfAddon]] {.async.} =
proc fetchAddonsByQuery*(query: string, category: Option[CfAddonGameCategory]): Future[seq[CfAddon]] {.async.} =
## retrieves all addons that match the given `query` search and `category`.
let encodedQuery = encodeUrl(query, usePlus = false)
let url = addonsBaseUrl & "/v1/mods/search?gameId=432&classId=" & $ord(category) & "&pageSize=50&sortField=6&sortOrder=desc&searchFilter=" & encodedQuery
var url = addonsBaseUrl & "/v1/mods/search?gameId=432&pageSize=50&sortField=6&sortOrder=desc&searchFilter=" & encodedQuery
if category.isSome:
url = url & "&classId=" & $ord(category.get())
try:
return get(url.Url).await.parseJson["data"].addonsFromForgeSvc
except HttpRequestError:
return @[]

proc fetchAddonsByQuery*(query: string): Future[seq[CfAddon]] {.async.} =
return await fetchAddonsByQuery(query, category = none[CfAddonGameCategory]())

proc fetchAddon*(projectId: int): Future[Option[CfAddon]] {.async.} =
## get the addon with the given `projectId`.
let url = addonsBaseUrl & "/v1/mods/" & $projectId
Expand Down
11 changes: 9 additions & 2 deletions src/cmd/add.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ proc strScan(input: string, strVal: var string, start: int): int =
inc result
strVal = input.substr(start, start+result-1)

proc paxAdd*(input: string, noDepends: bool, strategy: string): void =
proc paxAdd*(input: string, noDepends: bool, strategy: string, addonType: string): void =
## add a new mod
requirePaxProject()

Expand Down Expand Up @@ -116,7 +116,14 @@ proc paxAdd*(input: string, noDepends: bool, strategy: string): void =

else:
## Just search normally
let mcMods = waitFor(fetchAddonsByQuery(input))
let addonType: Option[CfAddonGameCategory] = case addonType:
of "mod":
some(CfAddonGameCategory.Mod)
of "resourcepack":
some(CfAddonGameCategory.Resourcepack)
else:
none[CfAddonGameCategory]()
let mcMods = waitFor(fetchAddonsByQuery(input, addonType))
let mcModOption = manifest.promptAddonChoice(mcMods, selectInstalled = false)
if mcModOption.isNone:
echoError "No mods found for your search."
Expand Down
12 changes: 10 additions & 2 deletions src/pax.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ let addCmd = (
noDepends: newCountArg(@["--no-deps"],
help = "don't install dependencies"
),
addonType: newStringArg(@["-t", "--type"],
choices = @["mod", "resourcepack"],
help = "restrict search to a certain addon type"
),
strategy: commonArgs.strategy,
yes: commonArgs.yes,
noColor: commonArgs.noColor,
Expand Down Expand Up @@ -209,8 +213,12 @@ if spec.init.seen:
elif spec.list.seen:
paxList(status = listCmd.status.seen, info = listCmd.info.seen)
elif spec.add.seen:
paxAdd(input = addCmd.input.value, noDepends = addCmd.noDepends.seen,
strategy = addCmd.strategy.value)
paxAdd(
input = addCmd.input.value,
noDepends = addCmd.noDepends.seen,
strategy = addCmd.strategy.value,
addonType = addCmd.addonType.value
)
elif spec.remove.seen:
paxRemove(name = removeCmd.name.value, strategy = removeCmd.strategy.value)
elif spec.pin.seen:
Expand Down
10 changes: 9 additions & 1 deletion tests/api/tcfclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ discard """
cmd: "nim $target --hints:on -d:testing -d:ssl --nimblePath:tests/deps $options $file"
"""

import asyncdispatch, options, sequtils, sugar
import asyncdispatch, options, sequtils, strutils, sugar
import api/cfclient, api/cfcore

block: # fetch by query
let mods = waitFor(fetchAddonsByQuery("jei"))
doAssert mods[0].projectId == 238222
doAssert mods[0].name == "Just Enough Items (JEI)"

block: # fetch mods by query
let mods = waitFor(fetchAddonsByQuery("jei", some(CfAddonGameCategory.Mod)))
doAssert mods.all(m => m.websiteUrl.contains("/mc-mods/"))

block: # fetch resource packs by query
let mods = waitFor(fetchAddonsByQuery("jei", some(CfAddonGameCategory.Resourcepack)))
doAssert mods.all(m => m.websiteUrl.contains("/texture-packs/"))

block: # fetch mod by id
let mcMod = waitFor(fetchAddon(220318)).get()
doAssert mcMod.projectId == 220318
Expand Down

0 comments on commit ca940a0

Please sign in to comment.