/// <summary> /// With this API, you can check if a user has write/delete permission on a specific Band. /// Depending on the Band settings, users may not have a permission to write a post or a comment even though they are members of the Band. /// Please call this API first to check user permission before trying to write/delete something. /// </summary> /// <returns>List of <see cref="string"/></returns> public async Task <List <string> > CheckPermissions(string bandKey, string permissions) { var apiCall = BandClientHelperExtensions.ToFullUrl($"/band/permissions?access_token={_accessToken.Token}&band_key={bandKey}&permissions={permissions}"); HttpResponseMessage response = await HttpClient.GetAsync(apiCall); await HandleResponse(response); string responseBody = await response.Content.ReadAsStringAsync(); var serializer = new JsonDotNetSerializer(); // TODO do something to check BAND's result codes... var bandPermissions = serializer.Deserialize <PermissionsResult>(responseBody).ResultData.Permissions; return(bandPermissions); }
/// <summary> /// This API lets you get photos in a specific album. /// </summary> /// <returns>List of <see cref="Photo"/></returns> public async Task <Photos> GetPhotos(string bandKey, string photoAlbumKey) { var apiCall = BandClientHelperExtensions.ToFullUrl($"/band/album/photos?access_token={_accessToken.Token}&band_key={bandKey}&photo_album_key={photoAlbumKey}"); HttpResponseMessage response = await HttpClient.GetAsync(apiCall); await HandleResponse(response); string responseBody = await response.Content.ReadAsStringAsync(); var serializer = new JsonDotNetSerializer(); // TODO do something to check BAND's result codes... var photos = serializer.Deserialize <PhotosResult>(responseBody).ResultData; return(photos); }
/// <summary> /// This API lets you get all Bands that a user has joined. /// </summary> /// <returns>List of <see cref="Band"/></returns> public async Task <List <Band> > GetBands() { var apiCall = BandClientHelperExtensions.ToFullUrl($"/bands?access_token={_accessToken.Token}"); HttpResponseMessage response = await HttpClient.GetAsync(apiCall); await HandleResponse(response); string responseBody = await response.Content.ReadAsStringAsync(); var serializer = new JsonDotNetSerializer(); // TODO do something to check BAND's result codes... var bands = serializer.Deserialize <BandsResult>(responseBody).ResultData.BandsList; return(bands); }
/// <summary> /// This API lets you get posts of a specific Band. The list is sorted by last modified date. /// </summary> /// <returns>List of <see cref="Post"/></returns> public async Task <Posts> GetPosts(string bandKey, string locale = "en_US") { var apiCall = BandClientHelperExtensions.ToFullUrl($"/band/posts?access_token={_accessToken.Token}&locale={locale}&band_key={bandKey}"); HttpResponseMessage response = await HttpClient.GetAsync(apiCall); await HandleResponse(response); string responseBody = await response.Content.ReadAsStringAsync(); var serializer = new JsonDotNetSerializer(); // TODO do something to check BAND's result codes... var posts = serializer.Deserialize <PostsResult>(responseBody).ResultData; return(posts); }
/// <summary> /// General error checking of the response before specific processing is done. /// </summary> /// <param name="response"></param> private async Task HandleResponse(HttpResponseMessage response) { if (!response.IsSuccessStatusCode) { // assumption is error response from fitbit in the 4xx range var errors = new JsonDotNetSerializer().ParseErrors(await response.Content.ReadAsStringAsync()); // rate limit hit //if (429 == (int)response.StatusCode) //{ // // not sure if we can use 'RetryConditionHeaderValue' directly as documentation is minimal for the header // var retryAfterHeader = response.Headers.GetValues("Retry-After").FirstOrDefault(); // if (retryAfterHeader != null) // { // int retryAfter; // if (int.TryParse(retryAfterHeader, out retryAfter)) // { // throw new FitbitRateLimitException(retryAfter, errors); // } // } //} // request exception parsing switch (response.StatusCode) { case HttpStatusCode.BadRequest: case HttpStatusCode.Unauthorized: case HttpStatusCode.Forbidden: case HttpStatusCode.NotFound: throw new BandRequestException(response, errors); } // if we've got here then something unexpected has occured throw new BandException($"An error has occured. Please see error list for details - {response.StatusCode}", errors); } }