GCC Code Coverage Report


Directory: src/
File: src/contact/msg-contact-service.c
Date: 2024-05-18 00:53:33
Exec Total Coverage
Lines: 71 77 92.2%
Functions: 9 9 100.0%
Branches: 16 25 64.0%

Line Branch Exec Source
1 /* Copyright 2023-2024 Jan-Michael Brummer <jan-michael.brummer1@volkswagen.de>
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <libsoup/soup.h>
18 #include <json-glib/json-glib.h>
19
20 #include <stdio.h>
21
22 #include "msg-authorizer.h"
23 #include "msg-error.h"
24 #include "msg-private.h"
25 #include "msg-service.h"
26 #include "msg-contact.h"
27 #include "msg-contact-service.h"
28
29 struct _MsgContactService {
30 MsgService parent_instance;
31 };
32
33
6/7
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 3 times.
18 G_DEFINE_TYPE (MsgContactService, msg_contact_service, MSG_TYPE_SERVICE);
34
35 static void
36 1 msg_contact_service_init (__attribute__ ((unused)) MsgContactService *self)
37 {
38 1 }
39
40 static void
41 2 msg_contact_service_class_init (__attribute__ ((unused)) MsgContactServiceClass *class)
42 {
43 2 }
44
45 MsgContactService *
46 1 msg_contact_service_new (MsgAuthorizer *authorizer)
47 {
48 1 return g_object_new (MSG_TYPE_CONTACT_SERVICE, "authorizer", authorizer, NULL);
49 }
50
51 /**
52 * msg_contact_service_get_contacts:
53 * @self: a contact service
54 * @cancellable: a cancellable
55 * @error: a error
56 *
57 * Get all contacts accessed by contact service.
58 *
59 * Returns: (element-type MsgContact) (transfer full): all contacts
60 */
61 GList *
62 1 msg_contact_service_get_contacts (MsgContactService *self,
63 GCancellable *cancellable,
64 GError **error)
65 {
66 1 JsonObject *root_object = NULL;
67 1 g_autofree char *url = NULL;
68 1 g_autolist (MsgContact) list = NULL;
69 1 JsonArray *array = NULL;
70 1 guint array_length = 0, index = 0;
71 1 g_autoptr (JsonParser) parser = NULL;
72
73
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!msg_service_refresh_authorization (MSG_SERVICE (self), cancellable, error))
74 return NULL;
75
76 1 url = g_strconcat (MSG_API_ENDPOINT, "/me/contacts", NULL);
77
78 do {
79 2 g_autoptr (SoupMessage) message = NULL;
80
81 1 message = msg_service_build_message (MSG_SERVICE (self), "GET", url, NULL, FALSE);
82 1 parser = msg_service_send_and_parse_response (MSG_SERVICE (self), message, &root_object, cancellable, error);
83
84 1 array = json_object_get_array_member (root_object, "value");
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 g_assert (array != NULL);
86
87 1 array_length = json_array_get_length (array);
88
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (index = 0; index < array_length; index++) {
89 1 MsgContact *contact = NULL;
90 1 JsonObject *contact_object = NULL;
91 1 g_autoptr (GError) local_error = NULL;
92
93 1 contact_object = json_array_get_object_element (array, index);
94
95 1 contact = msg_contact_new_from_json (contact_object, &local_error);
96
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (contact) {
97 1 list = g_list_append (list, contact);
98 } else {
99 g_warning ("Could not parse contact object: %s", local_error->message);
100 g_clear_error (&local_error);
101 }
102 }
103
104
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 g_clear_pointer (&url, g_free);
105 1 url = msg_service_get_next_link (root_object);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } while (url != NULL);
107
108 1 return g_steal_pointer (&list);
109 }
110
111 /**
112 * msg_contact_service_create:
113 * @self: a #MsgContextService
114 * @contact: a #MsgContact
115 * @cancellable: a #GCancellable
116 * @error: a #GError
117 *
118 * Create new contact #contact and return new contact object.
119 *
120 * Returns: (transfer full): a new #MsgContact
121 */
122 MsgContact *
123 1 msg_contact_service_create (MsgContactService *self,
124 MsgContact *contact,
125 GCancellable *cancellable,
126 GError **error)
127 {
128 1 g_autoptr (SoupMessage) message = NULL;
129 1 JsonObject *root_object = NULL;
130 1 g_autofree char *url = NULL;
131 1 g_autoptr (GBytes) response = NULL;
132 1 g_autoptr (JsonParser) parser = NULL;
133 1 g_autoptr (JsonBuilder) builder = NULL;
134 1 g_autoptr (JsonNode) node = NULL;
135 1 g_autofree char *json = NULL;
136 1 GBytes *body = NULL;
137
138
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!msg_service_refresh_authorization (MSG_SERVICE (self), cancellable, error))
139 return NULL;
140
141 1 url = g_strconcat (MSG_API_ENDPOINT, "/me/contacts", NULL);
142 1 message = msg_service_build_message (MSG_SERVICE (self), "POST", url, NULL, FALSE);
143
144 1 builder = json_builder_new ();
145 1 json_builder_begin_object (builder);
146 1 json_builder_set_member_name (builder, "givenName");
147 1 json_builder_add_string_value (builder, msg_contact_get_given_name (contact));
148 1 json_builder_set_member_name (builder, "surname");
149 1 json_builder_add_string_value (builder, msg_contact_get_surname (contact));
150 1 json_builder_end_object (builder);
151 1 node = json_builder_get_root (builder);
152 1 json = json_to_string (node, TRUE);
153
154 1 body = g_bytes_new (json, strlen (json));
155 1 soup_message_set_request_body_from_bytes (message, "application/json", body);
156
157 1 parser = msg_service_send_and_parse_response (MSG_SERVICE (self), message, &root_object, cancellable, error);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!parser)
159 return NULL;
160
161 1 return msg_contact_new_from_json (root_object, error);
162 }
163
164 /**
165 * msg_contact_service_delete:
166 * @self: a #MsgContactService
167 * @contact: a #MsgContact
168 * @cancellable: a #GCancellable
169 * @error: a #GError
170 *
171 * Delets #contact.
172 *
173 * Returns: %TRUE for succes, else %FALSE
174 */
175 gboolean
176 1 msg_contact_service_delete (MsgContactService *self,
177 MsgContact *contact,
178 GCancellable *cancellable,
179 GError **error)
180 {
181 1 g_autoptr (SoupMessage) message = NULL;
182 1 g_autofree char *url = NULL;
183 1 g_autoptr (GBytes) response = NULL;
184 1 g_autoptr (JsonParser) parser = NULL;
185
186
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!msg_service_refresh_authorization (MSG_SERVICE (self), cancellable, error))
187 return FALSE;
188
189 1 url = g_strconcat (MSG_API_ENDPOINT, "/me/contacts/", msg_contact_get_id (contact), NULL);
190 1 message = msg_service_build_message (MSG_SERVICE (self), "DELETE", url, NULL, FALSE);
191 1 response = msg_service_send_and_read (MSG_SERVICE (self), message, cancellable, error);
192
193 1 return response != NULL;
194 }
195