Line | Branch | Exec | Source |
---|---|---|---|
1 | /* Copyright 2024 Jan-Michael Brummer | ||
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-user.h" | ||
27 | #include "msg-user-service.h" | ||
28 | |||
29 | struct _MsgUserService { | ||
30 | MsgService parent_instance; | ||
31 | }; | ||
32 | |||
33 |
6/7✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2 times.
|
16 | G_DEFINE_TYPE (MsgUserService, msg_user_service, MSG_TYPE_SERVICE); |
34 | |||
35 | static void | ||
36 | 1 | msg_user_service_init (__attribute__ ((unused)) MsgUserService *self) | |
37 | { | ||
38 | 1 | } | |
39 | |||
40 | static void | ||
41 | 2 | msg_user_service_class_init (__attribute__ ((unused)) MsgUserServiceClass *class) | |
42 | { | ||
43 | 2 | } | |
44 | |||
45 | MsgUserService * | ||
46 | 1 | msg_user_service_new (MsgAuthorizer *authorizer) | |
47 | { | ||
48 | 1 | return g_object_new (MSG_TYPE_USER_SERVICE, "authorizer", authorizer, NULL); | |
49 | } | ||
50 | |||
51 | /** | ||
52 | * msg_user_service_get_user: | ||
53 | * @self: a #MsgUserService | ||
54 | * @name: user name (%NULL for me) | ||
55 | * @cancellable: a #GCancellable | ||
56 | * @error: a #GError | ||
57 | * | ||
58 | * Get user information | ||
59 | * | ||
60 | * Returns: (transfer full): request user | ||
61 | */ | ||
62 | MsgUser * | ||
63 | 2 | msg_user_service_get_user (MsgUserService *self, | |
64 | char *name, | ||
65 | GCancellable *cancellable, | ||
66 | GError **error) | ||
67 | { | ||
68 | 2 | g_autoptr (SoupMessage) message = NULL; | |
69 | 2 | JsonObject *root_object = NULL; | |
70 | 2 | g_autofree char *url = NULL; | |
71 | 2 | g_autoptr (GBytes) response = NULL; | |
72 | 2 | g_autoptr (JsonParser) parser = NULL; | |
73 | |||
74 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!msg_service_refresh_authorization (MSG_SERVICE (self), cancellable, error)) |
75 | ✗ | return NULL; | |
76 | |||
77 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (!name) |
78 | 1 | url = g_strconcat (MSG_API_ENDPOINT, "/me", NULL); | |
79 | else | ||
80 | 1 | url = g_strconcat (MSG_API_ENDPOINT, "/me/contacts/users/", name, NULL); | |
81 | |||
82 | 2 | message = msg_service_build_message (MSG_SERVICE (self), "GET", url, NULL, FALSE); | |
83 | 2 | parser = msg_service_send_and_parse_response (MSG_SERVICE (self), message, &root_object, cancellable, error); | |
84 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (!parser) |
85 | 1 | return NULL; | |
86 | |||
87 | 1 | return msg_user_new_from_json (root_object, error); | |
88 | } | ||
89 |