Beispiel #1
0
        public static SimpleNote GetNoteData(ISimpleJsonRest web, string noteID, SimpleNoteConfig cfg, SimpleNoteConnection conn, int?version = null)
        {
            try
            {
                if (version != null)
                {
                    return(GetNoteFromQuery(web.Get <APIResultNoteData>(string.Format("note/i/{0}/v/{1}", noteID, version)), web, noteID, cfg, conn));
                }
                else
                {
                    return(GetNoteFromQuery(web.Get <APIResultNoteData>("note/i/" + noteID), web, noteID, cfg, conn));
                }
            }
            catch (RestStatuscodeException e1)
            {
                if (e1.StatusCode == 400 && !string.IsNullOrWhiteSpace(e1.HTTPContent))
                {
                    var req = web.ParseJsonOrNull <APIBadRequest>(e1.HTTPContent);
                    if (req != null)
                    {
                        throw new SimpleNoteAPIException($"Server returned status 400.\nField: '{req.field}'.\nMessage: '{req.message}'", e1);
                    }
                }

                throw;
            }
        }
Beispiel #2
0
        public static SimpleNote ChangeExistingNote(ISimpleJsonRest web, SimpleNote note, SimpleNoteConfig cfg, SimpleNoteConnection conn, out bool updated)
        {
            if (note.Deleted)
            {
                throw new SimpleNoteAPIException("Cannot update an already deleted note");
            }
            if (note.ID == "")
            {
                throw new SimpleNoteAPIException("Cannot change a not uploaded note");
            }
            note.ModificationDate = DateTimeOffset.Now;

            APISendNoteData data = new APISendNoteData
            {
                tags             = note.Tags.ToList(),
                content          = note.Content,
                modificationDate = ConvertToEpochDate(note.ModificationDate),
                systemTags       = note.SystemTags.ToList(),
            };

            try
            {
                var r = web.PostTwoWay <APIResultNoteData>(data, "note/i/" + note.ID, new[] { 412 }, "response=1");

                if (r == null)
                {
                    // Statuscode 412 - Empty change

                    updated = false;
                    return((SimpleNote)note.Clone());
                }

                updated = true;
                return(GetNoteFromQuery(r, web, note.ID, cfg, conn));
            }
            catch (RestStatuscodeException e1)
            {
                if (e1.StatusCode == 400 && !string.IsNullOrWhiteSpace(e1.HTTPContent))
                {
                    var req = web.ParseJsonOrNull <APIBadRequest>(e1.HTTPContent);
                    if (req != null)
                    {
                        throw new SimpleNoteAPIException($"Server returned status 400.\nField: '{req.field}'.\nMessage: '{req.message}'", e1);
                    }
                }

                throw;
            }
        }
Beispiel #3
0
        private static SimpleNote GetNoteFromQuery(APIResultNoteData r, ISimpleJsonRest c, string id, SimpleNoteConfig cfg, SimpleNoteConnection conn)
        {
            try
            {
                var n = new SimpleNote(id, cfg, conn.HConfig);
                using (n.SuppressDirtyChanges())
                {
                    n.Deleted          = r.deleted;
                    n.ShareURL         = r.shareURL;
                    n.PublicURL        = r.publishURL;
                    n.SystemTags       = r.systemTags;
                    n.Content          = r.content;
                    n.ModificationDate = ConvertFromEpochDate(r.modificationDate);
                    n.CreationDate     = ConvertFromEpochDate(r.creationDate);
                    n.LocalVersion     = int.Parse(c.GetResponseHeader("X-Simperium-Version"));
                    n.Tags.Synchronize(r.tags);
                };

                return(n);
            }
            catch (RestException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new SimpleNoteAPIException("SimpleNote API returned unexpected note data", e);
            }
        }
Beispiel #4
0
        public static SimpleNote UploadNewNote(ISimpleJsonRest web, SimpleNote note, SimpleNoteConfig cfg, SimpleNoteConnection conn)
        {
            note.Deleted          = false;
            note.CreationDate     = DateTimeOffset.Now;
            note.ModificationDate = DateTimeOffset.Now;

            APIResultNoteData data = new APIResultNoteData
            {
                tags             = note.Tags.ToList(),
                deleted          = false,
                shareURL         = note.ShareURL,
                publishURL       = note.PublicURL,
                systemTags       = note.SystemTags,
                content          = note.Content,
                creationDate     = ConvertToEpochDate(note.CreationDate),
                modificationDate = ConvertToEpochDate(note.ModificationDate),
            };

            try
            {
                var r = web.PostTwoWay <APIResultNoteData>(data, "note/i/" + note.ID, "response=1");

                return(GetNoteFromQuery(r, web, note.ID, cfg, conn));
            }
            catch (RestStatuscodeException e1)
            {
                if (e1.StatusCode == 400 && !string.IsNullOrWhiteSpace(e1.HTTPContent))
                {
                    var req = web.ParseJsonOrNull <APIBadRequest>(e1.HTTPContent);
                    if (req != null)
                    {
                        throw new SimpleNoteAPIException($"Server returned status 400.\nField: '{req.field}'.\nMessage: '{req.message}'", e1);
                    }
                }

                throw;
            }
        }