-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
18 changed files
with
385 additions
and
133 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
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
68 changes: 68 additions & 0 deletions
68
gg-agenda-api/src/main/java/gg/agenda/api/user/agendaprofile/service/IntraProfileUtils.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,68 @@ | ||
package gg.agenda.api.user.agendaprofile.service; | ||
|
||
import static gg.utils.exception.ErrorCode.*; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.stereotype.Component; | ||
|
||
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraAchievement; | ||
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraImage; | ||
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraProfile; | ||
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraProfileResponse; | ||
import gg.auth.FortyTwoAuthUtil; | ||
import gg.utils.exception.custom.AuthenticationException; | ||
import gg.utils.external.ApiUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class IntraProfileUtils { | ||
private static final String INTRA_PROFILE_URL = "https://api.intra.42.fr/v2/me"; | ||
|
||
private final FortyTwoAuthUtil fortyTwoAuthUtil; | ||
|
||
private final ApiUtil apiUtil; | ||
|
||
public IntraProfile getIntraProfile() { | ||
IntraProfileResponse intraProfileResponse = requestIntraProfile(); | ||
intraProfileResponseValidation(intraProfileResponse); | ||
IntraImage intraImage = intraProfileResponse.getImage(); | ||
List<IntraAchievement> intraAchievements = intraProfileResponse.getAchievements(); | ||
return new IntraProfile(intraImage.getLink(), intraAchievements); | ||
} | ||
|
||
private IntraProfileResponse requestIntraProfile() { | ||
try { | ||
String accessToken = fortyTwoAuthUtil.getAccessToken(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setBearerAuth(accessToken); | ||
return apiUtil.apiCall(INTRA_PROFILE_URL, IntraProfileResponse.class, headers, HttpMethod.GET); | ||
} catch (Exception e) { | ||
String accessToken = fortyTwoAuthUtil.refreshAccessToken(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setBearerAuth(accessToken); | ||
return apiUtil.apiCall(INTRA_PROFILE_URL, IntraProfileResponse.class, headers, HttpMethod.GET); | ||
} | ||
} | ||
|
||
private void intraProfileResponseValidation(IntraProfileResponse intraProfileResponse) { | ||
if (Objects.isNull(intraProfileResponse)) { | ||
throw new AuthenticationException(AUTH_NOT_FOUND); | ||
} | ||
if (Objects.isNull(intraProfileResponse.getImage())) { | ||
throw new AuthenticationException(AUTH_NOT_FOUND); | ||
} | ||
if (Objects.isNull(intraProfileResponse.getAchievements())) { | ||
throw new AuthenticationException(AUTH_NOT_FOUND); | ||
} | ||
if (Objects.isNull(intraProfileResponse.getImage().getLink())) { | ||
throw new AuthenticationException(AUTH_NOT_FOUND); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...src/main/java/gg/agenda/api/user/agendaprofile/service/intraprofile/IntraAchievement.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,53 @@ | ||
package gg.agenda.api.user.agendaprofile.service.intraprofile; | ||
|
||
import java.net.URL; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class IntraAchievement { | ||
|
||
private static final String IMAGE_URL = "https://cdn.intra.42.fr"; | ||
|
||
private Long id; | ||
|
||
private String name; | ||
|
||
private String description; | ||
|
||
private String tier; | ||
|
||
private String kind; | ||
|
||
private boolean visible; | ||
|
||
private String image; | ||
|
||
@JsonProperty("nbr_of_success") | ||
private String nbrOfSuccess; | ||
|
||
@JsonProperty("users_url") | ||
private URL usersUrl; | ||
|
||
@Builder | ||
public IntraAchievement(Long id, String name, String description, String tier, String kind, boolean visible, | ||
String image, String nbrOfSuccess, URL usersUrl) { | ||
this.id = id; | ||
this.name = name; | ||
this.description = description; | ||
this.tier = tier; | ||
this.kind = kind; | ||
this.visible = visible; | ||
this.image = image; | ||
this.nbrOfSuccess = nbrOfSuccess; | ||
this.usersUrl = usersUrl; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...a-api/src/main/java/gg/agenda/api/user/agendaprofile/service/intraprofile/IntraImage.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,23 @@ | ||
package gg.agenda.api.user.agendaprofile.service.intraprofile; | ||
|
||
import java.net.URL; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class IntraImage { | ||
|
||
private URL link; | ||
|
||
private IntraImageVersion versions; | ||
|
||
@Builder | ||
public IntraImage(URL link, IntraImageVersion versions) { | ||
this.link = link; | ||
this.versions = versions; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...rc/main/java/gg/agenda/api/user/agendaprofile/service/intraprofile/IntraImageVersion.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,29 @@ | ||
package gg.agenda.api.user.agendaprofile.service.intraprofile; | ||
|
||
import java.net.URL; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class IntraImageVersion { | ||
|
||
private URL large; | ||
|
||
private URL medium; | ||
|
||
private URL small; | ||
|
||
private URL micro; | ||
|
||
@Builder | ||
public IntraImageVersion(URL large, URL medium, URL small, URL micro) { | ||
this.large = large; | ||
this.medium = medium; | ||
this.small = small; | ||
this.micro = micro; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...api/src/main/java/gg/agenda/api/user/agendaprofile/service/intraprofile/IntraProfile.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,24 @@ | ||
package gg.agenda.api.user.agendaprofile.service.intraprofile; | ||
|
||
import java.net.URL; | ||
import java.util.List; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class IntraProfile { | ||
|
||
private URL imageUrl; | ||
|
||
private List<IntraAchievement> achievements; | ||
|
||
@Builder | ||
public IntraProfile(URL imageUrl, List<IntraAchievement> achievements) { | ||
this.imageUrl = imageUrl; | ||
this.achievements = achievements; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...main/java/gg/agenda/api/user/agendaprofile/service/intraprofile/IntraProfileResponse.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,23 @@ | ||
package gg.agenda.api.user.agendaprofile.service.intraprofile; | ||
|
||
import java.util.List; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class IntraProfileResponse { | ||
|
||
IntraImage image; | ||
|
||
List<IntraAchievement> achievements; | ||
|
||
@Builder | ||
public IntraProfileResponse(IntraImage image, List<IntraAchievement> achievements) { | ||
this.image = image; | ||
this.achievements = achievements; | ||
} | ||
} |
Oops, something went wrong.