예제 #1
0
        private async void Run(string item)
        {
            try
            {
                Logger.Log(LogLevel.Error, "Unable to login.\n\n");
                var settings = (KeepCommandSettings)Settings;

                var googleClient = new GPSOAuthClient(settings.GoogleUserName, settings.GooglePassword);
                if (string.IsNullOrEmpty(_token))
                {
                    _token = await GetMasterToken(googleClient);
                }

                Dictionary <string, string> oauthResponse = await GetOauthResponse(googleClient).ConfigureAwait(false);

                if (!oauthResponse.ContainsKey("Auth"))
                {
                    _token = await GetMasterToken(googleClient);
                }
                oauthResponse = await GetOauthResponse(googleClient).ConfigureAwait(false);

                if (!oauthResponse.ContainsKey("Auth"))
                {
                    throw new Exception("Auth token was missing from oauth login response.");
                }

                var client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", oauthResponse["Auth"]);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
                client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
                client.DefaultRequestHeaders.Connection.Add("keep-alive");
                KeepRequest         keepRequest = _keepRequestFactoryFactory.Create();
                HttpContent         content     = GetRequest(keepRequest.ToJson());
                HttpResponseMessage response    = await client.PostAsync("https://www.googleapis.com/notes/v1/changes", content).ConfigureAwait(false);

                string json = await GetJson(response).ConfigureAwait(false);

                KeepResponse keepResponse = Serialize.FromJson(json);
                Node[]       newNodes     = new Node[2];
                string       parentNodeId = settings.ListId;
                newNodes[0] = keepResponse.Nodes.Single(n => n.Id == parentNodeId);
                newNodes[1] = new Node
                {
                    Text           = item,
                    ParentId       = parentNodeId,
                    ParentServerId = newNodes[0].ServerId,

                    Type       = "LIST_ITEM",
                    Timestamps = new Timestamps
                    {
                        Created = DateTimeOffset.Now,
                        Updated = DateTimeOffset.Now
                    },
                    Id = new RandomHelper().GetNodeId()
                };
                keepRequest = _keepRequestFactoryFactory.Create(newNodes, keepResponse.ToVersion);
                content     = GetRequest(keepRequest.ToJson());
                response    = await client.PostAsync("https://www.googleapis.com/notes/v1/changes", content).ConfigureAwait(false);

                json = GetJson(response).ConfigureAwait(false).GetAwaiter().GetResult();
                Logger.Log(LogLevel.Info, json);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Fatal, ex);
            }
        }
예제 #2
0
 public static string ToJson(this KeepRequest self) => JsonConvert.SerializeObject(self, Converter.Settings);