Blame src/examples/add-pocket.c

Packit Service c6b9b0
/*
Packit Service c6b9b0
 * Copyright © 2013 Bastien Nocera <hadess@hadess.net>
Packit Service c6b9b0
 *
Packit Service c6b9b0
 * This library is free software; you can redistribute it and/or
Packit Service c6b9b0
 * modify it under the terms of the GNU Lesser General Public
Packit Service c6b9b0
 * License as published by the Free Software Foundation; either
Packit Service c6b9b0
 * version 2 of the License, or (at your option) any later version.
Packit Service c6b9b0
 *
Packit Service c6b9b0
 * This library is distributed in the hope that it will be useful,
Packit Service c6b9b0
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service c6b9b0
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service c6b9b0
 * Lesser General Public License for more details.
Packit Service c6b9b0
 *
Packit Service c6b9b0
 * You should have received a copy of the GNU Lesser General
Packit Service c6b9b0
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit Service c6b9b0
 */
Packit Service c6b9b0
Packit Service c6b9b0
#include <config.h>
Packit Service c6b9b0
Packit Service c6b9b0
#define GOA_API_IS_SUBJECT_TO_CHANGE
Packit Service c6b9b0
#include <goa/goa.h>
Packit Service c6b9b0
Packit Service c6b9b0
#include <locale.h>
Packit Service c6b9b0
#include <rest/rest-proxy.h>
Packit Service c6b9b0
#include <rest/rest-proxy-call.h>
Packit Service c6b9b0
Packit Service c6b9b0
int
Packit Service c6b9b0
main (int argc, char **argv)
Packit Service c6b9b0
{
Packit Service c6b9b0
  GError *error = NULL;
Packit Service c6b9b0
  GoaClient *client;
Packit Service c6b9b0
  GList *accounts, *l;
Packit Service c6b9b0
  GoaAccount *account = NULL;
Packit Service c6b9b0
  GoaOAuth2Based *oauth2 = NULL;
Packit Service c6b9b0
  char *access_token = NULL;
Packit Service c6b9b0
  RestProxy *proxy;
Packit Service c6b9b0
  RestProxyCall *call;
Packit Service c6b9b0
  const char *url;
Packit Service c6b9b0
  const char *tweet_id = NULL;
Packit Service c6b9b0
Packit Service c6b9b0
  setlocale (LC_ALL, "");
Packit Service c6b9b0
Packit Service c6b9b0
  if (argc != 2 && argc != 3) {
Packit Service c6b9b0
    g_print ("Usage: %s URL [TWEET ID]\n", argv[0]);
Packit Service c6b9b0
    return 1;
Packit Service c6b9b0
  }
Packit Service c6b9b0
  url = argv[1];
Packit Service c6b9b0
  if (argv[2] != NULL)
Packit Service c6b9b0
    tweet_id = argv[2];
Packit Service c6b9b0
Packit Service c6b9b0
  client = goa_client_new_sync (NULL, &error);
Packit Service c6b9b0
  if (!client) {
Packit Service c6b9b0
    g_error ("Could not create GoaClient: %s", error->message);
Packit Service c6b9b0
    return 1;
Packit Service c6b9b0
  }
Packit Service c6b9b0
Packit Service c6b9b0
  accounts = goa_client_get_accounts (client);
Packit Service c6b9b0
Packit Service c6b9b0
  /* Find a Pocket account */
Packit Service c6b9b0
  for (l = accounts; l != NULL; l = l->next) {
Packit Service c6b9b0
    GoaObject *object = GOA_OBJECT (l->data);
Packit Service c6b9b0
Packit Service c6b9b0
    account = goa_object_peek_account (object);
Packit Service c6b9b0
    if (g_strcmp0 (goa_account_get_provider_type (account), "pocket") == 0) {
Packit Service c6b9b0
      g_object_ref (account);
Packit Service c6b9b0
      oauth2 = goa_object_get_oauth2_based (object);
Packit Service c6b9b0
      break;
Packit Service c6b9b0
    }
Packit Service c6b9b0
  }
Packit Service c6b9b0
Packit Service c6b9b0
  g_list_free_full (accounts, (GDestroyNotify) g_object_unref);
Packit Service c6b9b0
Packit Service c6b9b0
  g_assert (account);
Packit Service c6b9b0
  g_assert (oauth2);
Packit Service c6b9b0
Packit Service c6b9b0
  if (!goa_oauth2_based_call_get_access_token_sync (oauth2, &access_token, NULL, NULL, &error)) {
Packit Service c6b9b0
    g_error ("Could not get access token: %s", error->message);
Packit Service c6b9b0
    return 1;
Packit Service c6b9b0
  }
Packit Service c6b9b0
Packit Service c6b9b0
  g_print ("Got access tokens\n");
Packit Service c6b9b0
Packit Service c6b9b0
  proxy = rest_proxy_new ("https://getpocket.com/", FALSE);
Packit Service c6b9b0
  call = rest_proxy_new_call (proxy);
Packit Service c6b9b0
  rest_proxy_call_set_method (call, "POST");
Packit Service c6b9b0
  rest_proxy_call_set_function (call, "v3/add");
Packit Service c6b9b0
  rest_proxy_call_add_param (call, "consumer_key", goa_oauth2_based_get_client_id (oauth2));
Packit Service c6b9b0
  rest_proxy_call_add_param (call, "access_token", access_token);
Packit Service c6b9b0
  rest_proxy_call_add_param (call, "url", url);
Packit Service c6b9b0
  if (tweet_id)
Packit Service c6b9b0
    rest_proxy_call_add_param (call, "tweet_id", tweet_id);
Packit Service c6b9b0
Packit Service c6b9b0
  g_print ("Adding to Pocket...\n");
Packit Service c6b9b0
Packit Service c6b9b0
  if (!rest_proxy_call_sync (call, &error)) {
Packit Service c6b9b0
    g_error ("Cannot add to Pocket: %s", error->message);
Packit Service c6b9b0
    return 1;
Packit Service c6b9b0
  }
Packit Service c6b9b0
Packit Service c6b9b0
  g_print ("Added!\n");
Packit Service c6b9b0
Packit Service c6b9b0
  return 0;
Packit Service c6b9b0
}