Git init
[profile/ivi/libsoup2.4.git] / tests / getbug.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2001-2003, Ximian, Inc.
4  */
5
6 #include <ctype.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14
15 #include <libsoup/soup.h>
16
17 static GMainLoop *loop;
18
19 static void
20 print_value (GValue *value)
21 {
22         if (G_VALUE_HOLDS_STRING (value))
23                 printf ("%s", g_value_get_string (value));
24         else if (G_VALUE_HOLDS_INT (value))
25                 printf ("%d", g_value_get_int (value));
26         else if (G_VALUE_HOLDS_DOUBLE (value))
27                 printf ("%f", g_value_get_double (value));
28         else if (G_VALUE_TYPE (value) == G_TYPE_VALUE_ARRAY) {
29                 GValueArray *array = g_value_get_boxed (value);
30                 int i;
31                 printf ("[ ");
32                 for (i = 0; i < array->n_values; i++) {
33                         if (i != 0)
34                                 printf (", ");
35                         print_value (&array->values[i]);
36                 }
37                 printf (" ]");
38         } else
39                 printf ("(%s)", g_type_name (G_VALUE_TYPE (value)));
40 }
41
42 static void
43 print_struct_field (gpointer key, gpointer value, gpointer data)
44 {
45         printf ("%s: ", (char *)key);
46         print_value (value);
47         printf ("\n");
48 }
49
50 static void
51 got_response (SoupSession *session, SoupMessage *msg, gpointer user_data)
52 {
53         GHashTable *hash;
54         GError *error = NULL;
55
56         if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
57                 fprintf (stderr, "%d %s\n", msg->status_code, msg->reason_phrase);
58                 exit (1);
59         }
60
61         if (!soup_xmlrpc_extract_method_response (msg->response_body->data,
62                                                   msg->response_body->length,
63                                                   &error,
64                                                   G_TYPE_HASH_TABLE, &hash)) {
65                 if (!error) {
66                         fprintf (stderr, "Could not parse XMLRPC response:\n%d %s\n\n",
67                                  msg->status_code, msg->reason_phrase);
68                         fprintf (stderr, "%s\n", msg->response_body->data);
69                 } else {
70                         fprintf (stderr, "XML-RPC error: %d %s",
71                                  error->code, error->message);
72                 }
73                 exit (1);
74         }
75
76         g_hash_table_foreach (hash, print_struct_field, NULL);
77         g_hash_table_destroy (hash);
78
79         g_main_loop_quit (loop);
80 }
81
82 static void
83 usage (void)
84 {
85         fprintf (stderr, "Usage: getbug [-p proxy_uri] [bugzilla-uri] bug-number\n");
86         exit (1);
87 }
88
89 int
90 main (int argc, char **argv)
91 {
92         SoupSession *session;
93         SoupURI *proxy = NULL;
94         SoupMessage *msg;
95         const char *uri = "http://bugzilla.redhat.com/bugzilla/xmlrpc.cgi";
96         int opt, bug;
97
98         g_thread_init (NULL);
99         g_type_init ();
100
101         while ((opt = getopt (argc, argv, "p:")) != -1) {
102                 switch (opt) {
103                 case 'p':
104                         proxy = soup_uri_new (optarg);
105                         if (!proxy) {
106                                 fprintf (stderr, "Could not parse %s as URI\n",
107                                          optarg);
108                                 exit (1);
109                         }
110                         break;
111
112                 case '?':
113                         usage ();
114                         break;
115                 }
116         }
117         argc -= optind;
118         argv += optind;
119
120         if (argc > 1) {
121                 uri = argv[0];
122                 argc--;
123                 argv++;
124         }
125
126         if (argc != 1 || (bug = atoi (argv[0])) == 0)
127                 usage ();
128
129         session = soup_session_async_new_with_options (
130                 SOUP_SESSION_PROXY_URI, proxy,
131                 NULL);
132
133         msg = soup_xmlrpc_request_new (uri, "bugzilla.getBug",
134                                        G_TYPE_INT, bug,
135                                        G_TYPE_INVALID);
136         if (!msg) {
137                 fprintf (stderr, "Could not create web service request to '%s'\n", uri);
138                 exit (1);
139         }
140         soup_session_queue_message (session, SOUP_MESSAGE (msg),
141                                     got_response, NULL);
142
143         loop = g_main_loop_new (NULL, TRUE);
144         g_main_loop_run (loop);
145         g_main_loop_unref (loop);
146
147         return 0;
148 }