void rsyncEdwExpDataType(char *url, char *userId, char *password, char *outTab) /* rsyncEdwExpDataType - Get experiment and data types from ENCODED via json.. */ { char *jsonText = NULL; if (jsonIn) readInGulp(jsonIn, &jsonText, NULL); else jsonText = getJsonViaHttps(url, userId, password); if (jsonOut) writeGulp(jsonOut, jsonText, strlen(jsonText)); struct jsonElement *jsonRoot = jsonParse(jsonText); char *expListName = "@graph"; struct jsonElement *jsonExpList = jsonMustFindNamedField(jsonRoot, "", expListName); verbose(1, "Got @graph %p\n", jsonExpList); struct slRef *ref, *refList = jsonListVal(jsonExpList, expListName); verbose(1, "Got %d experiments\n", slCount(refList)); FILE *f = mustOpen(outTab, "w"); int realExpCount = 0; for (ref = refList; ref != NULL; ref = ref->next) { struct jsonElement *el = ref->val; char *acc = jsonStringField(el, "accession"); char *dataType = jsonStringField(el, "assay_term_name"); if (dataType != NULL) { fprintf(f, "%s\t%s\t%s\t%s\t%s\n", acc, dataType, jsonOptionalStringField(el, "lab.title", ""), jsonOptionalStringField(el, "biosample_term_name", ""), jsonOptionalStringField(el, "award.rfa", "")); ++realExpCount; } } verbose(1, "Got %d experiments with dataType\n", realExpCount); carefulClose(&f); }
void syncOneRecord(struct sqlConnection *conn, char *type, struct jsonWrite *json, char *table, long long id) /* Send over one record and save UUID result to row of table defined by id in idField. */ { /* Construct dyString for URL */ struct dyString *dyUrl = dyStringNew(0); dyStringPrintf(dyUrl, "http://%s:%s@%s/%s/", gUserId, gPassword, gHost, type); verbose(2, "%s\n", dyUrl->string); /* Construct dyString for http header */ struct dyString *dyHeader = dyStringNew(0); dyStringPrintf(dyHeader, "Content-length: %d\r\n", json->dy->stringSize); dyStringPrintf(dyHeader, "Content-type: text/javascript\r\n"); /* Send header and then JSON */ int sd = netOpenHttpExt(dyUrl->string, "POST", dyHeader->string); mustWriteFd(sd, json->dy->string, json->dy->stringSize); /* Grab response */ struct dyString *dyText = netSlurpFile(sd); close(sd); uglyf("%s\n", dyText->string); /* Turn it into htmlPage structure - this will also parse out http header */ struct htmlPage *response = htmlPageParse(dyUrl->string, dyText->string); uglyf("status %s %d\n", response->status->version, response->status->status); /* If we got bad status abort with hopefully very informative message. */ int status = response->status->status; if (status != 200 && status != 201) // HTTP codes { errAbort("ERROR - Metadatabase returns %d to our post request\n" "POSTED JSON: %s\n" "URL: %s\n" "FULL RESPONSE: %s\n", status, json->dy->string, dyUrl->string, dyText->string); } /* Parse uuid out of json response. It should look something like { "status": "success", "@graph": [ { "description": "The macs2 peak calling software from Tao Liu.", "name": "macs2", "title": "macs2", "url": "https://github.com/taoliu/MACS/", "uuid": "9bda84fd-9872-49e3-9aa0-b71adbf9f31d", "schema_version": "1", "source_url": "https://github.com/taoliu/MACS/", "references": [], "@id": "/software/9bda84fd-9872-49e3-9aa0-b71adbf9f31d/", "@type": ["software", "item"], "aliases": [] } ], "@type": ["result"] } */ struct jsonElement *jsonRoot = jsonParse(response->htmlText); struct jsonElement *graph = jsonMustFindNamedField(jsonRoot, "", "@graph"); struct slRef *ref = jsonListVal(graph, "@graph"); assert(slCount(ref) == 1); struct jsonElement *graphEl = ref->val; char *uuid = jsonStringField(graphEl, "uuid"); uglyf("Got uuid %s\n", uuid); /* Save uuid to table */ char query[256]; sqlSafef(query, sizeof(query), "update %s set metaUuid='%s' where id=%lld", table, uuid, id); sqlUpdate(conn, query); /* Clean up */ dyStringFree(&dyUrl); dyStringFree(&dyHeader); dyStringFree(&dyText); response->fullText = NULL; // avoid double free of this htmlPageFree(&response); }