/// <summary> /// Gets objects of type CMObject by (key,__id__) and returns that Type /// </summary> /// <returns>The user objects.</returns> /// <param name="user">User with session to use in the request.</param> /// <param name="keys">Collection of key,__id__,ID</param> /// <param name="opts">Optional Request parameters for things like post execution snippet params.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public Task <CMObjectFetchResponse <T> > GetUserObjects <T>(CMUser user, List <string> keys, CMRequestOptions opts = null) where T : CMObject { return(GetUserObjects <T>(user, keys.ToArray(), opts)); }
// Get ============== /// <summary> /// Gets an object of type CMObject by (key,__id__) and returns that Type /// automatically parsed into the proper type in the Success field of the results. /// </summary> /// <returns>The user object.</returns> /// <param name="user">User with session to use in the request.</param> /// <param name="key">key,__id__,ID</param> /// <param name="opts">Optional Request parameters for things like post execution snippet params.</param> /// <typeparam name="T">a Type which derives from CMObject. Used to parse results</typeparam> public Task <CMObjectFetchResponse <T> > GetUserObject <T>(CMUser user, string key, CMRequestOptions opts = null) where T : CMObject { if (opts == null) { opts = new CMRequestOptions(user); } if (key != null) { opts.Query["keys"] = key; } return(APIService.Request <CMObjectFetchResponse <T> >(this.Application, "user/text", HttpMethod.Get, null, opts)); }
/// <summary> /// Request the specified app, action, method, content and options. /// </summary> /// <param name="app">An instance of application ID and API key.</param> /// <param name="action">URL action which the platform should execute ("user/binary/", "user/search")</param> /// <param name="method">HTTP method type which the action uses (PUT, POST, GET, DELETE)</param> /// <param name="content">Content stream to be sent in the body. Also can be passed through on options</param> /// <param name="options">CMRequestOptions which contains the necessary values for query parameters and headers.</param> /// <typeparam name="T">CMResponse type derivative which wraps the return shape in the task response.</typeparam> public async Task <T> Request <T>(CMApplication app, string action, HttpMethod method, Stream content, CMRequestOptions options) where T : CMResponse, new() { HttpClientHandler clientHandler = new HttpClientHandler() { AllowAutoRedirect = true }; using (HttpClient httpClient = new HttpClient(clientHandler)) { CMRequestOptions opts = options ?? new CMRequestOptions(); // Set various query options. List <string> query = GetCloudMineQuery(opts); Uri uri = GetCloudmineUri(options.BaseURL ?? "api.cloudmine.io", app.APIVersion, app.ApplicationID, action, query); httpClient.BaseAddress = uri; if (opts.Credentials != null) { if (!string.IsNullOrEmpty(opts.Credentials.Username)) { var authData = string.Format("{0}:{1}", opts.Credentials.Username, opts.Credentials.Password); var authHeaderValue = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(authData)); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue); } } // push needs a platform but in the default case lets give our metrics something to chew on. if (!httpClient.DefaultRequestHeaders.Contains("device_type")) { httpClient.DefaultRequestHeaders.Add("device_type", "csharp"); } httpClient.DefaultRequestHeaders.Add("X-CloudMine-ApiKey", app.APIKey); httpClient.DefaultRequestHeaders.Add("sdk_type", "csharp"); httpClient.DefaultRequestHeaders.Add("X-Unique-Id", Guid.NewGuid().ToString()); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(opts.ContentType)); foreach (string headerKey in opts.Headers.Keys) { httpClient.DefaultRequestHeaders.Add(headerKey, opts.Headers[headerKey]); } if (method == HttpMethod.Post) { // Login requests post no body if (content != null || opts.Data != null) { StreamContent contentData = content != null ? new StreamContent(content) : new StreamContent(opts.Data); StringContent stringContent = new StringContent(await contentData.ReadAsStringAsync(), System.Text.Encoding.UTF8, "application/json"); using (HttpResponseMessage responseMsg = await httpClient.PostAsync(uri, stringContent)) return(await GenerateCMResponseObject <T> (responseMsg)); } else { using (HttpResponseMessage responseMsg = await httpClient.PostAsync(uri, new StringContent(string.Empty))) return(await GenerateCMResponseObject <T> (responseMsg)); } } else if (method == HttpMethod.Put) { StreamContent contentData = content != null ? new StreamContent(content) : new StreamContent(opts.Data); StringContent stringContent = new StringContent(await contentData.ReadAsStringAsync(), System.Text.Encoding.UTF8, "application/json"); using (HttpResponseMessage responseMsg = await httpClient.PutAsync(uri, stringContent)) return(await GenerateCMResponseObject <T>(responseMsg)); } else if (method == HttpMethod.Get) { using (HttpResponseMessage responseMsg = await httpClient.GetAsync(uri)) return(await GenerateCMResponseObject <T>(responseMsg)); } else if (method == HttpMethod.Delete) { using (HttpResponseMessage responseMsg = await httpClient.DeleteAsync(uri)) return(await GenerateCMResponseObject <T>(responseMsg)); } else { throw new InvalidOperationException(string.Format("{0} is not a CloudMine API supported HttpMethod.", method.Method)); } } }
// Code snippet ========= public Task <CMResponse> Run(String snippet, Dictionary <string, string> parameters = null, CMRequestOptions opts = null) { if (opts == null) { opts = new CMRequestOptions(); } foreach (string id in parameters.Keys) { opts.Query[id] = parameters[id]; } foreach (string id in opts.SnippetParams.Keys) { opts.Query[id] = opts.SnippetParams[id]; } opts.Snippet = null; opts.SnippetParams.Clear(); return(APIService.Request(Application, "run/" + snippet, HttpMethod.Post, null, opts)); }
/// <summary> /// Request the specified app, action, method, content and options. /// </summary> /// <param name="app">An instance of application ID and API key.</param> /// <param name="action">URL action which the platform should execute ("user/binary/", "user/search")</param> /// <param name="method">HTTP method type which the action uses (PUT, POST, GET, DELETE)</param> /// <param name="content">Content stream to be sent in the body. Also can be passed through on options</param> /// <param name="options">CMRequestOptions which contains the necessary values for query parameters and headers.</param> public Task <CMResponse> Request(CMApplication app, string action, HttpMethod method, Stream content, CMRequestOptions options) { return(Request <CMResponse>(app, action, method, content, options)); }