Skip to content

Commit 9c8ffbf

Browse files
committed
image upload, create post
1 parent 782bc96 commit 9c8ffbf

File tree

4 files changed

+122
-73
lines changed

4 files changed

+122
-73
lines changed

app/src/main/java/xyz/belvi/medium/ClientOperations/MediumClient.java

Lines changed: 99 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -73,50 +73,34 @@ protected NetworkResponse doInBackground(Void... params) {
7373
protected void onPostExecute(NetworkResponse networkResponse) {
7474
super.onPostExecute(networkResponse);
7575
if (networkResponse.isSuccess()) {
76-
try {
77-
switch (apiHost) {
78-
case ACCESS_TOKEN:
79-
((MediumConnectionCallback) connectionCallback).onAccessTokenRetrieved(new OauthDetails(networkResponse.getResponseString()));
80-
break;
81-
case REFRESH_TOKEN:
82-
((MediumConnectionCallback) connectionCallback).onAccessTokenRefreshed(new OauthDetails(networkResponse.getResponseString()));
83-
break;
84-
case ME:
85-
((MediumUserAuthCallback) connectionCallback).onUserDetailsRetrieved(new MediumUser(networkResponse.getResponseString()));
86-
break;
87-
case PUBLICATION:
88-
((PublicationCallback) connectionCallback).onPublicationRetrieved(new Publication().buildPublication(networkResponse.getResponseString()));
89-
break;
90-
case CONTRIBUTION:
91-
((PublicationCallback) connectionCallback).onReceivedContributors(new Contributor().buildContributor(networkResponse.getResponseString()));
92-
break;
93-
case POST:
94-
((MediumPostPublicationCallback) connectionCallback).PostPublished(new Post(networkResponse.getResponseString()));
95-
break;
96-
case PUBLICATION_POST:
97-
((MediumPostPublicationCallback) connectionCallback).PostPublished(new Post(networkResponse.getResponseString()));
98-
break;
99-
case IMAGE_UPLOAD:
100-
((MediumPostPublicationCallback) connectionCallback).ImageUploaded(new MediumImage(networkResponse.getResponseString()));
101-
break;
102-
}
103-
} catch (ClassCastException ex) {
104-
MediumError mediumError = null;
105-
if (apiHost == ApiHost.ACCESS_TOKEN || apiHost == ApiHost.REFRESH_TOKEN) {
106-
107-
mediumError = new MediumError("please, implement MediumConnectionCallback", ErrorCodes.MISMATCH_CALLBACK.getErrorCode());
108-
} else if (apiHost == ApiHost.ME) {
109-
mediumError = new MediumError("please, implement MediumUserAuthCallback", ErrorCodes.MISMATCH_CALLBACK.getErrorCode());
110-
111-
} else if (apiHost == ApiHost.CONTRIBUTION || apiHost == ApiHost.PUBLICATION) {
112-
mediumError = new MediumError("please, implement PublicationCallback", ErrorCodes.MISMATCH_CALLBACK.getErrorCode());
113-
114-
} else if (apiHost == ApiHost.POST || apiHost == ApiHost.PUBLICATION_POST || apiHost == ApiHost.IMAGE_UPLOAD) {
115-
mediumError = new MediumError("please, implement MediumPostPublicationCallback", ErrorCodes.MISMATCH_CALLBACK.getErrorCode());
116-
}
117-
if (mediumError != null)
118-
connectionCallback.connectionFailed(mediumError);
76+
77+
switch (apiHost) {
78+
case ACCESS_TOKEN:
79+
((MediumConnectionCallback) connectionCallback).onAccessTokenRetrieved(new OauthDetails(networkResponse.getResponseString()));
80+
break;
81+
case REFRESH_TOKEN:
82+
((MediumConnectionCallback) connectionCallback).onAccessTokenRefreshed(new OauthDetails(networkResponse.getResponseString()));
83+
break;
84+
case ME:
85+
((MediumUserAuthCallback) connectionCallback).onUserDetailsRetrieved(new MediumUser(networkResponse.getResponseString()));
86+
break;
87+
case PUBLICATION:
88+
((PublicationCallback) connectionCallback).onPublicationRetrieved(new Publication().buildPublication(networkResponse.getResponseString()));
89+
break;
90+
case CONTRIBUTION:
91+
((PublicationCallback) connectionCallback).onReceivedContributors(new Contributor().buildContributor(networkResponse.getResponseString()));
92+
break;
93+
case POST:
94+
((MediumPostPublicationCallback) connectionCallback).PostPublished(new Post(networkResponse.getResponseString()));
95+
break;
96+
case PUBLICATION_POST:
97+
((MediumPostPublicationCallback) connectionCallback).PostPublished(new Post(networkResponse.getResponseString()));
98+
break;
99+
case IMAGE_UPLOAD:
100+
((MediumPostPublicationCallback) connectionCallback).ImageUploaded(new MediumImage(networkResponse.getResponseString()));
101+
break;
119102
}
103+
120104
} else {
121105

122106
MediumError mediumError = new MediumError(networkResponse.getResponseString());
@@ -159,8 +143,62 @@ private String getParamsData() throws UnsupportedEncodingException {
159143
return postData.toString();
160144
}
161145

162-
public MediumClient build() throws MediumException, UnsupportedEncodingException {
146+
private void checkCallbackProperImplementation(ApiHost apiHost) throws MediumException {
147+
switch (apiHost) {
148+
case REQUEST_CODE:
149+
if (!(connectionCallback instanceof MediumConnectionCallback)) {
150+
throw new MediumException("MediumConnectionCallback has to be implemented with ApiHost " + ApiHost.REQUEST_CODE);
151+
}
152+
break;
153+
case ACCESS_TOKEN:
154+
if (!(connectionCallback instanceof MediumConnectionCallback)) {
155+
throw new MediumException("MediumConnectionCallback has to be implemented with ApiHost " + apiHost.name());
156+
}
157+
break;
158+
case REFRESH_TOKEN:
159+
if (!(connectionCallback instanceof MediumConnectionCallback)) {
160+
throw new MediumException("MediumConnectionCallback has to be implemented with ApiHost " + apiHost.name());
161+
}
162+
break;
163+
case PUBLICATION:
164+
if (!(connectionCallback instanceof PublicationCallback)) {
165+
throw new MediumException("PublicationCallback has to be implemented with ApiHost " + apiHost.name());
166+
}
167+
break;
168+
case CONTRIBUTION:
169+
if (!(connectionCallback instanceof PublicationCallback)) {
170+
throw new MediumException("PublicationCallback has to be implemented with ApiHost " + apiHost.name());
171+
}
172+
break;
173+
case POST:
174+
if (!(connectionCallback instanceof MediumPostPublicationCallback)) {
175+
throw new MediumException("MediumPostPublicationCallback has to be implemented with ApiHost " + apiHost.name());
176+
}
177+
break;
178+
case PUBLICATION_POST:
179+
if (!(connectionCallback instanceof MediumPostPublicationCallback)) {
180+
throw new MediumException("MediumPostPublicationCallback has to be implemented with ApiHost " + apiHost.name());
181+
}
182+
break;
183+
case IMAGE_UPLOAD:
184+
if (!(connectionCallback instanceof MediumPostPublicationCallback)) {
185+
throw new MediumException("MediumPostPublicationCallback has to be implemented with ApiHost " + apiHost.name());
186+
}
187+
break;
188+
case ME:
189+
if (!(connectionCallback instanceof MediumUserAuthCallback)) {
190+
throw new MediumException("MediumUserAuthCallback has to be implemented with ApiHost " + apiHost.name());
191+
}
192+
break;
193+
default:
194+
throw new MediumException("Unsupported APi Host");
163195

196+
}
197+
}
198+
199+
public MediumClient build() throws MediumException, UnsupportedEncodingException {
200+
checkCallbackProperImplementation(apiHost);
201+
String url = "";
164202
if (apiHost == ApiHost.REQUEST_CODE) {
165203
LocalBroadcastManager.getInstance(mActivity).registerReceiver(clientConnectionReceiver, new IntentFilter(ClientConstant.connectionReceiverAction));
166204
params.put(RequestParams.RESPONSE_TYPE, ResponseType.CODE);
@@ -172,31 +210,27 @@ public MediumClient build() throws MediumException, UnsupportedEncodingException
172210
apiScope = apiScope.substring(0, apiScope.length() - 1);
173211
}
174212
params.put(RequestParams.SCOPE, apiScope);
213+
url = apiHost.getUriPath() + getParamsData();
175214

176-
String url = apiHost.getUriPath() + getParamsData();
177-
return new MediumClient(url);
178215
} else if (apiHost == ApiHost.ACCESS_TOKEN || apiHost == ApiHost.REFRESH_TOKEN) {
179216
params.put(RequestParams.GRANT_TYPE, apiHost.getGrantType());
180217
networkParams = getParamsData();
181-
return new MediumClient(apiHost.getUriPath());
182-
218+
url = apiHost.getUriPath();
183219
} else if (apiHost == ApiHost.PUBLICATION) {
184-
String url = apiHost.getUriPath() + userId + "/publications";
185-
return new MediumClient(url);
220+
url = apiHost.getUriPath() + userId + "/publications";
186221
} else if (apiHost == ApiHost.CONTRIBUTION) {
187-
String url = apiHost.getUriPath() + publicationId + "/contributors";
188-
return new MediumClient(url);
222+
url = apiHost.getUriPath() + publicationId + "/contributors";
189223
} else if (apiHost == ApiHost.POST) {
190-
String url = apiHost.getUriPath() + userId + "/posts";
191-
return new MediumClient(url);
224+
networkParams = new Post().getPostObj(post);
225+
url = apiHost.getUriPath() + userId + "/posts";
192226
} else if (apiHost == ApiHost.PUBLICATION_POST) {
193-
String url = apiHost.getUriPath() + publicationId + "/posts";
194-
return new MediumClient(url);
195-
} else if (apiHost == ApiHost.IMAGE_UPLOAD) {
196-
return new MediumClient(apiHost.getUriPath());
197-
} else {
198-
throw new MediumException("Unsupported APi Host");
227+
networkParams = new Post().getPostObj(post);
228+
url = apiHost.getUriPath() + publicationId + "/posts";
199229
}
230+
if (url.trim().isEmpty()) {
231+
url = apiHost.getUriPath();
232+
}
233+
return new MediumClient(url);
200234
}
201235

202236
BroadcastReceiver clientConnectionReceiver = new BroadcastReceiver() {
@@ -205,16 +239,12 @@ public void onReceive(Context context, Intent intent) {
205239
boolean isAuthSuccessful = intent.getBooleanExtra(ClientConstant.connectionStatus, false);
206240
if (isAuthSuccessful) {
207241
boolean grantedAccess = intent.getBooleanExtra(ClientConstant.connectionAccessStatus, false);
208-
try {
209-
if (grantedAccess) {
210-
((MediumConnectionCallback) connectionCallback).onCodeRetrieved(intent.getExtras());
211-
} else {
212-
((MediumConnectionCallback) connectionCallback).onAccessDenied();
213-
}
214-
} catch (ClassCastException e) {
215-
connectionCallback.connectionFailed(new MediumError("Please Implement MediumConnectionCallback", ErrorCodes.MISMATCH_CALLBACK.getErrorCode()));
216-
}
217242

243+
if (grantedAccess) {
244+
((MediumConnectionCallback) connectionCallback).onCodeRetrieved(intent.getExtras());
245+
} else {
246+
((MediumConnectionCallback) connectionCallback).onAccessDenied();
247+
}
218248
} else
219249
connectionCallback.connectionFailed(new MediumError("Error occured when making request", ErrorCodes.CONNECTION_FAILED.getErrorCode()));
220250

app/src/main/java/xyz/belvi/medium/Enums/ErrorCodes.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public enum ErrorCodes {
99
AUTH_CODE_EXPIRED(6017),
1010
NO_CODE_SPECIFIED(6013),
1111
INVALID_USER_ID(6026),
12-
MISMATCH_CALLBACK(7002),
1312
CONNECTION_FAILED(7000), UNKNOWN(7001);
1413

1514
private int errorCode;

app/src/main/java/xyz/belvi/medium/MediumObject/Post.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,37 @@
1515
*/
1616
public class Post {
1717
String title, content, canonicalUrl, authorId, licenseUrl, id, url;
18-
ContentFormat contentFormat;
18+
ContentFormat contentFormat = ContentFormat.HTML;
1919
License license;
2020
long publishedAt;
21-
PublishStatus publishStatus;
21+
PublishStatus publishStatus = PublishStatus.DRAFT;
2222
HashSet<String> tags = new HashSet<>();
2323

2424

2525
public Post() {
2626

2727
}
2828

29+
public String getPostObj(Post post) {
30+
JSONObject jsonObject = new JSONObject();
31+
try {
32+
jsonObject.put("title", post.getTitle());
33+
jsonObject.put("contentFormat", post.getContentFormat().name());
34+
jsonObject.put("content", post.getContentFormat().name());
35+
jsonObject.put("canonicalUrl", post.getContentFormat().name());
36+
jsonObject.put("publishStatus", post.getPublishStatus().name());
37+
JSONArray tags = new JSONArray();
38+
for (String tagName : post.getTags()) {
39+
tags.put(tagName);
40+
}
41+
jsonObject.put("tags", tags);
42+
43+
} catch (JSONException e) {
44+
e.printStackTrace();
45+
}
46+
return jsonObject.toString();
47+
}
48+
2949
public Post(String responseObj) {
3050
try {
3151
JSONObject responseObject = new JSONObject(responseObj);

app/src/main/java/xyz/belvi/medium/Test/ApiTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4949
// .addConnectionCallback(this)
5050
// .clientID("347a306d2419").build();
5151

52-
MediumClient mediumClient = new MediumClient.Builder(this, ApiHost.PUBLICATION)
52+
MediumClient mediumClient = new MediumClient.Builder(this, ApiHost.IMAGE_UPLOAD)
5353
.code(code)
5454
.clientSecret("32e426452c95528a27bfb0b88d93d2767c45d2f1")
5555
.tokenType(tokenType)

0 commit comments

Comments
 (0)