Example #1
0
        /// <summary>
        /// Add a user as a participant of an event by ID.
        /// </summary>
        public static User AddParticipant(int event_id, int user_id, UserSession session = null)
        {
            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            var response = Server.Send(
                string.Format(Server.event_participant_url, event_id),
                session.token,
                "POST",
                new
            {
                event_participant = new
                {
                    participant_id = user_id
                }
            }
                );

            if (response.StatusCode == HttpStatusCode.Created)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                var json   = ParsedJson.Parse(reader.ReadToEnd());
                return(new User(json.GetObject("participant")));
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #2
0
        internal static Complaint InvokeUpdate(
            int complaint_id,
            object body,
            UserSession session = null
            )
        {
            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            // Negative complaint IDs are reserved for creating new complaints.
            var response = Server.Send(
                complaint_id < 0 ? Server.complaint_url :
                string.Format(Server.complaint_manage_url, complaint_id),
                session.token,
                complaint_id < 0 ? "POST" : "PUT",
                new
            {
                complaint = body
            }
                );

            if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return(new Complaint(ParsedJson.Parse(reader.ReadToEnd())));
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #3
0
        internal static Group InvokeUpdate(int group_id, object body, UserSession session = null)
        {
            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            // Negarive IDs are reserved for group creation
            var response = Server.Send(
                group_id < 0 ?
                Server.group_url :
                string.Format(Server.group_manage_url, group_id),
                session.token,
                "POST",
                new
            {
                group = body
            }
                );

            if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return(new Group(ParsedJson.Parse(reader.ReadToEnd())));
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #4
0
        /// <summary>
        /// Create a new user account on the server.
        /// </summary>
        /// <returns>A new user session.</returns>
        static public UserSession CreateUser(string email, string first_name, string last_name, string password)
        {
            var response = Send(
                user_url,
                null,
                "POST",
                new
            {
                user = new
                {
                    email,
                    first_name,
                    last_name,
                    password
                }
            }
                );

            if (response.StatusCode == HttpStatusCode.Created)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

                return(new UserSession(
                           response.Headers.Get("authorization"),
                           ParsedJson.Parse(reader.ReadToEnd())
                           ));
            }

            throw new Exception(UnexpectedStatus(response.StatusCode));
        }
Example #5
0
        internal static Group ManageMembers(int group_id, int user_id, bool add, UserSession session = null)
        {
            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            // Negarive IDs are reserved for group creation
            var response = Server.Send(
                string.Format(Server.group_member_url, group_id),
                session.token,
                add ? "PUT": "DELETE",
                new
            {
                group_member = new
                {
                    member_id = user_id
                }
            }
                );

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return(new Group(ParsedJson.Parse(reader.ReadToEnd()).GetObject("user").GetObject("group")));
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #6
0
 internal Group(ParsedJson info)
 {
     Id          = info.GetInt("id");
     Name        = info.GetString("name");
     Description = info.GetString("description");
     Rules       = info.GetString("rules");
     CreatedAt   = info.GetDateTime("created_at", false).Value;
     UpdatedAt   = info.GetDateTime("updated_at", false).Value;
 }
Example #7
0
 internal Agreement(ParsedJson json)
 {
     Id           = json.GetInt("id");
     PrimaryGroup = new Group(json.GetObject("group"));
     Title        = json.GetString("title");
     Description  = json.GetString("description");
     CreatedAt    = json.GetDateTime("created_at", false).Value;
     UpdatedAt    = json.GetDateTime("updated_at", false).Value;
     Creator      = new User(json.GetObject("creator"));
 }
Example #8
0
        /// <summary>
        /// Marks a complaint with the specified status
        /// </summary>
        /// <remarks>This action requires an administrative account.</remarks>
        /// <returns>New locked state of null otherwise.</returns>
        public static bool?MarkAs(int complaint_id, ComplaintStatus status, UserSession session = null)
        {
            string url;

            switch (status)
            {
            case ComplaintStatus.In_progress:
                url = Server.complaint_mark_progress_url;
                break;

            case ComplaintStatus.Rejected:
                url = Server.complaint_mark_rejected_url;
                break;

            case ComplaintStatus.Resolved:
                url = Server.complaint_mark_resolved_url;
                break;

            default:
                throw new Exception("Invalid action: can't mark a complaint with this status");
            }

            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            var response = Server.Send(
                string.Format(url, complaint_id),
                session.token,
                "PUT",
                null
                );

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                var json   = ParsedJson.Parse(reader.ReadToEnd());

                // Voting can cause status and lock change
                if (json.Members.ContainsKey("complaint"))
                {
                    json = json.GetObject("complaint");

                    if (json.Members.ContainsKey("locked"))
                    {
                        return(json.GetBool("locked"));
                    }
                }

                return(null);
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #9
0
        /// <summary>
        /// Vote for/against completion of this event.
        /// </summary>
        public void Vote(bool finished, UserSession session = null)
        {
            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            // See if there is a vote already
            var update_only = Votes.TryGetValue(session.Info.Id, out EventVote old_vote);

            if (update_only && finished == old_vote.Finished)
            {
                return;
            }

            var response = Server.Send(
                string.Format(Server.event_vote_url, Id),
                session.token,
                update_only ? "PUT" : "POST",
                new
            {
                event_vote = new
                {
                    finished
                }
            }
                );

            if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                var json   = ParsedJson.Parse(reader.ReadToEnd());
                Votes[session.Info.Id] = new EventVote(json);
                UpdatedAt = DateTime.Now;

                // Voting can cause status and lock change
                if (json.Members.ContainsKey("event"))
                {
                    json = json.GetObject("event");

                    if (json.Members.ContainsKey("status"))
                    {
                        Status = json.GetEnum <EventStatus>("status");
                    }

                    if (json.Members.ContainsKey("locked"))
                    {
                        Locked = json.GetBool("locked");
                    }
                }
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #10
0
 internal Expense(ParsedJson json)
 {
     Id           = json.GetInt("id");
     PrimaryGroup = new Group(json.GetObject("group"));
     Name         = json.GetString("name");
     Notes        = json.GetString("notes");
     Price        = json.GetDecimal("price");
     Quantity     = json.GetInt("quantity");
     Archived     = json.GetBool("archived");
     CreatedAt    = json.GetDateTime("created_at", false).Value;
     UpdatedAt    = json.GetDateTime("updated_at", false).Value;
     Creator      = new User(json.GetObject("creator"));
     Participants = json.GetArray("participants").ConvertAll(e => new User(e));
 }
Example #11
0
        /// <summary>
        /// Open an exiting group by ID.
        /// </summary>
        public static Group Query(int group_id)
        {
            var response = Server.Send(
                string.Format(Server.group_manage_url, group_id),
                null,
                "GET",
                null
                );

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return(new Group(ParsedJson.Parse(reader.ReadToEnd())));
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #12
0
        /// <summary>
        /// Enumerates all groups.
        /// </summary>
        public static List <Group> Enumerate()
        {
            var response = Server.Send(
                Server.group_url,
                null,
                "GET",
                null
                );

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return(ParsedJson.ParseArray(reader.ReadToEnd()).ConvertAll(g => new Group(g)));
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #13
0
        /// <summary>
        /// Open an existing event by ID.
        /// </summary>
        public static Event Query(int event_id, UserSession session = null)
        {
            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            var response = Server.Send(
                string.Format(Server.event_manage_url, event_id),
                session.token,
                "GET",
                null
                );

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return(new Event(ParsedJson.Parse(reader.ReadToEnd())));
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #14
0
        /// <summary>
        /// Enumerate events in which you participate.
        /// </summary>
        public static List <Event> Enumerate(UserSession session = null)
        {
            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            var response = Server.Send(
                Server.event_url,
                session.token,
                "GET",
                null
                );

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return(ParsedJson.ParseArray(reader.ReadToEnd()).ConvertAll(e => new Event(e)));
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #15
0
        internal User(ParsedJson info)
        {
            Id = info.GetInt("id");

            // FIXED: when registering, the response contains a group_id instead of a group
            if (info.Members.ContainsKey("group_id"))
            {
                PrimaryGroup = Group.Query(info.GetInt("group_id"));
            }
            else
            {
                PrimaryGroup = new Group(info.GetObject("group"));
            }

            Email     = info.GetString("email");
            FirstName = info.GetString("first_name");
            LastName  = info.GetString("last_name");
            CreatedAt = info.GetDateTime("created_at", false).Value;
            UpdatedAt = info.GetDateTime("updated_at", true);
            Admin     = info.GetBool("admin");
        }
Example #16
0
        internal static EventStatus?MarkEvent(int event_id, bool finished, UserSession session = null)
        {
            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            // Negative event IDs are reserved for creating new events.
            var response = Server.Send(
                finished ?
                string.Format(Server.event_mark_url, event_id) :
                string.Format(Server.event_unmark_url, event_id),
                session.token,
                "PUT",
                null
                );

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var data = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();

                if (data.Length > 0)
                {
                    var json = ParsedJson.Parse(data);

                    if (json.Members.ContainsKey("event"))
                    {
                        json = json.GetObject("event");

                        if (json.Members.ContainsKey("status"))
                        {
                            return(json.GetEnum <EventStatus>("status"));
                        }
                    }
                }

                return(null);
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #17
0
        internal Event(ParsedJson json)
        {
            Id           = json.GetInt("id");
            PrimaryGroup = new Group(json.GetObject("group"));
            Description  = json.GetString("description");
            Title        = json.GetString("title");
            StartsAt     = json.GetDateTime("starts_at", true);
            FinishesAt   = json.GetDateTime("finishes_at", true);
            CreatedAt    = json.GetDateTime("created_at", false).Value;
            UpdatedAt    = json.GetDateTime("updated_at", false).Value;
            Creator      = new User(json.GetObject("creator"));
            Kind         = json.GetEnum <EventKind>("kind");
            Status       = json.GetEnum <EventStatus>("status");
            Locked       = json.GetBool("locked");
            Participants = json.GetArray("participants").ConvertAll(e => new User(e));
            Votes        = new Dictionary <int, EventVote>();

            foreach (var vote in json.GetArray("votes"))
            {
                Votes[vote.GetInt("id")] = new EventVote(vote);
            }
        }
Example #18
0
        protected static Event InvokeUpdate(
            int event_id,
            object payload,
            UserSession session = null
            )
        {
            // Assume current session by default
            session = session ?? Server.FallbackToCurrentSession;

            // Negative event IDs are reserved for creating new events.
            var response = Server.Send(
                event_id < 0 ?
                Server.event_url :
                string.Format(Server.event_manage_url, event_id),
                session.token,
                event_id < 0 ? "POST" : "PUT",
                new
            {
                // The API expects "event" which is reserved in C#
                _event = payload
            },
                new JsonSerializerOptions
            {
                // Fix the "event" issue
                PropertyNamingPolicy = new FixReservedWordsNamingPolicy()
            }
                );

            if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK)
            {
                var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return(new Event(ParsedJson.Parse(reader.ReadToEnd())));
            }

            throw new Exception(Server.UnexpectedStatus(response.StatusCode));
        }
Example #19
0
 internal UserSession(string authToken, ParsedJson info)
 {
     token = authToken;
     Info  = new User(info);
 }
Example #20
0
        internal static Exception CollectServerErrors(WebException e)
        {
            // We can't examine exceptions that are not related
            // to server responses (timeouts, network failures, ...)
            if (!(e.Response is HttpWebResponse response))
            {
                return(e);
            }

            if (response.ContentLength != 0)
            {
                try
                {
                    var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    var json   = ParsedJson.Parse(reader.ReadToEnd());

                    if (json.Members.TryGetValue("error", out JsonElement error) ||
                        json.Members.TryGetValue("errors", out error))
                    {
                        // If the response is just a string then use it
                        if (error.ValueKind == JsonValueKind.String)
                        {
                            return(new Exception(error.GetString(), e));
                        }

                        // If we have key-value errors, then collect them
                        if (error.ValueKind == JsonValueKind.Object)
                        {
                            var messages = new List <string>();

                            json = ParsedJson.Parse(error.GetRawText());

                            foreach (var member in json.Members)
                            {
                                switch (member.Value.ValueKind)
                                {
                                // Add strings
                                case JsonValueKind.String:
                                    messages.Add($"{member.Key} {member.Value.GetString()}.");
                                    break;

                                // Flatten arrays of strings
                                case JsonValueKind.Array:
                                    foreach (var member_element in member.Value.EnumerateArray())
                                    {
                                        if (member_element.ValueKind == JsonValueKind.String)
                                        {
                                            messages.Add($"{member.Key} {member_element.GetString()}.");
                                        }
                                    }
                                    break;
                                }
                            }

                            if (messages.Count > 0)
                            {
                                return(new Exception(string.Join(Environment.NewLine, messages), e));
                            }
                        }
                    }
                }
                catch
                {
                    // If we failed to parse the response, just ignore it
                    // and fall back to the default message.
                }
            }

            return(e);
        }
Example #21
0
 internal EventVote(ParsedJson json)
 {
     Finished  = json.GetBool("finished");
     CreatedAt = json.GetDateTime("created_at", false).Value;
     UpdatedAt = json.GetDateTime("updated_at", false).Value;
 }