Skip to content

Commit 5d7a8c6

Browse files
committed
added medium create post rest api integration
1 parent 3fcc5a9 commit 5d7a8c6

File tree

10 files changed

+196
-257
lines changed

10 files changed

+196
-257
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.cevheri.blog.service;
2+
3+
import com.cevheri.blog.service.dto.PostDTO;
4+
5+
public interface ThirdPartyBlogService {
6+
String sendPost(PostDTO dto);
7+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.cevheri.blog.service.impl;
2+
3+
import com.cevheri.blog.domain.enumeration.ExitCodeType;
4+
import com.cevheri.blog.domain.enumeration.ThirdPartyAppName;
5+
import com.cevheri.blog.security.SecurityUtils;
6+
import com.cevheri.blog.service.IntegrationLogService;
7+
import com.cevheri.blog.service.ThirdPartyBlogService;
8+
import com.cevheri.blog.service.dto.IntegrationLogDTO;
9+
import com.cevheri.blog.service.dto.PostDTO;
10+
import com.cevheri.blog.service.mapper.PostMapper;
11+
import com.cevheri.blog.service.medium.PostApi;
12+
import com.cevheri.blog.service.medium.PostResponse;
13+
import org.apache.commons.lang3.exception.ExceptionUtils;
14+
import org.springframework.stereotype.Service;
15+
import org.springframework.transaction.annotation.Transactional;
16+
17+
import java.io.IOException;
18+
import java.time.Instant;
19+
20+
@Service
21+
@Transactional
22+
public class MediumServiceImpl
23+
implements ThirdPartyBlogService {
24+
25+
private final PostMapper postMapper;
26+
private final IntegrationLogService integrationLogService;
27+
28+
public MediumServiceImpl(PostMapper postMapper,
29+
IntegrationLogService integrationLogService) {
30+
this.postMapper = postMapper;
31+
this.integrationLogService = integrationLogService;
32+
}
33+
34+
35+
@Override
36+
public String sendPost(PostDTO postDTO) {
37+
IntegrationLogDTO integrationLogDTO = new IntegrationLogDTO();
38+
PostResponse result = new PostResponse();
39+
try {
40+
41+
if(postDTO.getIntegrationId()!=null){
42+
//TODO call medium updatePost api.
43+
}
44+
45+
result = PostApi.createPost(postMapper.toPostModel(postDTO));
46+
47+
if(!result.getErrors().isEmpty()){
48+
//TODO PostCreateError. Medium response Error. Write a log!!!!
49+
}
50+
51+
integrationLogDTO.setExitCode(ExitCodeType.SUCCESS);
52+
integrationLogDTO.setResponseData(result.toString());
53+
}catch (IOException ie){
54+
integrationLogDTO.setExitCode(ExitCodeType.ERROR);
55+
integrationLogDTO.setResponseData(null);
56+
integrationLogDTO.setErrorMessage(ExceptionUtils.getRootCauseMessage(ie));
57+
}finally {
58+
integrationLogDTO.setApiUrl(result.getApiUrl());
59+
integrationLogDTO.setCreatedBy(SecurityUtils.getCurrentUserLogin().get());
60+
integrationLogDTO.setCreatedDate(Instant.now());
61+
integrationLogDTO.setIntegrationName(ThirdPartyAppName.MEDIUM);
62+
integrationLogDTO.setRequestData(postDTO.toString());
63+
integrationLogService.save(integrationLogDTO);
64+
}
65+
String integrationId = result.getData().getId();
66+
return integrationId;
67+
}
68+
69+
}

src/main/java/com/cevheri/blog/service/mapper/PostMapper.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import com.cevheri.blog.domain.Post;
55
import com.cevheri.blog.domain.Tag;
66
import com.cevheri.blog.domain.User;
7-
import com.cevheri.blog.service.dto.BlogDTO;
8-
import com.cevheri.blog.service.dto.PostDTO;
9-
import com.cevheri.blog.service.dto.TagDTO;
10-
import com.cevheri.blog.service.dto.UserDTO;
7+
import com.cevheri.blog.service.dto.*;
8+
119
import java.util.Set;
1210
import java.util.stream.Collectors;
11+
12+
import com.cevheri.blog.service.medium.PostResponse;
1313
import org.mapstruct.*;
1414

1515
/**
@@ -25,6 +25,10 @@ public interface PostMapper extends EntityMapper<PostDTO, Post> {
2525
@Mapping(target = "removeTag", ignore = true)
2626
Post toEntity(PostDTO postDTO);
2727

28+
@Mapping(target = "removeTag", ignore = true)
29+
Post toEntity(UpdatePostDTO postDTO);
30+
31+
2832
@Named("userLogin")
2933
@BeanMapping(ignoreByDefault = true)
3034
@Mapping(target = "id", source = "id")
@@ -47,4 +51,21 @@ public interface PostMapper extends EntityMapper<PostDTO, Post> {
4751
default Set<TagDTO> toDtoTagNameSet(Set<Tag> tag) {
4852
return tag.stream().map(this::toDtoTagName).collect(Collectors.toSet());
4953
}
54+
55+
default PostResponse toPostModel(PostDTO postDTO){
56+
PostResponse result = new PostResponse();
57+
PostResponse.PostData model = result.getData();
58+
model.setId(null);
59+
model.setAuthorId(null);
60+
model.setTitle(postDTO.getTitle());
61+
model.setTags(postDTO.getTags().stream().map(TagDTO::getName).collect(Collectors.toList()));
62+
model.setCanonicalUrl("https://cevheri-blog.herokuapp.com");
63+
model.setContent(postDTO.getContent());
64+
model.setLicense(null);
65+
model.setContentFormat("html");
66+
model.setPublishStatus(null);
67+
result.setData(model);
68+
return result;
69+
}
70+
5071
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.cevheri.blog.service.medium;
2+
3+
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
6+
7+
import java.io.Serializable;
8+
9+
public abstract class AbstractModel implements Serializable {
10+
}

src/main/java/com/cevheri/blog/service/medium/ApiError.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.cevheri.blog.service.medium;
22

3-
public class ApiError {
3+
import java.io.Serializable;
4+
5+
public class ApiError implements Serializable {
46
private String code;
57
private String message;
68

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.cevheri.blog.service.medium;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
6+
public class ApiResponse<T extends AbstractModel> implements Serializable {
7+
private String apiUrl;
8+
private T data;
9+
10+
public T getData() {
11+
return data;
12+
}
13+
14+
public void setData(T data) {
15+
this.data = data;
16+
}
17+
18+
private List<ApiError> errors;
19+
public List<ApiError> getErrors() {
20+
return errors;
21+
}
22+
23+
public void setErrors(List<ApiError> errors) {
24+
this.errors = errors;
25+
}
26+
27+
public String getApiUrl() {
28+
return apiUrl;
29+
}
30+
31+
public void setApiUrl(String apiUrl) {
32+
this.apiUrl = apiUrl;
33+
}
34+
35+
@Override
36+
public boolean equals(Object o) {
37+
if (this == o) return true;
38+
if (o == null || getClass() != o.getClass()) return false;
39+
40+
ApiResponse<?> that = (ApiResponse<?>) o;
41+
42+
if (apiUrl != null ? !apiUrl.equals(that.apiUrl) : that.apiUrl != null) return false;
43+
if (data != null ? !data.equals(that.data) : that.data != null) return false;
44+
return errors != null ? errors.equals(that.errors) : that.errors == null;
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
int result = apiUrl != null ? apiUrl.hashCode() : 0;
50+
result = 31 * result + (data != null ? data.hashCode() : 0);
51+
result = 31 * result + (errors != null ? errors.hashCode() : 0);
52+
return result;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "ApiResponse{" +
58+
"apiUrl='" + apiUrl + '\'' +
59+
", data=" + data +
60+
", errors=" + errors +
61+
'}';
62+
}
63+
}

src/main/java/com/cevheri/blog/service/medium/PostApi.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,44 @@
44
import org.testcontainers.shaded.okhttp3.*;
55

66
import java.io.IOException;
7-
import java.util.Arrays;
87

98
public class PostApi {
109

11-
private final String createPostResourceUrl = "/users/{{authorId}}/posts";
12-
private final String listPublicationsResourceUrl = "/users/{{userId}}/publications";
10+
private String listPublicationsResourceUrl = "/users/{{userId}}/publications";
1311

12+
public static PostResponse createPost(PostResponse input) throws IOException {
1413

15-
public PostModel createPost(PostModel input) throws IOException {
1614

1715
ObjectMapper objectMapper = new ObjectMapper();
18-
String content = objectMapper.writeValueAsString(input);
16+
String content = objectMapper.writeValueAsString(input.getData());
1917
System.out.println("createPost content:" + content);
2018

2119
OkHttpClient client = new OkHttpClient().newBuilder()
2220
.build();
2321
MediaType mediaType = MediaType.parse("application/json");
24-
RequestBody body = RequestBody.create(mediaType,
25-
"{" + content + "}"
26-
);
27-
28-
String resourceUrl = createPostResourceUrl.replace("{{authorId}}", UserApi.AUTHOR_ID);
22+
RequestBody body = RequestBody.create(mediaType, content);
23+
PostResponse result = new PostResponse();
24+
String createPostResourceUrl = result.getApiUrl().replace("{{authorId}}", UserApi.AUTHOR_ID);
25+
//String createPostResourceUrl = result.getApiUrl().replace("{{authorId}}", "12974e025f39ae31b1e4c4a419f4f8210f1b399d20e39f24d5eeb3f1715d041e4");
2926

3027
Request request = new Request.Builder()
31-
.url(EndPoint.BASE_URL + resourceUrl)
28+
.url(EndPoint.BASE_URL + createPostResourceUrl)
3229
.method("POST", body)
3330
.addHeader("accept", "application/json")
3431
.addHeader("Authorization", Client.ACCESS_KEY)
3532
.addHeader("Content-Type", "application/json")
3633
.build();
3734
Response response = client.newCall(request).execute();
3835
assert response.body() != null;
39-
PostModel result = objectMapper.readValue(response.body().string(), PostModel.class);
36+
String responseBody = response.body().string();
37+
result = objectMapper.readValue(responseBody, PostResponse.class);
38+
response.body().close();
39+
4040
System.out.println("createPost result:" + result);
41+
// if (result.getData() != null) {
42+
// PostModel model = (PostModel) result.getData();
43+
// System.out.println("createPost model:" + model);
44+
// }
4145
return result;
4246
}
4347

0 commit comments

Comments
 (0)