Skip to content
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
wants to merge 8 commits into
base: master
Choose a base branch
from
114 changes: 55 additions & 59 deletions src/main/java/io/litmuschaos/LitmusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public LitmusClient(String host, LitmusAuthToken token) {
public LitmusClient(String host, String token) {

Can you get token as string and make like below?

this.token = new ListAuthToken(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);
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/io/litmuschaos/constants/ApiEndpoints.java
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 src/main/java/io/litmuschaos/constants/RequestHeaders.java
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";

}
8 changes: 8 additions & 0 deletions src/main/java/io/litmuschaos/constants/RequestParams.java
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";
}
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";
}
6 changes: 4 additions & 2 deletions src/main/java/io/litmuschaos/graphql/LitmusGraphQLClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.netflix.graphql.dgs.client.HttpResponse;
import java.io.IOException;
import java.util.Map;

import io.litmuschaos.model.LitmusAuthToken;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand All @@ -15,11 +17,11 @@ public class LitmusGraphQLClient {

public final GraphQLClient client;

public LitmusGraphQLClient(OkHttpClient okHttpClient, String host, String token) {
public LitmusGraphQLClient(OkHttpClient okHttpClient, String host, LitmusAuthToken token) {
client = GraphQLClient.createCustom(host, ((url, headers, body) -> {
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", token)
.addHeader("Authorization", token.getTokenValue())
.post(RequestBody.create(body, MediaType.parse("application/json"))
).build();

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/io/litmuschaos/http/HttpResponseHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.litmuschaos.http;

import com.google.gson.*;
import io.litmuschaos.constants.ResponseBodyFields;
import io.litmuschaos.exception.LitmusApiException;
import io.litmuschaos.exception.detailed.*;
import okhttp3.Response;
Expand Down Expand Up @@ -55,8 +56,8 @@ private String parseErrorDescription(String errorBody) {

for (String jsonStr : jsonObjects) {
JsonObject jsonObject = gson.fromJson(jsonStr, JsonObject.class);
if (jsonObject.has("errorDescription")) {
return jsonObject.get("errorDescription").getAsString();
if (jsonObject.has(ResponseBodyFields.ERROR_DESCRIPTION)) {
return jsonObject.get(ResponseBodyFields.ERROR_DESCRIPTION).getAsString();
}
}
} catch (Exception e) {
Expand All @@ -70,8 +71,8 @@ private <T> T transform(String response, Type responseType) {

if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("data")) {
JsonElement dataElement = jsonObject.get("data");
if (jsonObject.has(ResponseBodyFields.DATA)) {
JsonElement dataElement = jsonObject.get(ResponseBodyFields.DATA);
return gson.fromJson(dataElement.toString(), responseType);
}
}
Expand Down
Loading