Skip to content

Commit

Permalink
Adding cards into a new All sets request. Made separate from the orig…
Browse files Browse the repository at this point in the history
…inal request due to a massive performance difference. A few warning fixes as well.
  • Loading branch information
thechucklingatom committed Nov 18, 2018
1 parent 22b2c31 commit ae007cf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Java SDK for using the [magicthegathering.io](http://magicthegathering.io) APIs.

Note that API use is free and does not require authentication or registration, but some rate limits apply. Read the official API website for more information.

Add the dependency to your project and you're good to go!
Add the dependency to your project and you're good to go! If you are on Android make sure you call on a seperate thread than the main.

Prerequisites
-------
Expand All @@ -25,17 +25,17 @@ Integration
<dependency>
<groupId>io.magicthegathering</groupId>
<artifactId>javasdk</artifactId>
<version>0.0.10</version>
<version>0.0.11</version>
</dependency>
```
#### Gradle
```gradle
compile 'io.magicthegathering:javasdk:0.0.10'
compile 'io.magicthegathering:javasdk:0.0.11'
```

#### Ivy
```xml
<dependency org="io.magicthegathering" name="javasdk" rev="0.0.10"/>
<dependency org="io.magicthegathering" name="javasdk" rev="0.0.11"/>
```

Usage examples
Expand All @@ -59,10 +59,17 @@ MtgSet set = SetAPI.getSet(setCode);
```

#### Get all Sets
This does **not** populate the card lists by default. This is to improve perfomance if all you need is a set list.
Filter also does not currently load set lists. Will be adding in a future release.
```java
List<MtgSet> sets = SetAPI.getAllSets();
```

#### Get all Sets with card lists loaded.
```java
List<MtgSet> sets = SetAPI.getAllSetsWithCards();
```

#### Generate a Booster
```java
String setCode = "KLD";
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.magicthegathering</groupId>
<artifactId>javasdk</artifactId>
<version>0.0.10</version>
<version>0.0.11</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/io/magicthegathering/javasdk/api/SetAPI.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.magicthegathering.javasdk.api;

import io.magicthegathering.javasdk.resource.Card;
import io.magicthegathering.javasdk.resource.MtgSet;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import io.magicthegathering.javasdk.resource.Card;
import io.magicthegathering.javasdk.resource.MtgSet;

/**
* {@link SetAPI} is used to fetch {@link MtgSet}s from magicthegathering.io
*
Expand All @@ -23,13 +23,15 @@ public static MtgSet getSet(String setCode) {
String path = String.format("%s/%s/", RESOURCE_PATH, setCode);
MtgSet returnSet = get(path, "set", MtgSet.class);
if(returnSet != null) {
returnSet.setCards(CardAPI.getAllCards(new LinkedList<>(Arrays.asList("set=" + setCode))));
returnSet.setCards(CardAPI.getAllCards(new LinkedList<>(Collections.singletonList("set=" + setCode))));
}
return returnSet;
}

/**
* The method that returns all the {@link MtgSet}.
* If you want all the card lists populated use
* {@link #getAllSetsWithCards()}.
* @return A List of all the sets.
*/
public static List<MtgSet> getAllSets() {
Expand All @@ -39,7 +41,7 @@ public static List<MtgSet> getAllSets() {
/**
* The method that will generate a booster for the selected {@link MtgSet}
* @param setCode Code of which set you want a booster for.
* @return
* @return the randomized booster for the set.
*/
public static List<Card> getBooster(String setCode) {
String path = String.format("%s/%s/%s/", RESOURCE_PATH, setCode,
Expand All @@ -56,4 +58,18 @@ public static List<Card> getBooster(String setCode) {
public static List<MtgSet> getAllSets(List<String> filters){
return getList(RESOURCE_PATH, "sets", MtgSet.class, filters);
}

/**
* Gets a list of {@link MtgSet} with all the card objects populated.
* Performance will be degraded because of all the Api calls that will
* happen.
* @return A list of all the sets with cards populated.
*/
public static List<MtgSet> getAllSetsWithCards() {
List<MtgSet> returnList = getList(RESOURCE_PATH, "sets", MtgSet.class);
for(MtgSet set : returnList){
set.setCards(CardAPI.getAllCards(new LinkedList<>(Collections.singletonList("set=" + set.getCode()))));
}
return returnList;
}
}
12 changes: 10 additions & 2 deletions src/test/java/io/magicthegathering/javasdk/api/SetAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -20,7 +21,7 @@ public void testGetSet() {
MtgSet testSet = new MtgSet();
testSet.setGatherercode("1E");
assertEquals(testSet, SetAPI.getSet("LEA"));
assertFalse(testSet.equals(SetAPI.getSet("LEB")));
assertNotEquals(testSet, SetAPI.getSet("LEB"));
}

@Test
Expand Down Expand Up @@ -56,7 +57,7 @@ public void testSetFilter(){
@Test
public void testSetGetCards() {
MtgSet testSet;
testSet = SetAPI.getSet("DRK");
testSet = SetAPI.getSet("LEA");

assertNotNull(testSet.getCards());

Expand All @@ -67,4 +68,11 @@ public void testSetGetCards() {

assertTrue(testSet.getCards().contains(testCard));
}

@Test
public void testGetAllSetsWithCards() {
List<MtgSet> sets = SetAPI.getAllSetsWithCards();

assertNotNull(sets.get(0).getCards());
}
}

0 comments on commit ae007cf

Please sign in to comment.