static JsonNode * node_from_call (RestProxyCall *call, JsonParser *parser) { JsonNode *root; GError *error; gboolean ret = FALSE; if (call == NULL) return NULL; if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) { g_message ("Error from MySpace: %s (%d)", rest_proxy_call_get_status_message (call), rest_proxy_call_get_status_code (call)); return NULL; } ret = json_parser_load_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call), &error); root = json_parser_get_root (parser); if (root == NULL) { g_message ("Error from MySpace: %s", rest_proxy_call_get_payload (call)); return NULL; } return 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; }
JsonNode * json_node_from_call (RestProxyCall *call) { JsonParser *parser; JsonNode *root = NULL; JsonObject *obj = NULL; GError *error; gboolean ret = FALSE; const char *status; parser = json_parser_new (); if (call == NULL) goto out; if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) { g_message ("Error from Google Map: %s (%d)", rest_proxy_call_get_status_message (call), rest_proxy_call_get_status_code (call)); goto out; } ret = json_parser_load_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call), &error); if (!ret) goto out; root = json_parser_get_root (parser); if (root == NULL) { g_message ("Error from Google Map: %s", rest_proxy_call_get_payload (call)); goto out; } /* check status */ obj = json_node_get_object (root); status = json_object_get_string_member (obj, "status"); if (g_strcmp0 (status, "OK") != 0) { g_message ("Error from Google Map: %s", status); root = NULL; goto out; } root = json_node_copy (root); out: g_object_unref (parser); return root; }
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 _got_trending_topic_updates_cb (RestProxyCall *call, const GError *error_in, GObject *weak_object, gpointer userdata) { SwTwitterItemViewPrivate *priv = GET_PRIVATE (weak_object); SwItemView *item_view = SW_ITEM_VIEW (weak_object); SwSet *set; JsonParser *parser; JsonObject *root_o; SwService *service; GError *error = NULL; if (error_in) { g_warning (G_STRLOC ": Error getting trending topic data: %s", error_in->message); return; } service = sw_item_view_get_service (SW_ITEM_VIEW (item_view)); set = sw_item_set_new (); parser = json_parser_new (); if (!json_parser_load_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call), &error)) { g_warning (G_STRLOC ": error parsing json: %s", error->message); } else { JsonNode *root_n; JsonObject *trends_o; JsonArray *trends_a; GList *values; gint i; root_n = json_parser_get_root (parser); root_o = json_node_get_object (root_n); trends_o = json_object_get_object_member (root_o, "trends"); /* We have to assume just the one object member */ if (json_object_get_size (trends_o) == 1) { values = json_object_get_values (trends_o); trends_a = json_node_get_array ((JsonNode *)(values->data)); for (i = 0; i < json_array_get_length (trends_a); i++) { JsonObject *trend_o; SwItem *item; item = sw_item_new (); sw_item_set_service (item, service); trend_o = json_array_get_object_element (trends_a, i); sw_item_take (item, "date", sw_time_t_to_string (time(NULL))); sw_item_put (item, "id", json_object_get_string_member (trend_o, "name")); sw_item_put (item, "content", json_object_get_string_member (trend_o, "name")); sw_set_add (set, (GObject *)item); g_object_unref (item); } g_list_free (values); } } sw_item_view_set_from_set (SW_ITEM_VIEW (item_view), set); /* Save the results of this set to the cache */ sw_cache_save (service, priv->query, priv->params, set); sw_set_unref (set); g_object_unref (parser); }
static gchar * get_identity_sync (GoaOAuthProvider *provider, const gchar *access_token, const gchar *access_token_secret, gchar **out_presentation_identity, GCancellable *cancellable, GError **error) { RestProxy *proxy; RestProxyCall *call; JsonParser *parser; JsonObject *json_object; gchar *ret; gchar *id; gchar *presentation_identity; ret = NULL; proxy = NULL; call = NULL; parser = NULL; id = NULL; presentation_identity = NULL; /* TODO: cancellable */ proxy = oauth_proxy_new_with_token (goa_oauth_provider_get_consumer_key (provider), goa_oauth_provider_get_consumer_secret (provider), access_token, access_token_secret, "http://api.flickr.com/services/rest", FALSE); call = rest_proxy_new_call (proxy); rest_proxy_call_add_param (call, "method", "flickr.test.login"); rest_proxy_call_add_param (call, "format", "json"); rest_proxy_call_add_param (call, "nojsoncallback", "1"); rest_proxy_call_set_method (call, "GET"); if (!rest_proxy_call_sync (call, error)) goto out; if (rest_proxy_call_get_status_code (call) != 200) { g_set_error (error, GOA_ERROR, GOA_ERROR_FAILED, _("Expected status 200 when requesting user id, instead got status %d (%s)"), rest_proxy_call_get_status_code (call), rest_proxy_call_get_status_message (call)); goto out; } parser = json_parser_new (); if (!json_parser_load_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call), error)) { g_prefix_error (error, _("Error parsing response as JSON: ")); goto out; } json_object = json_node_get_object (json_parser_get_root (parser)); json_object = json_object_get_object_member (json_object, "user"); if (json_object == NULL) { g_set_error (error, GOA_ERROR, GOA_ERROR_FAILED, _("Didn't find user member in JSON data")); goto out; } id = g_strdup (json_object_get_string_member (json_object, "id")); if (id == NULL) { g_set_error (error, GOA_ERROR, GOA_ERROR_FAILED, _("Didn't find user.id member in JSON data")); goto out; } json_object = json_object_get_object_member (json_object, "username"); if (json_object == NULL) { g_set_error (error, GOA_ERROR, GOA_ERROR_FAILED, _("Didn't find user.username member in JSON data")); goto out; } presentation_identity = g_strdup (json_object_get_string_member (json_object, "_content")); if (presentation_identity == NULL) { g_set_error (error, GOA_ERROR, GOA_ERROR_FAILED, _("Didn't find user.username._content member in JSON data")); goto out; } ret = id; id = NULL; if (out_presentation_identity != NULL) { *out_presentation_identity = presentation_identity; presentation_identity = NULL; } out: g_free (id); g_free (presentation_identity); if (call != NULL) g_object_unref (call); if (proxy != NULL) g_object_unref (proxy); return ret; }
JsonNode * json_node_from_call (RestProxyCall *call, GError** error) { JsonNode *root; JsonObject *object = NULL; char *error_message = NULL; JsonParser *parser = NULL; g_return_val_if_fail (call, NULL); if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) { g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR, "Error from Facebook: %s (%d)", rest_proxy_call_get_status_message (call), rest_proxy_call_get_status_code (call)); g_object_unref (parser); return NULL; } parser = json_parser_new (); if (!json_parser_load_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call), NULL)) { g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR, "Malformed JSON from Facebook: %s", rest_proxy_call_get_payload (call)); g_object_unref (parser); return NULL; } root = json_parser_get_root (parser); if (root) root = json_node_copy (root); g_object_unref (parser); if (root == NULL) { g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR, "Error from Facebook: %s", rest_proxy_call_get_payload (call)); return NULL; } /* * Is it an error? If so, it'll be a hash containing * the key "error", which maps to a hash containing * a key "message". */ if (json_node_get_node_type (root) == JSON_NODE_OBJECT) { object = json_node_get_object (root); } if (object && json_object_has_member (object, "error")) { JsonNode *inner = json_object_get_member (object, "error"); JsonObject *inner_object = NULL; if (json_node_get_node_type (inner) == JSON_NODE_OBJECT) inner_object = json_node_get_object (inner); if (inner_object && json_object_has_member (inner_object, "message")) error_message = get_child_node_value (inner, "message"); } if (error_message) { g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR, "Error response from Facebook: %s", error_message); g_free (error_message); json_node_free (root); return NULL; } else { return root; } }