Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>AsyncCourse</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
</dependencies>

</project>
122 changes: 122 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class Main {
private final static HttpClient client = HttpClient.newHttpClient();
private final static ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private final static HttpResponse.BodyHandler<String> BODY_HANDLER = HttpResponse.BodyHandlers.ofString();

public static void main(String[] args) {
long time = System.currentTimeMillis();
User user = getUser();
System.out.println(user);
System.out.println(System.currentTimeMillis() - time);
}

public record User(String name, String email, List<Repo> repos) {
public User(@JsonProperty("login") String name, String email, List<Repo> repos) {
this.name = name;
this.email = email;
this.repos = repos == null ? new ArrayList<>() : repos;
}

public void addAllRepos(List<Repo> repos) {
this.repos.addAll(repos);
}
}

public record Repo(String name, String contributeUrl, List<String> contributeNames) {
public Repo(String name, @JsonProperty("contributors_url") String contributeUrl, List<String> contributeNames) {
this.name = name;
this.contributeUrl = contributeUrl;
this.contributeNames = contributeNames == null ? new ArrayList<>() : contributeNames;
}

public void addContributeNames(List<String> ctr) {
this.contributeNames.addAll(ctr);
}
}

record Contributor(@JsonProperty("login") String name) {
}

public static User getUser() {
HttpRequest requestClient = httpRequestGET("https://api.github.com/users/pivotal");
HttpRequest requestRepo = httpRequestGET("https://api.github.com/users/pivotal/repos");

CompletableFuture<List<Repo>> reposFuture = client.sendAsync(requestRepo, BODY_HANDLER)
.thenApply(HttpResponse::body)
.thenApply(Main::parseRepos)
.thenApply(repos -> {
List<CompletableFuture<Void>> allFutures = new ArrayList<>();
List<Repo> resultRepos = repos.stream().limit(10).toList();
resultRepos.forEach(repo ->
allFutures.add(client.sendAsync(httpRequestGET(repo.contributeUrl), BODY_HANDLER)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше через collect и собрать список, а не через side effect

.thenApply(HttpResponse::body)
.thenApply(Main::parseContributors)
.thenAccept(repo::addContributeNames)
.thenAccept(nothing -> System.out.println("contribute"))
));
CompletableFuture.allOf(allFutures.toArray(new CompletableFuture[0])).join();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

попробуйте список фьюч преобразовать в фьючу со списком значений

private static <T> CompletableFuture<List<T>> getAll(List<CompletableFuture<T>> contributorFutures) {
        return CompletableFuture.allOf(contributorFutures.toArray(CompletableFuture[]::new))
            .thenApply(v -> contributorFutures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
    }

return resultRepos;
}
);

return client.sendAsync(requestClient, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(Main::parseUser)
.thenCombine(reposFuture, (userF, reposF) -> {
userF.addAllRepos(reposF);
return userF;
})
.thenApply(f -> {
System.out.println("user");
return f;
}).join();
}

private static HttpRequest httpRequestGET(String url) {
return HttpRequest.newBuilder()
.uri(URI.create(url))
.GET()
.build();
}

private static List<Repo> parseRepos(String jsonString) {
try {
return Arrays.stream(mapper.readValue(jsonString, Repo[].class)).toList();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private static List<String> parseContributors(String jsonString) {
try {
return Arrays.stream(mapper.readValue(jsonString, Contributor[].class)).map(Contributor::name).toList();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}


private static User parseUser(String jsonString) {
try {
return mapper.readValue(jsonString, User.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

}