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 * 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 * _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 * 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; }
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; }
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; } }