示例#1
0
static WebSearchServiceData *AllocateWebSearchServiceData (json_t *op_json_p)
{
	WebSearchServiceData *service_data_p = (WebSearchServiceData *) AllocMemory (sizeof (WebSearchServiceData));
	
	if (service_data_p)
		{
			WebServiceData *data_p = & (service_data_p -> wssd_base_data);

			if (InitWebServiceData (data_p, op_json_p))
				{
					service_data_p -> wssd_link_selector_s = GetJSONString (op_json_p, "link_selector");

					if (service_data_p -> wssd_link_selector_s)
						{
							service_data_p -> wssd_title_selector_s = GetJSONString (op_json_p, "title_selector");

							return service_data_p;
						}

					ClearWebServiceData (data_p);
				}

			FreeMemory (service_data_p);
		}
		
	return NULL;
}
ExternalBlastTool :: ExternalBlastTool (BlastServiceJob *job_p, const BlastServiceData *data_p, const json_t *root_p)
	: BlastTool (job_p, data_p, root_p, BS_DEFAULT_OUTPUT_FORMAT)
{
	ebt_blast_s = GetJSONString (root_p, EBT_COMMAND_LINE_EXECUTABLE_S);
	if (!ebt_blast_s)
		{
			throw std :: invalid_argument ("blast executable name not set");
		}

	ebt_working_directory_s = GetJSONString (root_p, EBT_WORKING_DIR_S);
	if (!ebt_working_directory_s)
		{
			throw std :: invalid_argument ("working directory not set");
		}

	const char *result_s = GetJSONString (root_p, EBT_RESULTS_FILE_S);
	if (result_s)
		{
			ebt_results_filename_s = CopyToNewString (result_s, 0, false);

			if (!ebt_results_filename_s)
				{
					throw std :: invalid_argument ("failed to copy results filename");
				}
		}
	else
		{
			throw std :: invalid_argument ("results filename not set");
		}
}
示例#3
0
VJSONObject* VValueBag::BuildJSONObject(VError& outError) const
{
    // will use stringify until redone in a more elegant way
	VJSONObject* result = nil;
	VString jsonstr;
	outError = GetJSONString(jsonstr);
	if (outError == VE_OK)
	{
		VJSONValue val;
		outError = val.ParseFromString(jsonstr);
		if (val.IsObject() && outError == VE_OK)
			result = RetainRefCountable(val.GetObject());
	}
	return result;
}
示例#4
0
void CSendHost_Imgur::SendThread(void* obj)
{
	CSendHost_Imgur* self = (CSendHost_Imgur*)obj;
	/// send DATA and wait for m_nlreply
	NETLIBHTTPREQUEST* reply = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)g_hNetlibUser, (LPARAM)&self->m_nlhr);
	self->HTTPFormDestroy(&self->m_nlhr);
	if (reply) {
		if (reply->dataLength) {
			char buf[128];

			if (GetJSONBool(reply->pData, reply->dataLength, "success")) {
				GetJSONString(reply->pData, reply->dataLength, "data[link]", buf, sizeof(buf));

				mir_free(self->m_URL), self->m_URL = mir_strdup(buf);
				char* ext = strrchr(self->m_URL, '.');
				if (ext) {
					size_t thumblen = mir_strlen(self->m_URL) + 2;
					mir_free(self->m_URLthumb), self->m_URLthumb = (char*)mir_alloc(thumblen);
					thumblen = ext - self->m_URL;
					memcpy(self->m_URLthumb, self->m_URL, thumblen);
					self->m_URLthumb[thumblen] = 'm'; // 320x320, see http://api.imgur.com/models/image
					mir_strcpy(self->m_URLthumb + thumblen + 1, self->m_URL + thumblen);
				}
				CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)reply);
				self->svcSendMsgExit(self->m_URL); return;
			}
			else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, GetJSONInteger(reply->pData, reply->dataLength, "status", 0));
		}
		else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, reply->resultCode);

		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)reply);
	}
	else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, self->m_nlhr.resultCode);

	self->Exit(ACKRESULT_FAILED);
}
示例#5
0
static drbClient *CreateClient (char *token_key_s, char *token_secret_s)
{
	drbClient *client_p = NULL;
	
	const json_t *dropbox_json_p = GetGlobalConfigValue ("dropbox");

	if (dropbox_json_p)
		{
			const char *key_s = GetJSONString (dropbox_json_p, "key");
			const char *secret_s = GetJSONString (dropbox_json_p, "secret");

			if (key_s && secret_s)
				{
					/* Global initialisation */
					drbInit ();

					client_p = drbCreateClient ((char *) key_s, (char *) secret_s, token_key_s, token_secret_s);

					if (client_p)
						{
							// Request a AccessToken if undefined (NULL)
							if (! (token_key_s && token_secret_s))
								{
									bool success_flag = false;
									drbOAuthToken *req_token_p = drbObtainRequestToken (client_p);

									if (req_token_p)
										{
											drbOAuthToken *acc_token_p = NULL;
											char *url_s = drbBuildAuthorizeUrl (req_token_p);
											printf("Please visit %s\nThen press Enter...\n", url_s);
											free (url_s);
											fgetc (stdin);

											acc_token_p = drbObtainAccessToken (client_p);

											if (acc_token_p)
												{
													// This key and secret can replace the NULL value in t_key and
													// t_secret for the next time.
													printf("key:    %s\nsecret: %s\n", acc_token_p -> key, acc_token_p -> secret);
													success_flag = true;
												}
											else
												{
													fprintf(stderr, "Failed to obtain an AccessToken...\n");
												}
										}
									else
										{
											fprintf(stderr, "Failed to obtain a RequestToken...\n");
										}

									if (success_flag)
										{
											/* Set default arguments to not repeat them on each API call */
											drbSetDefault (client_p, DRBOPT_ROOT, DRBVAL_ROOT_AUTO, DRBOPT_END);
										}
									else
										{
											drbDestroyClient (client_p);
											client_p = NULL;
										}

								}		/* if (! (token_key_s && token_secret_s) */

						}		/* if (client_p) */

				}		/* if (key_s && secret_s) */

		}		/* if (dropbox_json_p) */

	
	return client_p;
}
示例#6
0
ExternalServer *CreateExternalServerFromJSON (const json_t *json_p)
{
	ExternalServer *server_p = NULL;
	const char *name_s = GetJSONString (json_p, SERVER_NAME_S);

	if (name_s)
		{
			const char *uri_s = GetJSONString (json_p, SERVER_URI_S);

			if (uri_s)
				{
					const char *uuid_s = GetJSONString (json_p, SERVER_UUID_S);
					ConnectionType ct = CT_WEB;
					const char *type_s = GetJSONString (json_p, SERVER_CONNECTION_TYPE_S);

					if (type_s)
						{
							if (strcmp (type_s, CONNECTION_RAW_S) == 0)
								{
									ct = CT_RAW;
								}
						}		/* if (type_s) */


					server_p = AllocateExternalServer (name_s, uri_s, uuid_s, ct);

					if (server_p)
						{
							json_t *paired_services_json_p = json_object_get (json_p, SERVER_PAIRED_SERVICES_S);

							if (paired_services_json_p)
								{
									if (json_is_array (paired_services_json_p))
										{
											size_t i;
											json_t *paired_service_json_p;

											json_array_foreach (paired_services_json_p, i, paired_service_json_p)
												{
													if (!AddPairedServiceFromJSON (server_p, paired_service_json_p))
														{
															char *dump_s = json_dumps (paired_service_json_p, JSON_INDENT (2));

															if (dump_s)
																{
																	PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "Failed to add paired service from \"%s\"", dump_s);
																	free (dump_s);
																}
															else
																{
																	PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "Failed to add paired service from json");
																}
														}
												}

										}
									else if (json_is_object (paired_services_json_p))
										{
											if (!AddPairedServiceFromJSON (server_p, paired_services_json_p))
												{
													char *dump_s = json_dumps (paired_services_json_p, JSON_INDENT (2));

													if (dump_s)
														{
															PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "Failed to add paired service from \"%s\"", dump_s);
															free (dump_s);
														}
													else
														{
															PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "Failed to add paired service from json");
														}
												}
										}
									else
										{
											PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "%s is of type %d", SERVER_PAIRED_SERVICES_S, json_typeof (paired_services_json_p));
										}

								}		/* if (paired_services_json_p) */

						}		/* if (server_p) */
					else
						{
							PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "Failed to allocate external server %s on %s", name_s, uri_s);
						}

				}		/* if (uri_s) */
bool ServicePrefsWidget :: SetServiceParams (const json_t *service_config_p)
{
	bool success_flag = true;
	const json_t *json_p = json_object_get (service_config_p, SERVICE_RUN_S);

	if (json_p && (json_is_true (json_p)))
		{
			SetRunFlag (true);
		}
	else
		{
			SetRunFlag (false);
		}

	/* Set the params */
	json_p = json_object_get (service_config_p, PARAM_SET_KEY_S);

	if (json_p)
		{
			const json_t *params_json_p = json_object_get (json_p, PARAM_SET_PARAMS_S);

			if (params_json_p)
				{
					if (json_is_array (params_json_p))
						{
							json_t *param_p;
							size_t i;

							json_array_foreach (params_json_p, i, param_p)
								{
									const char *param_name_s = GetJSONString (param_p, PARAM_NAME_S);

									if (param_name_s)
										{
											BaseParamWidget *widget_p = spw_params_widget_p -> GetWidgetForParameter (param_name_s);

											if (widget_p)
												{
													const json_t *param_value_p = json_object_get (param_p, PARAM_CURRENT_VALUE_S);

													if (param_value_p)
														{
															if (! (widget_p -> SetValueFromJSON (param_value_p)))
																{
																	PrintJSONToErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, param_value_p, "Failed to set %s -> %s from json", spw_service_name_s, param_name_s);
																}
														}
													else
														{
															PrintJSONToErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, param_p, "Failed to get parameter value %s -> %s from json", spw_service_name_s, PARAM_CURRENT_VALUE_S);
														}

												}		/* if (widget_p) */
											else
												{
													PrintJSONToErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, param_p, "Failed to get widget for %s -> %s from json", spw_service_name_s, param_name_s);
												}

										}		/* if (param_name_s) */
									else
										{
											PrintJSONToErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, param_p, "Failed to get parameter name %s -> %s from json", spw_service_name_s, PARAM_NAME_S);
										}

								}		/* json_array_foreach (params_json_p, i, param_p) */

						}		/* if (json_is_array (params_json_p)) */

				}
			else
				{
					PrintJSONToErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, json_p, "Failed to get child %s from json", PARAM_SET_PARAMS_S);
				}
		}
static int32 AddRemoteResultsToServiceJobs (const json_t *server_response_p, ServiceJobSet *jobs_p, const char * const remote_service_s, const char * const remote_uri_s, const BlastServiceData *blast_data_p)
{
	int32 num_successful_runs = 0;

	if (server_response_p)
		{
			const json_t *service_results_p = json_object_get (server_response_p, SERVICE_RESULTS_S);

			if (service_results_p)
				{
					if (json_is_array (service_results_p))
						{
							size_t i;
							json_t *service_result_p;

							if (remote_service_s)
								{
									json_array_foreach (service_results_p, i, service_result_p)
										{
											const char *service_name_s = GetJSONString (service_result_p, SERVICE_NAME_S);

											if (service_name_s)
												{
													if (strcmp (service_name_s, remote_service_s) == 0)
														{
															OperationStatus status;

															if (GetStatusFromJSON (service_result_p, &status))
																{
																	switch (status)
																		{
																			case OS_SUCCEEDED:
																				{
																					/* Get the results and add them to our list of jobs */
																					json_t *results_p = json_object_get (service_result_p, SERVICE_RESULTS_S);

																					if (results_p)
																						{
																							if (json_is_array (results_p))
																								{
																									bool id_flag = false;
																									uuid_t remote_id;
																									size_t j;
																									json_t *job_json_p;
																									Service *service_p = jobs_p -> sjs_service_p;
																									const char *name_s = GetJSONString (service_result_p, JOB_NAME_S);
																									const char *description_s = GetJSONString (service_result_p, JOB_DESCRIPTION_S);
																									const char *remote_id_s = GetJSONString (service_result_p, JOB_UUID_S);

																									if (remote_id_s)
																										{
																											if (uuid_parse (remote_id_s, remote_id) == 0)
																												{
																													id_flag = true;
																												}
																										}

																									json_array_foreach (results_p, j, job_json_p)
																										{
																											RemoteServiceJob *job_p = CreateRemoteServiceJobFromResultsJSON (job_json_p, service_p, name_s, description_s, status);

																											if (job_p)
																												{
																													bool added_flag = false;
																													/*
																													 * Save the details to access the remote service with a file named using
																													 * the local uuid
																													 */
																													if ((job_p -> rsj_uri_s = CopyToNewString (remote_uri_s, 0, false)) != NULL)
																														{
																															if ((job_p -> rsj_service_name_s = CopyToNewString (remote_service_s, 0, false)) != NULL)
																																{
																																	if (id_flag)
																																		{
																																			uuid_copy (job_p -> rsj_job_id, remote_id);

																																			if (AddServiceJobToServiceJobSet (jobs_p, & (job_p -> rsj_job)))
																																				{
																																					if (!SaveRemoteJobDetails (job_p, blast_data_p))
																																						{
																																							PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "Failed to save remote info file for \"%s\"", remote_id_s);
																																						}

																																					added_flag = true;
																																					++ num_successful_runs;
																																				}
																																			else
																																				{
																																					PrintJSONToErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, service_results_p, "Failed to add ServiceJob to ServiceJobSet ");
																																				}

																																		}
																																}
																															else
																																{
																																	PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "Failed to add remote service name \"%s\"", remote_service_s);
																																}		/* if (! (job_p -> bsj_job.sj_remote_uri_s)) */
																														}
																													else
																														{
																															PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "Failed to add remote uri \"%s\"", remote_uri_s);
																														}		/* if (! (job_p -> bsj_job.sj_remote_uri_s)) */

																													if (!added_flag)
																														{
																															FreeServiceJob (& (job_p -> rsj_job));
																														}

																												}		/* if (job_p) */
																											else
																												{
																													PrintJSONToErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, service_results_p, "Failed to create ServiceJob ");
																												}

																										}		/* json_array_foreach (results_p, j, job_json_p) */

																								}		/* if (json_is_array (results_p)) */

																						}		/* if (results_p) */
																					else
																						{
																							PrintJSONToErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, service_results_p, "Failed to get SERVICE_RESULTS_S ");
																						}

																				}		/* case OS_SUCCEEDED: */
																				break;

																			default:
																				break;
																		}		/* switch (status) */

																}		/* if (GetStatusFromJSON (service_results_p, &status)) */
															else
																{
																	PrintJSONToErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, service_results_p, "Failed to get OperationStatus");
																}

														}		/* if (strcmp (service_name_s = service_p -> ps_name_s) == 0) */
												}		/* if (service_name_s) */
											else
												{
													PrintJSONToErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, service_results_p, "Failed to get service name");
												}
										}		/* json_array_foreach (server_response_p, i, service_results_p) */
char *GetPreviousRemoteBlastServiceJob (const char *local_job_id_s, const uint32 output_format_code, const BlastServiceData *blast_data_p)
{
	char *result_s = NULL;
	char *job_filename_s = GetLocalJobFilename (local_job_id_s, blast_data_p);

	if (job_filename_s)
		{
			json_error_t e;
			json_t *remote_p = json_load_file (job_filename_s, 0, &e);

			if (remote_p)
				{
					/*
					 * This json_t should consist of the remote server's uri and the uuid
					 * of the job on that server
					 */
					const char *uri_s = GetJSONString (remote_p, JOB_REMOTE_URI_S);

					if (uri_s)
						{
							const char *remote_job_id_s = GetJSONString (remote_p, JOB_REMOTE_UUID_S);

							if (remote_job_id_s)
								{

									/*
									 * Make the call to the remote server for the previous results
									 * using this id
									 */
									ParameterSet *param_set_p = AllocateParameterSet ("Blast service parameters", "The parameters used for the Blast service");
									ParameterGroup *group_p = NULL;

									if (param_set_p)
										{
											Parameter *param_p = SetUpPreviousJobUUIDParamater (blast_data_p, param_set_p, group_p);

											if (param_p)
												{
													if (SetParameterValue (param_p, remote_job_id_s, true))
														{
															param_p = SetUpOutputFormatParamater (blast_data_p, param_set_p, group_p);

															if (param_p)
																{
																	if (SetParameterValue (param_p, &output_format_code, true))
																		{
																			Service *service_p = blast_data_p -> bsd_base_data.sd_service_p;

																			if (service_p)
																				{
																					const char *service_name_s = GetServiceName (blast_data_p -> bsd_base_data.sd_service_p);

																					if (service_name_s)
																						{
																							json_t *res_p = MakeRemotePairedServiceCall (service_name_s, param_set_p, uri_s, NULL);

																							if (res_p)
																								{
																									int32 num_added = AddRemoteResultsToServiceJobs (res_p, service_p -> se_jobs_p, service_name_s, uri_s, blast_data_p);

																									#if PAIRED_BLAST_SERVICE_DEBUG >= STM_LEVEL_FINE
																									PrintLog (STM_LEVEL_FINE, __FILE__, __LINE__, "Added " INT32_FMT " jobs from remote results");
																									#endif

																									json_decref (res_p);
																								}		/* if (res_p) */
																							else
																								{
																									PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "MakeRemotePairedServiceCall to \"%s\" at \"%s\" with param set to \"%s\" returned NULL", service_name_s, uri_s, param_p -> pa_current_value.st_string_value_s);
																								}

																						}		/* if (service_name_s) */
																					else
																						{
																							PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to get Blast service name");
																						}

																				}		/* if (service_p) */
																			else
																				{
																					PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to set get Blast service");
																				}

																		}		/* if (SetParameterValue (param_p, &output_format_code, true)) */
																	else
																		{
																			PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to set Parameter value for out put format to " UINT32_FMT, output_format_code);
																		}
																}
															else
																{
																	PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to create Parameter for output format");
																}

														}		/* if (SetParameterValue (param_p, remote_job_id_s, true)) */
													else
														{
															PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to set Parameter value for previous job ids to \"%s\"", remote_job_id_s);
														}

												}		/* if (param_p) */
											else
												{
													PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to create Parameter for previous job ids");
												}

											FreeParameterSet (param_set_p);
										}		/* if (param_set_p) */
									else
										{
											PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to allocate ParamaterSet");
										}

								}		/* if (remote_job_id_s) */
							else
								{
									PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to get \"%s\" from \"%s\"", JOB_REMOTE_UUID_S, job_filename_s);
								}

						}		/* if (uri_s) */
					else
						{
							PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to get \"%s\" from \"%s\"", JOB_REMOTE_UUID_S, job_filename_s);
						}

					json_decref (remote_p);
				}		/* if (remote_p) */
			else
				{
					PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to load remote json data from \"%s\", error at line %d, col %d, \"%s\"", job_filename_s, e.line, e.column, e.text);
				}

			FreeCopiedString (job_filename_s);
		}		/* if (job_filename_s) */
	else
		{
			PrintErrors (STM_LEVEL_WARNING, __FILE__, __LINE__, "Failed to get output filename for \"%s\"", local_job_id_s);
		}

	return result_s;
}