Skip to content

Instantly share code, notes, and snippets.

@scionwest
Created December 6, 2014 20:37
Show Gist options
  • Save scionwest/ca4645d7ebff15dec443 to your computer and use it in GitHub Desktop.
Save scionwest/ca4645d7ebff15dec443 to your computer and use it in GitHub Desktop.
ParseRestHelper
//-----------------------------------------------------------------------
// <copyright file="ParseRestHelper.cs" company="Sully Studios">
// Copyright (c) Johnathon Sullinger. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Lifestream.Data.Repository.ParseCloud
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
/// <summary>
/// Provides helper methods for interacting with the Parse Rest API
/// </summary>
public static class ParseRestHelper
{
/// <summary>
/// The parse application header
/// </summary>
private static string parseAppHeader = "X-Parse-Application-Id";
/// <summary>
/// The parse rest header
/// </summary>
private static string parseRestHeader = "X-Parse-REST-API-Key";
/// <summary>
/// The parse application identifier
/// </summary>
private static string parseAppId = "SomeKey";
/// <summary>
/// The parse rest key
/// </summary>
private static string parseRestKey = "SomeOtherKey";
/// <summary>
/// Creates the HTTP client.
/// Adds the parse App Id and REST API keys by default, along with setting the content type to application/json
/// </summary>
/// <returns>Returns an HttpClient with the headers set to the AppId and Rest API key.</returns>
public static HttpClient CreateHttpClient()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add(parseAppHeader, parseAppId);
httpClient.DefaultRequestHeaders.Add(parseRestHeader, parseRestKey);
httpClient.DefaultRequestHeaders.Add("ContentType", "application/json");
return httpClient;
}
/// <summary>
/// Creates the HTTP client.
/// Adds the parse App Id and REST API keys by default, along with setting the content type to application/json
/// </summary>
/// <param name="additionalHeaderData">Any additional header data.</param>
/// <returns>
/// Returns an HttpClient with the headers set to the AppId and Rest API key.
/// </returns>
public static HttpClient CreateHttpClient(params KeyValuePair<string, string>[] additionalHeaderData)
{
var httpClient = ParseRestHelper.CreateHttpClient();
if (additionalHeaderData != null)
{
foreach (KeyValuePair<string, string> keyValue in additionalHeaderData)
{
httpClient.DefaultRequestHeaders.Add(keyValue.Key, keyValue.Value);
}
}
return httpClient;
}
public static async Task<HttpResponseMessage> PostAsync(HttpClient client, string path, string json)
{
// TODO: Change Content-Type: application/json to an enum value to support images and other content types.
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
string url = string.Format("https://api.parse.com/1/{0}", path);
// Post the content to the server.
return await client.PostAsync(new Uri(url), content);
}
public static async Task<HttpResponseMessage> GetAsync(HttpClient client, string path)
{
string url = string.Format("https://api.parse.com/1/{0}", path);
return await client.GetAsync(new Uri(url));
}
public static async Task<HttpResponseMessage> GetAsync(HttpClient client, string path, params KeyValuePair<string, string>[] parameters)
{
string parameter = string.Join("&", parameters.Select(p => string.Format("{0}={1}", p.Key, p.Value)));
string url = string.Format("https://api.parse.com/1/{0}?{1}", path, parameter);
return await client.GetAsync(new Uri(url));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment