MagicTheGathering / mtg-sdk-java

Magic: The Gathering SDK - Java
MIT License
71 stars 25 forks source link

Update MTGAPI.java #18

Closed RenanRossiDias closed 6 years ago

RenanRossiDias commented 7 years ago

Hi again!

When i first used the API, i got an error on this block when trying to perform a request without "page" parameter. My workaround was inserting the page parameter and manually iterate it to perform queryes over results.

Recently i found a closed pull request that was reporting the very same error.

Well, i built this line to perform what i think that the code wants to. Since it is trying to parse a numeric value, but the end of the String is not defined.

I took the liberty to also evaluate if the page parameter is the last of them. Case it's not, i'm searching for the next delimiter parameter, which i forgot to add on this update with other two variables.

Anticipated Sorryes about my mistake.

Here are the Three used variables:

private static String PAGE_LINK_REQUEST = "page="; private static String DELIM_LINK_RESPONSE = ">"; private static String DELIM_LINK_FILTER = "&";

thechucklingatom commented 7 years ago

Hello, thank you for your contribution. Can you give me an example use case how to reproduce this problem? Just reading this I am unable to fully understand the problem you were running into. I also don't See where these constants are being declared right now, but they could just be in another class and I am forgetting.

RenanRossiDias commented 7 years ago

These constants where to be added on MTGAPI, below : private static final String RESOURCE_PATH = "cards"; protected static String ENDPOINT = "https://api.magicthegathering.io/v1"; protected static OkHttpClient CLIENT = new OkHttpClient(); private static String DELIM_LINK = ","; private static String DELIM_LINK_PARAM = ";";

Code chunk:

            for (String[] params : paramList) {
                if (params[1].contains("last")) {
                    System.out.println("StringToBeParsed: " + params[0].split("page=")[1].replace(">", ""));
                    numberOfPages = Integer.parseInt(params[0].split("page=")[1].replace(">", ""));
                }
            }

Error log:

I/System.out: StringToBeParsed: 330&rarity=common%2Cuncommon%2Crare%2Cmythic+rare%2Cspecial%2C

W/System.err: java.lang.NumberFormatException: Invalid int: "330&rarity=common%2Cuncommon%2Crare%2Cmythic+rare%2Cspecial%2C"

I've reached this error by doing a request using "CardAPI.getAllCards(List filters)" without adding a "page=". I traced it up to MTGAPI, from where i got the code chunk above.

Just put a sout over there to check what's going on with the substring to be parsed. As shown on the Error log, above.

When i add "page=(any number)" to filters list, then the error don't happen at all.

thechucklingatom commented 7 years ago

Do you have an example of how you call the api that leads to this, and the parameters that you are passing to it? I want to have a test set up that displays the issue.

thechucklingatom commented 6 years ago

Just another request for an example call that causes this error. I am looking for how to reproduce the issue that you are fixing. Thanks.