json_t * get_projects(json_t * root_class){ json_t *links, *workItems,*projects; const char * url; int retcod[] = { 0 }; if (!json_is_object(root_class)) { fprintf(stderr, "***error: root_class is not an object\n"); return NULL; } links = json_object_get(root_class, "_links"); if (!json_is_object(links)) { fprintf(stderr, "***error: root_class._links is not an object\n"); return NULL; } workItems = json_object_get(links, "workItems"); if (!json_is_string(workItems)) { fprintf(stderr, "***error: root_class._links.workItems is not a string\n"); return NULL; } url = json_string_value(workItems); projects = http_get_json(url, retcod); projects = json_object_get(projects, "workItems"); if (!json_is_array(projects)){ fprintf(stderr, "***error: workItems is not an array\n"); return NULL; } return projects; }
const array_t *sc_search_tracks(sc_t *sc, char *q) { char *url = sc_create_url(SC_ETRACKS, NULL, NULL, SC_ARG_LEN(2), SC_ARG_LIST(sc->client_id, "q", q, "limit", "10")); cJSON *root = http_get_json(url); free(url); return sc_tracks_from_json(root); }
const array_t *sc_get_tracks(sc_t *sc) { char *url = sc_create_url(SC_EUSERS, SC_ETRACKS, sc->username, SC_ARG_LEN(0), SC_DEFAULT_ARG_LIST(sc->client_id)); cJSON *root = http_get_json(url); free(url); return sc_tracks_from_json(root); }
const array_t *sc_get_playlists(sc_t *sc) { char *url = sc_create_url(SC_EUSERS, SC_EPLAYLISTS, sc->username, SC_ARG_LEN(0), SC_DEFAULT_ARG_LIST(sc->client_id)); cJSON *root = http_get_json(url); free(url); array_t *playlists = arrnew(cJSON_GetArraySize(root)); for (int i = 0; i < cJSON_GetArraySize(root); i++) { cJSON *playlist = cJSON_GetArrayItem(root, i); arradd(playlists, (void*)sc_playlist_from_json(playlist)); } cJSON_Delete(root); return playlists; }
json_t * get_workItem(json_t * root_projects, int index){ json_t *project, *links, *self ,*workItem; const char * url; int retcod[] = { 0 }; project = json_array_get(root_projects, index); if (!json_is_object(project)) { fprintf(stderr, "***error: projects[%d] is not an object\n", index); return NULL; } links = json_object_get(project, "_links"); if (!json_is_object(links)) { fprintf(stderr, "***error: projects[%d]._links is not an object\n",index); return NULL; } self = json_object_get(links, "self"); if (!json_is_string(self)) { fprintf(stderr, "***error: projects[%d]._links.self is not a string\n", index); return NULL; } url = json_string_value(self); workItem = http_get_json(url, retcod); return workItem; }
json_t * find_class(json_t * root, char * acronym, char * semester , char * class_name){ json_t *classes, *acro, *sem, *name,*current_class ,*links,*self,*class_link; const char *value; size_t i; int retcod[] = { 0 }; // retcod = *int if (!json_is_object(root)) { fprintf(stderr, "***error: root is not an object\n"); return NULL; } classes = json_object_get(root, "classes"); if (!json_is_array(classes)) { fprintf(stderr, "***error: classes is not an array\n"); return NULL; } /* at this point, root i an array, so we will look for the info we need */ for (i = 0; i < json_array_size(classes); i++){ /* get the classes[i] object */ current_class = json_array_get(classes, i); if (!json_is_object(current_class)) { fprintf(stderr, "***error: classes[%d] is not an object\n", i); return NULL; } acro = json_object_get(current_class, "courseUnitShortName"); if (!json_is_string(acro)) { fprintf(stderr, "***error: classes[%d].courseUnitShortName is not a string\n", i); return NULL; } value = json_string_value(acro); if ((strcmp(value, acronym)) != 0) continue; // if the acronym is not the one we want, we skip the rest sem = json_object_get(current_class, "lectiveSemesterShortName"); if (!json_is_string(sem)) { fprintf(stderr, "***error: classes[%d].lectiveSemesterShortName is not a string\n", i); return NULL; } value = json_string_value(sem); if ((strcmp(value, semester)) != 0) continue; // if the semester is not the one we want, we skip the rest name = json_object_get(current_class, "className"); if (!json_is_string(sem)) { fprintf(stderr, "***error: classes[%d].className is not a string\n", i); return NULL; } value = json_string_value(name); if ((strcmp(value, class_name)) != 0) //if the class name is not the one we want, we skip the rest continue; /* by this time, we found the acronym, semester and class name, so let's look for the class link */ links = json_object_get(current_class, "_links"); if (!json_is_object(links)) { fprintf(stderr, "***error: classes[%d]._links is not an object\n", i); return NULL; } self = json_object_get(links, "self"); if (!json_is_string(self)) { fprintf(stderr, "***error: classes[%d]._links.self is not a string\n", i); return NULL; } value = json_string_value(self); class_link = http_get_json(value, retcod); /* using http_get_json function, we return the class link in a json_t type */ return class_link; } return NULL; }