public ApiResponse()
 {
     Failure = new ApiFailure();
     Success = new ApiSuccess <TData>();
 }
예제 #2
0
        /// <summary>
        /// api task
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task <string> SendData(QueryBuilder query)
        {
            ApiStart?.Invoke();
            if (this.IsBusy)
            {
                ApiFailure?.Invoke(102);
                return("");
            }
            this.IsBusy = true;
            var url = AppRepository.GetInstance().TranslationApi + query.ToString();

            try {
                using (var client = new HttpClient()) {
                    var response = await client.GetAsync(url);

                    var body = await response.Content.ReadAsStringAsync(); // これが非同期だから、このメソッドを非同期にする意味ないかも。。。

                    var status = (int)response.StatusCode;
                    if (200 == status & !body.StartsWith("<!DOC"))
                    {
                        var json = JsonParser.Create(body);
                        if (ApiMode.List == this._apiMode)
                        {
                            this._list = json.GetList();
                            GetListApiSuccess(json.GetList());
                        }
                        else
                        {
                            status = int.Parse(json.GetValue(JsonKey.Status));
                            if (200 == status)
                            {
                                if (ApiMode.Search == this._apiMode)
                                {
                                    TranslateApiSuccess?.Invoke(json.GetValue(JsonKey.TranslatedText));
                                }
                                else
                                {
                                    SaveApiSuccess?.Invoke();
                                }
                            }
                            else
                            {
                                ApiFailure?.Invoke(status);
                            }
                        }
                    }
                    else
                    {
                        ApiFailure?.Invoke(status);
                    }
                    this.IsBusy = false;
                    ApiStop();
                    return("");
                }
            } catch {
                ApiFailure?.Invoke(500);
                this.IsBusy = false;
                ApiStop();
                return("");
            }
        }