예제 #1
0
파일: XeroFactory.cs 프로젝트: ttcp112/FJ
        public static async Task <bool> PushIngredientsToXero(XeroIngredientModel item)
        {
            bool result = false;

            try
            {
                using (var client = new HttpClient())
                {
                    string idOld = item.Id;
                    item.Id            = null;
                    client.BaseAddress = new Uri(Commons.XeroURL);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    try
                    {
                        //HttpResponseMessage
                        HttpResponseMessage response = await client.PostAsJsonAsync(Commons.XeroApi_InsertInventory, item).ConfigureAwait(false);

                        if (response.IsSuccessStatusCode)//sucess
                        {
                            ResponseXeroModelBase content = await response.Content.ReadAsAsync <ResponseXeroModelBase>();

                            if (content != null)
                            {
                                if (content.Data != null)
                                {
                                    _ingredientFactory.UpdateIngredient(idOld, content.Data.Id);
                                    NSLog.Logger.Info(string.Format("Save ingredient [{0}] success!", item.Code));
                                    result = true;
                                }
                            }
                        }
                        else
                        {
                            //log
                            _baseFactory.InsertIngredientTrackLog(item.Code);
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e);
                    }
                    return(result);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
            return(result);
        }