private Uri SaveResource <T>(T participant, string resourceUri) where T : class { this.LastApiMessage = string.Empty; StatusMessage message; RestAdapter adapter = GetAdapter(); try { message = adapter.Post <T>(new Uri(this.apiUriBase + resourceUri), participant); } catch (Exception ex) { throw new ApiException(ex); } finally { ReleaseAdapter(adapter); } if (message.Code != (int)HttpStatusCode.Created) { throw new ApiException(new RestException(message)); } this.LastApiMessage = message.Message; return(new Uri(message.Link.HRef)); }
private void ReleaseAdapter(RestAdapter adapter) { lock (adapterLock) { adapter.IsAssigned = false; } }
private T GetResource <T>(string resourceUri) where T : class { RestAdapter adapter = GetAdapter(); try { return(adapter.Get <T>(new Uri(this.apiUriBase + resourceUri))); } catch (Exception ex) { throw new ApiException(ex); } finally { ReleaseAdapter(adapter); } }
private IList <TItem> GetResourceList <TContainer, TItem>(string resourceUri) where TContainer : class, IEnumerable <TItem> where TItem : class { RestAdapter adapter = GetAdapter(); try { TContainer items = adapter.Get <TContainer>(new Uri(this.apiUriBase + resourceUri)); return(new List <TItem>(items)); } catch (Exception ex) { throw new ApiException(ex); } finally { ReleaseAdapter(adapter); } }
private RestAdapter GetAdapter() { lock (adapterLock) { if (null == this.adapters) { throw new ObjectDisposedException(GetType().FullName); } RestAdapter adapter = this.adapters.FirstOrDefault(a => a.IsBusy == false && a.IsAssigned == false); if (adapter == null) { adapter = new RestAdapter(this.username, this.password); adapter.ContentType = this.ContentType; this.adapters.Add(adapter); } adapter.IsAssigned = true; return(adapter); } }
private object Get(string resourceUri) { if (null == resourceUri) { throw new ArgumentNullException("resourceUri"); } RestAdapter adapter = GetAdapter(); try { return(adapter.Get(new Uri(this.apiUriBase + resourceUri))); } catch (Exception ex) { throw new ApiException(ex); } finally { ReleaseAdapter(adapter); } }
/// <summary> /// Gets a resource where the expected type is not know. /// </summary> /// <param name="resourceUri">The resourceUri</param> /// <exception cref="ApiException">The request failed. See inner exception for details.</exception> /// <returns>The resource</returns> public object Get(Uri resourceUri) { if (null == resourceUri) { throw new ArgumentNullException("resourceUri"); } RestAdapter adapter = GetAdapter(); try { return(adapter.Get(resourceUri)); } catch (Exception ex) { throw new ApiException(ex); } finally { ReleaseAdapter(adapter); } }
private bool DeleteResource(string resourceUri) { this.LastApiMessage = string.Empty; StatusMessage message; RestAdapter adapter = GetAdapter(); try { message = adapter.Delete(new Uri(this.apiUriBase + resourceUri)); } catch (Exception ex) { throw new ApiException(ex); } finally { ReleaseAdapter(adapter); } this.LastApiMessage = message.Message; return(message.Code == (int)HttpStatusCode.OK); }
private bool UpdateResource <T>(T competition, string resourceUri) where T : class { this.LastApiMessage = string.Empty; StatusMessage message; RestAdapter adapter = GetAdapter(); try { message = adapter.Put <T>(new Uri(this.apiUriBase + resourceUri), competition); } catch (Exception ex) { throw new ApiException(ex); } finally { ReleaseAdapter(adapter); } this.LastApiMessage = message.Message; return(message.Code == (int)HttpStatusCode.OK); }