Exemple #1
0
        public FooResponse CreateBlob(string mimeType)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://foocdn.azurewebsites.net/api/content/add/");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            var cdnKey = ConfigurationManager.AppSettings["fooCdnKey"];
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = String.Format("{{ \"AccountKey\": \"{0}\", \"MimeType\":\"" + mimeType + "\"}}", cdnKey);
                streamWriter.Write(json);
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            string createdBlob = null;

            if (httpResponse.StatusCode == HttpStatusCode.OK)
            {
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    createdBlob = streamReader.ReadToEnd();

                    //get rid of the quotes surrounding it
                    createdBlob = createdBlob.Substring(1, createdBlob.Length - 2);
                }
            }

            FooResponse fooResult = new FooResponse()
            {
                Value = createdBlob,
                StatusCode = httpResponse.StatusCode
            };

            return fooResult;
        }
Exemple #2
0
        public FooResponse Get(string blobID, string mediaType)
        {
            using (var fooCDN = new HttpClient())
            {
                fooCDN.BaseAddress = _baseUri;
                fooCDN.DefaultRequestHeaders.Accept.Clear();
                fooCDN.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType));

                HttpResponseMessage response = fooCDN.GetAsync("/api/content/" + blobID).Result;

                byte[] bytes = null;
                if(response.IsSuccessStatusCode)
                {
                    bytes = response.Content.ReadAsByteArrayAsync().Result;
                }

                FooResponse fooResult = new FooResponse()
                {
                    Value = bytes,
                    StatusCode = response.StatusCode
                };

                return fooResult;
            }
        }
Exemple #3
0
        public FooResponse Delete(string blobID)
        {
            using (var fooCDN = new HttpClient())
            {
                fooCDN.BaseAddress = _baseUri;
                fooCDN.DefaultRequestHeaders.Accept.Clear();

                HttpResponseMessage response = fooCDN.DeleteAsync("/api/content/" + blobID).Result;

                FooResponse fooResult = new FooResponse()
                {
                    StatusCode = response.StatusCode
                };

                return fooResult;
            }
        }
Exemple #4
0
        public FooResponse Put(string blobID, StorageType type)
        {
            using (var fooCDN = new HttpClient())
            {
                fooCDN.BaseAddress = _baseUri;
                fooCDN.DefaultRequestHeaders.Accept.Clear();

                HttpContent empty = new StringContent("");
                empty.Headers.Add("Content-Length", "0");

                HttpResponseMessage response = fooCDN.PutAsync("/api/content/" + blobID + "?type=" + type.ToString(), empty).Result;

                FooResponse fooResult = new FooResponse()
                {
                    StatusCode = response.StatusCode
                };
                var x = response.IsSuccessStatusCode;
                return fooResult;
            }
        }
Exemple #5
0
        public FooResponse Post(string blobID, byte[] data)
        {
            using (var fooCDN = new HttpClient())
            {
                fooCDN.BaseAddress = _baseUri;
                fooCDN.DefaultRequestHeaders.Accept.Clear();

                MultipartFormDataContent content = new MultipartFormDataContent();
                HttpContent internalContent = new ByteArrayContent(data);
                content.Add(internalContent);

                HttpResponseMessage response = fooCDN.PostAsync("/api/content/" + blobID, content).Result;

                var result = response.Content.ReadAsByteArrayAsync().Result;

                FooResponse fooResult = new FooResponse()
                {
                    Value = result,
                    StatusCode = response.StatusCode
                };

                return fooResult;
            }
        }
Exemple #6
0
        public FooResponse GetInfo(string blobID)
        {
            using (var fooCDN = new HttpClient())
            {
                fooCDN.BaseAddress = _baseUri;
                fooCDN.DefaultRequestHeaders.Accept.Clear();
                fooCDN.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = fooCDN.GetAsync("/api/content/" + blobID + "/info").Result;

                var result = response.Content.ReadAsAsync<Object>().Result;

                //Into a dictionary for now
                Dictionary<string, dynamic> dictionaryResult = null;

                if(response.IsSuccessStatusCode)
                {
                    dictionaryResult = _serializer.Deserialize<Dictionary<string, dynamic>>(result.ToString());
                }

                FooResponse fooResult = new FooResponse()
                {
                    Value = dictionaryResult,
                    StatusCode = response.StatusCode
                };

                return fooResult;
            }
        }