/
koskv
/
TeamSync-Bot
Обзор
Документация
Войти
/
koskv
/
TeamSync-Bot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/org/example/teamsyncbot/client/user/TeamSyncUserClient.java
68 строк
3 KB
Koskv
Add: Get All Users
13 дек 2025, 23:43
13 дек 2025, 23:43
6966893
Код
Авторство
О чём код?
package org.example.teamsyncbot.client.user; import lombok.AllArgsConstructor; import org.example.teamsyncbot.client.auth.AuthService; import org.example.teamsyncbot.config.BotProperties; import org.example.teamsyncbot.dto.user.UserSummaryDto; import org.example.teamsyncbot.dto.user.UserCreateRequest; import org.example.teamsyncbot.exception.TeamSyncClientException; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import java.util.List; @Component @AllArgsConstructor public class TeamSyncUserClient { private final RestTemplate restTemplate; private final BotProperties botProperties; private final AuthService authService; private String getBaseUrl() { return botProperties.getBackendUrl() + "/api/users"; } private HttpHeaders authedHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); String token = authService.getValidToken(); headers.setBearerAuth(token.substring(7)); return headers; } public List<UserSummaryDto> getAllUsers() throws TeamSyncClientException { try { HttpEntity<Void> req = new HttpEntity<>(authedHeaders()); ResponseEntity<List<UserSummaryDto>> resp = restTemplate.exchange( getBaseUrl(), HttpMethod.GET, req, new ParameterizedTypeReference<List<UserSummaryDto>>() {} ); return resp.getBody() != null ? resp.getBody() : List.of(); } catch (HttpClientErrorException.Unauthorized e) { authService.invalidateToken(); throw new TeamSyncClientException("Сессия устарела", e); } catch (Exception e) { throw new TeamSyncClientException("Ошибка при получении пользователей", e); } } public UserSummaryDto createUser(UserCreateRequest request) throws TeamSyncClientException { try { HttpEntity<UserCreateRequest> req = new HttpEntity<>(request, authedHeaders()); ResponseEntity<UserSummaryDto> resp = restTemplate.postForEntity(getBaseUrl(), req, UserSummaryDto.class); return resp.getBody(); } catch (HttpClientErrorException.Unauthorized e) { authService.invalidateToken(); throw new TeamSyncClientException("Сессия устарела", e); } catch (Exception e) { throw new TeamSyncClientException("Ошибка при создании пользователя", e); } } }