void test_flickr_error (void) { RestXmlParser *parser; RestXmlNode *root; GError *error; const char test_1[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<rsp stat=\"ok\"><auth></auth></rsp>"; const char test_2[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<foobar/>"; const char test_3[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<rsp stat=\"fail\"><err code=\"108\" msg=\"Invalid frob\" /></rsp>"; parser = rest_xml_parser_new (); root = rest_xml_parser_parse_from_data (parser, test_1, sizeof (test_1) - 1); error = NULL; flickr_proxy_is_successful (root, &error); g_assert_no_error (error); rest_xml_node_unref (root); error = NULL; root = rest_xml_parser_parse_from_data (parser, test_2, sizeof (test_2) - 1); flickr_proxy_is_successful (root, &error); g_assert_error (error, FLICKR_PROXY_ERROR, 0); g_error_free (error); rest_xml_node_unref (root); error = NULL; root = rest_xml_parser_parse_from_data (parser, test_3, sizeof (test_3) - 1); flickr_proxy_is_successful (root, &error); g_assert_error (error, FLICKR_PROXY_ERROR, 108); g_error_free (error); rest_xml_node_unref (root); }
static RestXmlNode * _make_node_from_call (RestProxyCall *call) { static RestXmlParser *parser = NULL; RestXmlNode *root; if (call == NULL) return NULL; if (parser == NULL) parser = rest_xml_parser_new (); if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) { g_warning (G_STRLOC ": Error from Twitter: %s (%d)", rest_proxy_call_get_status_message (call), rest_proxy_call_get_status_code (call)); return NULL; } root = rest_xml_parser_parse_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call)); if (root == NULL) { g_warning (G_STRLOC ": Error parsing payload from Twitter: %s", rest_proxy_call_get_payload (call)); return NULL; } return root; }
static RestXmlNode * xml_node_from_call (RestProxyCall *call, const char *name) { static RestXmlParser *parser = NULL; RestXmlNode *root; if (call == NULL) return NULL; if (parser == NULL) parser = rest_xml_parser_new (); if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) { g_message ("Error from %s: %s (%d)", name, rest_proxy_call_get_status_message (call), rest_proxy_call_get_status_code (call)); return NULL; } root = rest_xml_parser_parse_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call)); if (root == NULL) { g_message ("Error from %s: %s", name, rest_proxy_call_get_payload (call)); return NULL; } return root; }
static RestXmlNode * node_from_call (RestProxyCall *call) { static RestXmlParser *parser = NULL; RestXmlNode *node = NULL; if (call == NULL) return NULL; if (parser == NULL) parser = rest_xml_parser_new (); if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) { g_message (G_STRLOC ": error from Vimeo: %s (%d)", rest_proxy_call_get_status_message (call), rest_proxy_call_get_status_code (call)); return NULL; } node = rest_xml_parser_parse_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call)); /* No content, or wrong content */ if (node == NULL) { g_message (G_STRLOC ": cannot make Vimeo call: %s", rest_proxy_call_get_payload (call)); } g_object_unref (call); return node; }
int main (int argc, char **argv) { GError *error = NULL; RestXmlParser *parser; RestXmlNode *root, *node; char *xml; g_thread_init (NULL); g_type_init (); parser = rest_xml_parser_new (); root = rest_xml_parser_parse_from_data (parser, TEST_XML, strlen (TEST_XML)); g_assert (root); xml = rest_xml_node_print (root); g_assert (xml); if (strcmp (TEST_XML, xml)) { g_error ("Generated output for parsed XML does not match:\n" "in: %s\n" "out: %s\n", TEST_XML, xml); } rest_xml_node_unref (root); root = rest_xml_node_add_child (NULL, "node0"); rest_xml_node_add_attr (root, "a00", "v00"); rest_xml_node_add_attr (root, "a01", "v01"); node = rest_xml_node_add_child (root, "node1"); rest_xml_node_add_attr (node, "a10", "v10"); node = rest_xml_node_add_child (root, "node1"); rest_xml_node_add_attr (node, "a10", "v10"); rest_xml_node_set_content (root, "Cont0"); xml = rest_xml_node_print (root); g_assert (xml); if (strcmp (TEST_XML, xml)) { g_error ("Generated output for constructed XML does not match:\n" "in: %s\n" "out: %s\n", TEST_XML, xml); } rest_xml_node_unref (root); return 0; }
RestXmlNode * ovirt_rest_xml_node_from_call(RestProxyCall *call) { RestXmlParser *parser; RestXmlNode *node; parser = rest_xml_parser_new (); node = rest_xml_parser_parse_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call)); g_object_unref(G_OBJECT(parser)); return node; }
static RestXmlNode * node_from_call (RestProxyCall *call, GError **error) { static RestXmlParser *parser = NULL; RestXmlNode *node; if (call == NULL) return NULL; if (parser == NULL) parser = rest_xml_parser_new (); if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) { g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR, "HTTP error: %s (%d)", rest_proxy_call_get_status_message (call), rest_proxy_call_get_status_code (call)); return NULL; } node = rest_xml_parser_parse_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call)); /* Invalid XML, or incorrect root */ if (node == NULL || !g_str_equal (node->name, "rsp")) { g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR, "malformed remote response: %s", rest_proxy_call_get_payload (call)); if (node) rest_xml_node_unref (node); return NULL; } if (g_strcmp0 (rest_xml_node_get_attr (node, "stat"), "ok") != 0) { RestXmlNode *err; err = rest_xml_node_find (node, "err"); g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR, "remote Vimeo error: %s", err ? rest_xml_node_get_attr (err, "msg") : "unknown"); rest_xml_node_unref (node); return NULL; } return node; }
static RestXmlNode * xml_node_from_call (RestProxyCall *call, const char *name) { static RestXmlParser *parser = NULL; RestXmlNode *root; if (call == NULL) return NULL; if (parser == NULL) parser = rest_xml_parser_new (); if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) { g_message ("Error from %s: %s (%d)", name, rest_proxy_call_get_status_message (call), rest_proxy_call_get_status_code (call)); return NULL; } root = rest_xml_parser_parse_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call)); if (root == NULL) { g_message ("Error from %s: %s", name, rest_proxy_call_get_payload (call)); return NULL; } /* Exception handling */ if (strcmp (name, "Youtube") == 0) { if (strcmp (root->name, "error_response") == 0) { RestXmlNode *node; node = rest_xml_node_find (root, "error_msg"); g_message ("Error response from Youtube: %s\n", node->content); rest_xml_node_unref (root); return NULL; } } return root; }
static RestXmlNode * node_from_call (RestProxyCall *call) { static RestXmlParser *parser = NULL; RestXmlNode *node; if (call == NULL) return NULL; if (parser == NULL) parser = rest_xml_parser_new (); if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) { g_message (G_STRLOC ": error from Last.fm: %s (%d)", rest_proxy_call_get_status_message (call), rest_proxy_call_get_status_code (call)); return NULL; } node = rest_xml_parser_parse_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call)); /* No content, or wrong content */ if (node == NULL || strcmp (node->name, "lfm") != 0) { g_message (G_STRLOC ": cannot make Last.fm call"); /* TODO: display the payload if its short */ if (node) rest_xml_node_unref (node); return NULL; } if (strcmp (rest_xml_node_get_attr (node, "status"), "ok") != 0) { RestXmlNode *err_node; err_node = rest_xml_node_find (node, "error"); g_message (G_STRLOC ": cannot make Last.fm call: %s (code %s)", err_node->content, rest_xml_node_get_attr (err_node, "code")); rest_xml_node_unref (node); return NULL; } return node; }
int main (int argc, char **argv) { RestProxy *proxy; RestProxyCall *call; GError *error = NULL; char pin[256]; RestXmlParser *parser; RestXmlNode *root, *node; g_type_init (); /* Create the proxy */ proxy = oauth_proxy_new (/* Consumer Key */ "NmUm6hxQ9a4u", /* Consumer Secret */ "t4FM7LiUeD4RBwKSPa6ichKPDh5Jx4kt", /* FireEagle endpoint */ "https://fireeagle.yahooapis.com/", FALSE); /* First stage authentication, this gets a request token. */ if (!oauth_proxy_request_token (OAUTH_PROXY (proxy), "oauth/request_token", "oob", &error)) g_error ("Cannot request token: %s", error->message); /* From the token construct a URL for the user to visit */ g_print ("Go to https://fireeagle.yahoo.net/oauth/authorize?oauth_token=%s then enter the verification code\n", oauth_proxy_get_token (OAUTH_PROXY (proxy))); /* Read the PIN */ fgets (pin, sizeof (pin), stdin); g_strchomp (pin); /* Second stage authentication, this gets an access token. */ if (!oauth_proxy_access_token (OAUTH_PROXY (proxy), "oauth/access_token", pin, &error)) g_error ("Cannot request token: %s", error->message); /* Get the user's current location */ call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "api/0.1/user"); if (!rest_proxy_call_run (call, NULL, &error)) g_error ("Cannot make call: %s", error->message); parser = rest_xml_parser_new (); root = rest_xml_parser_parse_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call)); g_object_unref (parser); g_object_unref (call); g_object_unref (proxy); node = rest_xml_node_find (root, "location"); node = rest_xml_node_find (node, "name"); g_print ("%s\n", node->content); return 0; }
static void mex_suggest_complete_cb (MexDownloadQueue *queue, const gchar *uri, const gchar *buffer, gsize count, const GError *error, gpointer userdata) { RestXmlNode *root, *n; RestXmlParser *parser; MexSearchPlugin *self = userdata; MexSearchPluginPrivate *priv = self->priv; priv->suggest_id = NULL; /* hide spinner */ mx_spinner_set_animating (MX_SPINNER (priv->spinner), FALSE); clutter_actor_hide (priv->spinner); if (error) { g_warning ("Error querying Google suggestions: %s", error->message); return; } parser = rest_xml_parser_new (); root = rest_xml_parser_parse_from_data (parser, buffer, count); if (!root) { g_warning ("Unknown error parsing Google suggestions XML"); g_object_unref (parser); return; } /* Clear model */ mex_model_clear (MEX_MODEL (priv->suggest_model)); /* Add new suggestions to model */ n = rest_xml_node_find (root, "CompleteSuggestion"); for (; n; n = n->next) { MexContent *content; const gchar *suggestion; RestXmlNode *node = rest_xml_node_find (n, "suggestion"); if (!node) continue; suggestion = rest_xml_node_get_attr (node, "data"); if (!suggestion) continue; content = MEX_CONTENT (mex_program_new (priv->suggest_model)); mex_content_set_metadata (content, MEX_CONTENT_METADATA_TITLE, suggestion); mex_content_set_metadata (content, MEX_CONTENT_METADATA_MIMETYPE, "x-mex/search"); mex_model_add_content (MEX_MODEL (priv->suggest_model), content); } /* Unref */ rest_xml_node_unref (root); g_object_unref (parser); }