Nurdaulet Agabek
Nurdaulet Agabek

Reputation: 1

Reactor WebClient: PrematureCloseException - Connection Closed Before Response

I'm using Spring WebClient to fetch data from the StackOverflow API, but sometimes I get this error: reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response

Issue: Some requests fail with connection issues or authentication errors. Occasionally, I receive 403 Forbidden or 401 Unauthorized responses. I'm not sure if my authentication header setup is correct.

import backend.academy.scrapper.ScrapperConfig;
import backend.academy.scrapper.client.GithubClientImpl;
import backend.academy.scrapper.client.LinkTrackerBotApiClientImpl;
import backend.academy.scrapper.client.StackoverflowClientImpl;
import backend.academy.scrapper.linkParser.GithubLinkParser;
import backend.academy.scrapper.linkParser.StackoverflowLinkParser;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestClient;

@Configuration
public class ClientBeans {

    @Bean
    public GithubClientImpl githubClient(ScrapperConfig scrapperConfig, GithubLinkParser githubLinkParser) {
        return new GithubClientImpl(RestClient.builder()
            .baseUrl(scrapperConfig.github().baseUrl())
            .defaultHeaders(headers -> {
                scrapperConfig.github().baseRequest().headers().forEach(headers::set);
                headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + scrapperConfig.github().githubToken());
            })
            .build(), githubLinkParser);
    }

    @Bean
    public StackoverflowClientImpl stackoverflowClient(ScrapperConfig scrapperConfig, StackoverflowLinkParser stackoverflowLinkParser) {
        return new StackoverflowClientImpl(RestClient.builder()
            .baseUrl(scrapperConfig.stackOverflow().baseUrl())
            .defaultUriVariables(Map.of(
                "key", scrapperConfig.stackOverflow().key(),
                "site", scrapperConfig.stackOverflow().site()
            ))
            .build(), stackoverflowLinkParser);
    }

    @Bean
    public LinkTrackerBotApiClientImpl linkTrackerBotApiClient(ScrapperConfig scrapperConfig) {
        return new LinkTrackerBotApiClientImpl(RestClient.builder()
            .baseUrl(scrapperConfig.linkTrackerBotApi().baseUrl())
            .build());
    }
}

import backend.academy.scrapper.client.dto.GithubRepoInfo;
import backend.academy.scrapper.linkParser.GithubLinkParser;
import backend.academy.scrapper.linkParser.parsedLink.ParsedGithubLink;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.client.RestClient;

@Log4j2
@RequiredArgsConstructor
public class GithubClientImpl implements GithubClient {
    private final RestClient restClient;
    private final GithubLinkParser githubLinkParser;

    @Override
    public GithubRepoInfo getRepoInfo(String link) {
        ParsedGithubLink parsedGithubLink = githubLinkParser.parse(link);
        return restClient.get()
            .uri("/repos/{owner}/{repo}", Map.of(
                "owner", parsedGithubLink.owner(),
                "repo", parsedGithubLink.repo())
            ).retrieve()
            .body(GithubRepoInfo.class);
    }


}

Upvotes: 0

Views: 15

Answers (0)

Related Questions