88import android .os .AsyncTask ;
99
1010import org .json .JSONArray ;
11+ import org .json .JSONException ;
1112import org .json .JSONObject ;
1213
1314import java .io .BufferedReader ;
1415import java .io .InputStream ;
1516import java .io .InputStreamReader ;
17+ import java .net .MalformedURLException ;
1618import java .net .URL ;
1719import java .util .ArrayList ;
1820import java .util .Arrays ;
2123import javax .net .ssl .HttpsURLConnection ;
2224
2325class ApiRequestAsyncTask extends
24- AsyncTask <Void , Void , ApiResponse []> {
26+ AsyncTask <Void , Void , ApiNotebookResponse []> {
2527 private final static String API_ROOT = "https://www.onenote.com/api/v1.0/" ;
2628 public List <ApiAsyncResponse > delegates = new ArrayList <ApiAsyncResponse >(2 );
2729 public ApiRequest [] mRequests ;
@@ -38,10 +40,10 @@ public void addDelegate(ApiAsyncResponse delegate) {
3840 }
3941
4042 @ Override
41- protected ApiResponse [] doInBackground (Void ... voids ) {
42- List <ApiResponse > responseList = new ArrayList <ApiResponse >();
43+ protected ApiNotebookResponse [] doInBackground (Void ... voids ) {
44+ List <ApiNotebookResponse > responseList = new ArrayList <ApiNotebookResponse >();
4345 for (ApiRequest request : mRequests ) {
44- ApiResponse [] apiResponses ;
46+ ApiNotebookResponse [] apiResponses ;
4547 try {
4648 apiResponses = requestResource (request );
4749 } catch (Exception ex ) {
@@ -51,22 +53,27 @@ protected ApiResponse[] doInBackground(Void... voids) {
5153 }
5254 responseList .addAll (Arrays .asList (apiResponses ));
5355 }
54- return responseList .toArray (new ApiResponse [responseList .size ()]);
56+ return responseList .toArray (new ApiNotebookResponse [responseList .size ()]);
5557 }
5658
57- private ApiResponse [] requestResource (ApiRequest request ) throws Exception {
59+ private ApiNotebookResponse [] requestResource (ApiRequest request ) throws Exception {
5860 JSONObject responseJSON = getJSONResponse (API_ROOT + request .getEndpointURL ());
5961
60- List <ApiResponse > apiResponses = new ArrayList <ApiResponse >();
62+ List <ApiNotebookResponse > notebookResponses = new ArrayList <ApiNotebookResponse >();
6163 if (responseJSON != null ) {
6264 // Determine if values were returned or an error occurred
6365 if (responseJSON .has ("value" )) {
6466 // Get response values
65- JSONArray values = responseJSON .getJSONArray ("value" );
67+ JSONArray notebookObjects = responseJSON .getJSONArray ("value" );
6668 // Iterate over all values, and convert JSON objects into library objects
67- for (int i = 0 ; i < values .length (); i ++) {
68- JSONObject object = values .getJSONObject (i );
69- apiResponses .add (buildApiResponse (request , object ));
69+ for (int i = 0 ; i < notebookObjects .length (); i ++) {
70+ JSONObject notebookObject = notebookObjects .getJSONObject (i );
71+
72+ ApiNotebookResponse notebook = new ApiNotebookResponse (notebookObject );
73+ notebook .sections = getSections (notebookObject );
74+ notebook .sectionGroups = getSectionGroups (notebookObject );
75+
76+ notebookResponses .add (notebook );
7077 }
7178 } else if (responseJSON .has ("error" )) {
7279 // Process and throw an API error
@@ -85,50 +92,34 @@ private ApiResponse[] requestResource(ApiRequest request) throws Exception {
8592 }
8693 }
8794
88- return apiResponses .toArray (new ApiResponse [ apiResponses .size ()]);
95+ return notebookResponses .toArray (new ApiNotebookResponse [ notebookResponses .size ()]);
8996 }
90-
91- private ApiResponse buildApiResponse (ApiRequest request , JSONObject object ) throws Exception {
92- ApiResponse apiResponse = null ;
93- // Get links
94- JSONObject links = object .optJSONObject ("links" );
95- if (request .getPrimaryEndpoint () == ApiRequestEndpoint .NOTEBOOKS
96- && !request .hasResourceId ()
97- && !request .hasSecondaryEndpoint ()) {
98- // Build notebook response
99- apiResponse = new ApiNotebookResponse ();
100- ((ApiNotebookResponse ) apiResponse ).setIsDefault (
101- object .optBoolean ("isDefault" ));
102- ((ApiNotebookResponse ) apiResponse ).setUserRole (
103- object .optString ("userRole" ));
104- ((ApiNotebookResponse ) apiResponse ).setOwnerName (
105- object .optString ("ownerName" ));
106- } else if (request .getSecondaryEndpoint () == ApiRequestEndpoint .SECTIONS ) {
107- // Build section response
108- apiResponse = new ApiSectionResponse ();
109- ((ApiSectionResponse ) apiResponse ).setIsDefault (
110- object .optBoolean ("isDefault" ));
111- if (links != null ) {
112- ((ApiSectionResponse ) apiResponse ).setPagesUrl (
113- new URL (links .getJSONObject ("pagesUrl" ).getString ("href" )));
114- } else {
115- ((ApiSectionResponse ) apiResponse ).setPagesUrl (
116- new URL (object .getString ("pagesUrl" )));
117- }
118-
119- } else if (request .getSecondaryEndpoint () == ApiRequestEndpoint .SECTION_GROUPS ) {
120- // Build section groups response
121- apiResponse = new ApiSectionGroupResponse ();
97+
98+ private List <ApiSectionGroupResponse > getSectionGroups (JSONObject parentObject ) throws JSONException , MalformedURLException {
99+ List <ApiSectionGroupResponse > sectionGroups = new ArrayList <ApiSectionGroupResponse >();
100+ JSONArray sectionGroupObjects = parentObject .getJSONArray ("sectionGroups" );
101+
102+ for (int k = 0 ; k < sectionGroupObjects .length (); k ++)
103+ {
104+ JSONObject sectionGroupObject = sectionGroupObjects .getJSONObject (k );
105+ ApiSectionGroupResponse sectionGroup = new ApiSectionGroupResponse (sectionGroupObject );
106+ sectionGroup .sectionGroups = getSectionGroups (sectionGroupObject );
107+ sectionGroup .sections = getSections (sectionGroupObject );
108+ sectionGroups .add (sectionGroup );
122109 }
123- if (apiResponse != null ) {
124- // Apply properties common to all response types
125- apiResponse .setId (object .getString ("id" ));
126- apiResponse .setName (object .getString ("name" ));
127- apiResponse .setCreatedTime (object .optString ("createdTime" ));
128- apiResponse .setModifiedTime (object .optString ("lastModifiedTime" ));
129- apiResponse .setLastModifiedBy (object .optString ("lastModifiedBy" ));
110+
111+ return sectionGroups ;
112+ }
113+
114+ private List <ApiSectionResponse > getSections (JSONObject parentObject ) throws JSONException , MalformedURLException
115+ {
116+ List <ApiSectionResponse > sections = new ArrayList <ApiSectionResponse >();
117+ JSONArray sectionObjects = parentObject .getJSONArray ("sections" );
118+ for (int k = 0 ; k < sectionObjects .length (); k ++)
119+ {
120+ sections .add (new ApiSectionResponse (sectionObjects .getJSONObject (k )));
130121 }
131- return apiResponse ;
122+ return sections ;
132123 }
133124
134125
@@ -184,7 +175,7 @@ private JSONObject getJSONResponse(String endpoint) throws Exception {
184175 }
185176
186177 @ Override
187- protected void onPostExecute (ApiResponse [] responses ) {
178+ protected void onPostExecute (ApiNotebookResponse [] responses ) {
188179 super .onPostExecute (responses );
189180 for (ApiAsyncResponse delegate : delegates ) {
190181 delegate .onApiResponse (responses , mCaughtException );
0 commit comments