-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adicao de novas classes para uso na implementacao da API da Marvel
- Loading branch information
1 parent
7c38700
commit 3fad0fb
Showing
10 changed files
with
149 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/alura/model/Movie.java → ...main/java/com/alura/model/imdb/Movie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/alura/model/Poster.java → ...ain/java/com/alura/model/imdb/Poster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/alura/model/Rating.java → ...ain/java/com/alura/model/imdb/Rating.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.alura.model; | ||
package com.alura.model.imdb; | ||
|
||
import lombok.Getter; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.alura.model.marvel; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class Character { | ||
private Integer id; | ||
private String name; | ||
private String description; | ||
private String thumbnailPath; | ||
private String thumbnailExtension; | ||
private String resourceURI; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/alura/model/marvel/PaginateCharacters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.alura.model.marvel; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class PaginateCharacters { | ||
private Integer offset; | ||
private Integer limit; | ||
private Integer total; | ||
private Integer count; | ||
private List<Character> characters; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.alura.service.impl; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.math.BigInteger; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
import com.alura.model.Endpoint; | ||
import com.alura.model.marvel.Character; | ||
import com.alura.model.marvel.PaginateCharacters; | ||
import com.alura.service.StickerApi; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
@SuppressWarnings("squid:S106") | ||
public class MarvelApi implements StickerApi { | ||
|
||
private static final UUID ID_REQUEST = UUID.randomUUID(); | ||
private String publicKey; | ||
private String privateKey; | ||
private PaginateCharacters characters; | ||
|
||
public MarvelApi(Endpoint endpoint, String publicKey, String privateKey) | ||
throws IOException, InterruptedException, NoSuchAlgorithmException { | ||
this.publicKey = publicKey; | ||
this.privateKey = privateKey; | ||
this.characters = getResponseData(endpoint.getUrl()); | ||
} | ||
|
||
private PaginateCharacters getResponseData(String url) | ||
throws IOException, InterruptedException, NoSuchAlgorithmException { | ||
var json = this.jsonFromGet(String.format("%s?ts=%s&apikey=%s&hash=%s", url, ID_REQUEST, publicKey, | ||
new BigInteger(1, MessageDigest.getInstance("MD5").digest((ID_REQUEST + privateKey + publicKey).getBytes())) | ||
.toString(16))); | ||
var jsonNode = new ObjectMapper().readTree(json).get("data"); | ||
return PaginateCharacters.builder() | ||
.offset(jsonNode.get("offset").asInt()) | ||
.limit(jsonNode.get("limit").asInt()) | ||
.total(jsonNode.get("total").asInt()) | ||
.count(jsonNode.get("count").asInt()) | ||
.characters( | ||
StreamSupport.stream(jsonNode.get("results").spliterator(), false) | ||
.map(node -> Character.builder() | ||
.id(node.get("id").asInt()) | ||
.name(node.get("name").asText()) | ||
.description(node.get("description").asText()) | ||
.thumbnailPath(node.get("thumbnail").get("path").asText()) | ||
.thumbnailExtension(node.get("thumbnail").get("extension").asText()) | ||
.resourceURI(node.get("resourceURI").asText()) | ||
.build()) | ||
.collect(Collectors.toList())) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void shrinkList(int min, int max) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void print() { | ||
printField("\033[41m", "Marvel Characters"); | ||
this.characters.getCharacters().stream() | ||
.forEach(character -> { | ||
printField("\033[1;37m", "Name: " + character.getName()); | ||
printField("\033[1;37m", | ||
"Thumbnail: " + character.getThumbnailPath() + "." + character.getThumbnailExtension()); | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public void updateListWithInput(InputStream input) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void generateStickerImage() { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
} |