-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: replace strings with constant values #39
Open
seedspirit
wants to merge
8
commits into
litmuschaos:master
Choose a base branch
from
seedspirit:issue#34
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8344834
refactor: replace strings with constant values
seedspirit de19c82
merge: resolve conflict
seedspirit 655c53f
feat: replace graphql api endpoint string to constant values
seedspirit 947f0b4
refactor: change auth api as token issuer/invalidator
seedspirit 2de2a75
feat: replace invitation api endpoint to constant values
seedspirit c323560
refactor: wrapped auth token with class
seedspirit 3143a89
fix: change graphql auth string token to wrapping class
seedspirit 56d736c
merge: resolve conflict
seedspirit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import io.litmuschaos.generated.types.*; | ||
import io.litmuschaos.graphql.LitmusGraphQLClient; | ||
import io.litmuschaos.http.LitmusHttpClient; | ||
import io.litmuschaos.model.LitmusAuthToken; | ||
import io.litmuschaos.request.*; | ||
import io.litmuschaos.response.*; | ||
import okhttp3.OkHttpClient; | ||
|
@@ -18,181 +19,176 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static io.litmuschaos.constants.ApiEndpoints.*; | ||
import static io.litmuschaos.constants.RequestParams.*; | ||
|
||
public class LitmusClient implements AutoCloseable { | ||
|
||
private String token; | ||
private LitmusAuthToken token; | ||
private final LitmusHttpClient httpClient; | ||
private final LitmusGraphQLClient graphQLClient; | ||
|
||
public LitmusClient(String host, String token) { | ||
public LitmusClient(String host, LitmusAuthToken token) { | ||
String sanitizedHost = sanitizeURL(host); | ||
OkHttpClient okHttpClient = new OkHttpClient(); | ||
this.token = token; | ||
this.httpClient = new LitmusHttpClient(okHttpClient, sanitizedHost + "/auth"); | ||
this.graphQLClient = new LitmusGraphQLClient(okHttpClient, sanitizedHost + "/api/query", this.token); | ||
this.httpClient = new LitmusHttpClient(okHttpClient, sanitizedHost + AUTH); | ||
this.graphQLClient = new LitmusGraphQLClient(okHttpClient, sanitizedHost + API_QUERY, this.token); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
this.httpClient.close(); | ||
} | ||
|
||
// User | ||
// public LoginResponse authenticate(LoginRequest request) | ||
// throws IOException, LitmusApiException { | ||
// LoginResponse response = httpClient.post("/login", request, LoginResponse.class); | ||
// this.token = response.getAccessToken(); | ||
// return response; | ||
// } | ||
public LitmusAuthToken authenticate(LoginRequest request) | ||
throws IOException, LitmusApiException { | ||
LoginResponse response = httpClient.post(LOGIN, request, LoginResponse.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this code actually working? |
||
return response.getAccessToken(); | ||
} | ||
|
||
// public CommonResponse logout() throws IOException, LitmusApiException { | ||
// CommonResponse commonResponse = httpClient.post("/logout", token, CommonResponse.class); | ||
// this.token = ""; | ||
// return commonResponse; | ||
// } | ||
public CommonResponse logout(LitmusAuthToken accessToken) throws IOException, LitmusApiException { | ||
CommonResponse commonResponse = httpClient.post(LOGOUT, accessToken, CommonResponse.class); | ||
return commonResponse; | ||
} | ||
|
||
public ListTokensResponse getTokens(String userId) throws IOException, LitmusApiException { | ||
return httpClient.get("/token/" + userId, token, ListTokensResponse.class); | ||
return httpClient.get(GET_TOKENS + "/" + userId, token, ListTokensResponse.class); | ||
} | ||
|
||
public TokenCreateResponse createToken(TokenCreateRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/create_token", token, request, TokenCreateResponse.class); | ||
return httpClient.post(CREATE_TOKEN, token, request, TokenCreateResponse.class); | ||
} | ||
|
||
public CommonResponse deleteToken(TokenDeleteRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/remove_token", token, request, CommonResponse.class); | ||
return httpClient.post(REMOVE_TOKEN, token, request, CommonResponse.class); | ||
} | ||
|
||
public UserResponse getUser(String userId) throws IOException, LitmusApiException { | ||
return httpClient.get("/get_user/" + userId, token, UserResponse.class); | ||
return httpClient.get(GET_USER + "/" + userId, token, UserResponse.class); | ||
} | ||
|
||
public List<UserResponse> getUsers() throws IOException, LitmusApiException { | ||
TypeToken<List<UserResponse>> typeToken = new TypeToken<>() { | ||
}; | ||
return httpClient.get("/users", token, typeToken); | ||
TypeToken<List<UserResponse>> typeToken = new TypeToken<>() {}; | ||
return httpClient.get(GET_USERS, token, typeToken); | ||
} | ||
|
||
public PasswordUpdateResponse updatePassword(PasswordUpdateRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/update/password", token, request, PasswordUpdateResponse.class); | ||
return httpClient.post(UPDATE_PASSWORD, token, request, PasswordUpdateResponse.class); | ||
} | ||
|
||
public UserResponse createUser(UserCreateRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/create_user", token, request, UserResponse.class); | ||
return httpClient.post(CREATE_USER, token, request, UserResponse.class); | ||
} | ||
|
||
public CommonResponse resetPassword(PasswordResetRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/reset/password", token, request, CommonResponse.class); | ||
return httpClient.post(RESET_PASSWORD, token, request, CommonResponse.class); | ||
} | ||
|
||
public CommonResponse updateUserDetails(UserDetailsUpdateRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/update/details", token, request, CommonResponse.class); | ||
return httpClient.post(UPDATE_USER_DETAILS, token, request, CommonResponse.class); | ||
} | ||
|
||
public CommonResponse updateUserState(UserStateUpdateRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/update/state", token, request, CommonResponse.class); | ||
return httpClient.post(UPDATE_USER_STATE, token, request, CommonResponse.class); | ||
} | ||
|
||
// Capabilities | ||
public CapabilityResponse capabilities() throws IOException, LitmusApiException { | ||
return httpClient.get("/capabilities", CapabilityResponse.class); | ||
return httpClient.get(CAPABILITIES, CapabilityResponse.class); | ||
} | ||
|
||
// Project | ||
public ListProjectsResponse listProjects(ListProjectRequest request) | ||
throws IOException, LitmusApiException { | ||
Map<String, String> requestParam = new HashMap<>(); | ||
requestParam.put("page", String.valueOf(request.getPage())); | ||
requestParam.put("limit", String.valueOf(request.getLimit())); | ||
requestParam.put("sortField", request.getSortField()); | ||
requestParam.put("createdByMe", String.valueOf(request.getCreatedByMe())); | ||
return httpClient.get("/list_projects", token, requestParam, ListProjectsResponse.class); | ||
requestParam.put(PAGE, String.valueOf(request.getPage())); | ||
requestParam.put(LIMIT, String.valueOf(request.getLimit())); | ||
requestParam.put(SORT_FIELD, request.getSortField()); | ||
requestParam.put(CREATED_BY_ME, String.valueOf(request.getCreatedByMe())); | ||
return httpClient.get(LIST_PROJECTS, token, requestParam, ListProjectsResponse.class); | ||
} | ||
|
||
public ProjectResponse createProject(CreateProjectRequest request) | ||
throws IOException, LitmusApiException { | ||
return httpClient.post("/create_project", token, request, ProjectResponse.class); | ||
return httpClient.post(CREATE_PROJECT, token, request, ProjectResponse.class); | ||
} | ||
|
||
public CommonResponse updateProjectName(ProjectNameRequest request) | ||
throws IOException, LitmusApiException { | ||
return httpClient.post("/update_project_name", token, request, CommonResponse.class); | ||
return httpClient.post(UPDATE_PROJECT_NAME, token, request, CommonResponse.class); | ||
} | ||
|
||
public ProjectResponse getProject(String projectId) throws IOException, LitmusApiException { | ||
return httpClient.get("/get_project/" + projectId, token, ProjectResponse.class); | ||
return httpClient.get(GET_PROJECT + "/" + projectId, token, ProjectResponse.class); | ||
} | ||
|
||
public List<ProjectResponse> getOwnerProjects() throws IOException, LitmusApiException { | ||
TypeToken<List<ProjectResponse>> typeToken = new TypeToken<List<ProjectResponse>>() { | ||
}; | ||
return httpClient.get("/get_owner_projects", token, typeToken); | ||
TypeToken<List<ProjectResponse>> typeToken = new TypeToken<List<ProjectResponse>>() {}; | ||
return httpClient.get(GET_OWNER_PROJECTS, token, typeToken); | ||
} | ||
|
||
public CommonResponse leaveProject(LeaveProjectRequest request) | ||
throws IOException, LitmusApiException { | ||
return httpClient.post("/leave_project", token, request, CommonResponse.class); | ||
return httpClient.post(LEAVE_PROJECT, token, request, CommonResponse.class); | ||
} | ||
|
||
public ProjectRoleResponse getProjectRole(String projectId) | ||
throws IOException, LitmusApiException { | ||
return httpClient.get("/get_project_role/" + projectId, token, ProjectRoleResponse.class); | ||
return httpClient.get(GET_PROJECT_ROLE + "/" + projectId, token, ProjectRoleResponse.class); | ||
} | ||
|
||
public UserWithProjectResponse getUserWithProject(String username) | ||
throws IOException, LitmusApiException { | ||
return httpClient.get("/get_user_with_project/" + username, token, | ||
return httpClient.get(GET_USER_WITH_PROJECT + "/" + username, token, | ||
UserWithProjectResponse.class); | ||
} | ||
|
||
public List<ProjectsStatsResponse> getProjectsStats() throws IOException, LitmusApiException { | ||
TypeToken<List<ProjectsStatsResponse>> typeToken = new TypeToken<List<ProjectsStatsResponse>>() { | ||
}; | ||
return httpClient.get("/get_projects_stats", token, typeToken); | ||
TypeToken<List<ProjectsStatsResponse>> typeToken = new TypeToken<List<ProjectsStatsResponse>>() {}; | ||
return httpClient.get(GET_PROJECTS_STATS, token, typeToken); | ||
} | ||
|
||
public List<ProjectMemberResponse> getProjectMembers(String projectID, String status) | ||
throws IOException, LitmusApiException { | ||
TypeToken<List<ProjectMemberResponse>> typeToken = new TypeToken<List<ProjectMemberResponse>>() { | ||
}; | ||
return httpClient.get("/get_project_members/" + projectID + "/" + status, token, typeToken); | ||
TypeToken<List<ProjectMemberResponse>> typeToken = new TypeToken<List<ProjectMemberResponse>>() {}; | ||
return httpClient.get(GET_PROJECT_MEMBERS + "/" + projectID + "/" + status, token, typeToken); | ||
} | ||
|
||
public List<ProjectMemberResponse> getProjectOwners(String projectID) | ||
throws IOException, LitmusApiException { | ||
TypeToken<List<ProjectMemberResponse>> typeToken = new TypeToken<List<ProjectMemberResponse>>() { | ||
}; | ||
return httpClient.get("/get_project_owners/" + projectID, token, typeToken); | ||
TypeToken<List<ProjectMemberResponse>> typeToken = new TypeToken<List<ProjectMemberResponse>>() {}; | ||
return httpClient.get(GET_PROJECT_OWNERS + "/" + projectID, token, typeToken); | ||
} | ||
|
||
public CommonResponse deleteProject(String projectID) throws IOException, LitmusApiException { | ||
return httpClient.post("/delete_project/" + projectID, token, CommonResponse.class); | ||
return httpClient.post(DELETE_PROJECT + "/" + projectID, token, CommonResponse.class); | ||
} | ||
|
||
public SendInvitationResponse sendInvitation(SendInvitationRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/send_invitation", token, request, SendInvitationResponse.class); | ||
return httpClient.post(SEND_INVITATION, token, request, SendInvitationResponse.class); | ||
} | ||
|
||
public CommonResponse acceptInvitation(AcceptInvitationRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/accept_invitation", token, request, CommonResponse.class); | ||
return httpClient.post(ACCEPT_INVITATION, token, request, CommonResponse.class); | ||
} | ||
|
||
public CommonResponse declineInvitation(DeclineInvitationRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/decline_invitation", token, request, CommonResponse.class); | ||
return httpClient.post(DECLINE_INVITATION, token, request, CommonResponse.class); | ||
} | ||
|
||
public CommonResponse removeInvitation(RemoveInvitationRequest request) throws IOException, LitmusApiException { | ||
return httpClient.post("/remove_invitation", token, request, CommonResponse.class); | ||
return httpClient.post(REMOVE_INVITATION, token, request, CommonResponse.class); | ||
} | ||
|
||
public List<ListInvitationResponse> listInvitation(String status) | ||
throws IOException, LitmusApiException { | ||
return httpClient.get("/list_invitations_with_filters/" + status, token, List.class); | ||
return httpClient.get(LIST_INVITATIONS_WITH_FILTERS + "/" + status, token, List.class); | ||
} | ||
|
||
public List<InviteUsersResponse> inviteUsers(String projectId) | ||
throws IOException, LitmusApiException { | ||
return httpClient.get("/invite_users/" + projectId, token, List.class); | ||
return httpClient.get(INVITE_USERS + "/" + projectId, token, List.class); | ||
} | ||
|
||
// Environment | ||
|
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,51 @@ | ||
package io.litmuschaos.constants; | ||
|
||
public class ApiEndpoints { | ||
|
||
// Auth | ||
public static final String AUTH = "/auth"; | ||
public static final String LOGIN = "/login"; | ||
public static final String LOGOUT = "/logout"; | ||
|
||
// Graphql | ||
public static final String API_QUERY = "/api/query"; | ||
|
||
// Token | ||
public static final String GET_TOKENS = "/token"; | ||
public static final String CREATE_TOKEN = "/create_token"; | ||
public static final String REMOVE_TOKEN = "/remove_token"; | ||
|
||
// User | ||
public static final String GET_USER = "/get_user"; | ||
public static final String GET_USERS = "/users"; | ||
public static final String CREATE_USER = "/create_user"; | ||
public static final String UPDATE_PASSWORD = "/update/password"; | ||
public static final String RESET_PASSWORD = "/reset/password"; | ||
public static final String UPDATE_USER_DETAILS = "/update/details"; | ||
public static final String UPDATE_USER_STATE = "/update/state"; | ||
|
||
// Capabilities | ||
public static final String CAPABILITIES = "/capabilities"; | ||
|
||
// Project | ||
public static final String LIST_PROJECTS = "/list_projects"; | ||
public static final String CREATE_PROJECT = "/create_project"; | ||
public static final String UPDATE_PROJECT_NAME = "/update_project_name"; | ||
public static final String GET_PROJECT = "/get_project"; | ||
public static final String GET_OWNER_PROJECTS = "/get_owner_projects"; | ||
public static final String LEAVE_PROJECT = "/leave_project"; | ||
public static final String GET_PROJECT_ROLE = "/get_project_role"; | ||
public static final String GET_USER_WITH_PROJECT = "/get_user_with_project"; | ||
public static final String GET_PROJECTS_STATS = "/get_projects_stats"; | ||
public static final String GET_PROJECT_MEMBERS = "/get_project_members"; | ||
public static final String GET_PROJECT_OWNERS = "/get_project_owners"; | ||
public static final String DELETE_PROJECT = "/delete_project"; | ||
|
||
// Invitation | ||
public static final String SEND_INVITATION = "/send_invitation"; | ||
public static final String ACCEPT_INVITATION = "/accept_invitation"; | ||
public static final String DECLINE_INVITATION = "/decline_invitation"; | ||
public static final String REMOVE_INVITATION = "/remove_invitation"; | ||
public static final String LIST_INVITATIONS_WITH_FILTERS = "/list_invitations_with_filters"; | ||
public static final String INVITE_USERS = "/invite_users"; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/io/litmuschaos/constants/RequestHeaders.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,11 @@ | ||
package io.litmuschaos.constants; | ||
|
||
|
||
public class RequestHeaders { | ||
|
||
public final static String AUTHORIZATION = "Authorization"; | ||
public final static String BEARER = "Bearer"; | ||
public final static String APPLICATION_JSON = "application/json"; | ||
public final static String CHARSET_UTF_8 = "charset=utf-8"; | ||
|
||
} |
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,8 @@ | ||
package io.litmuschaos.constants; | ||
|
||
public class RequestParams { | ||
public static final String PAGE = "page"; | ||
public static final String LIMIT = "limit"; | ||
public static final String SORT_FIELD = "sortField"; | ||
public static final String CREATED_BY_ME = "createdByMe"; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/io/litmuschaos/constants/ResponseBodyFields.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,7 @@ | ||
package io.litmuschaos.constants; | ||
|
||
public class ResponseBodyFields { | ||
|
||
public static final String ERROR_DESCRIPTION = "errorDescription"; | ||
public static final String DATA = "data"; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you get token as string and make like below?
this.token = new ListAuthToken(token);