@@ -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
0 commit comments