// Create compiler directives to exclude all but this in NET4 version // public oEmbedResponse Read(string api, string url, int? maxwidth=null, int? maxheight=null, oEmbedFormat format=oEmbedFormat.Unspecified, // NameValueCollection queryParameters=null, string userAgent=null) // { // return // Read(new oEmbedRequest(api, url, maxwidth, maxheight, format, // queryParameters ?? new NameValueCollection(), userAgent)); // } public oEmbedResponse Read(oEmbedRequest request) { var response = new oEmbedResponse(); if (!string.IsNullOrEmpty(request.Api) && !string.IsNullOrEmpty(request.Url)) { response = GetQueryResult(request); if (response.oEmbed != null) { response.oEmbed.Url = response.oEmbed.Url ?? request.Url; } else { response.StatusCode = HttpStatusCode.NotFound; } } else { response.StatusCode = HttpStatusCode.BadRequest; response.StatusDescription = "Resource Api or Url is null or empty"; } return response; }
private oEmbedResponse GetQueryResult(oEmbedRequest request) { if (request.MaxWidth.HasValue && request.QueryParameters.AllKeys.All(x => x != "maxwidth")) request.QueryParameters.Add("maxwidth", request.MaxWidth.Value.ToString(CultureInfo.InvariantCulture)); if (request.MaxHeight.HasValue && request.QueryParameters.AllKeys.All(x => x != "maxheight")) request.QueryParameters.Add("maxheight", request.MaxHeight.Value.ToString(CultureInfo.InvariantCulture)); if (request.Format != oEmbedFormat.Unspecified && request.QueryParameters.AllKeys.All(x => x != "format")) if (request.Format != oEmbedFormat.Unspecified) request.QueryParameters.Add("format", request.Format.ToString().ToLower()); var query = new StringBuilder(request.Api + "?url=" + HttpUtility.UrlEncode(request.Url)); for (var i = 0; i < request.QueryParameters.Count; i++) { query.Append("&" + request.QueryParameters.GetKey(i) + "=" + request.QueryParameters.Get(i)); } return GetOEmbedResponseFromWeb(request, query.ToString()); }
private oEmbedResponse GetOEmbedResponseFromWeb(oEmbedRequest request, string query) { var readResponse = new oEmbedResponse(); var webrequest = (HttpWebRequest) WebRequest.Create(query); if (request.UserAgent != null) webrequest.UserAgent = request.UserAgent; if (request.Format == oEmbedFormat.Json) webrequest.Accept = "application/json"; if (request.Format == oEmbedFormat.Xml) webrequest.Accept = "text/xml"; using (var response = webrequest.GetResponse() as HttpWebResponse) { if (response != null) { readResponse.StatusCode = response.StatusCode; readResponse.StatusDescription = response.StatusDescription; readResponse.RawResult = ReadResponse(response.GetResponseStream()); if (response.ContentType.ToLower().Contains("xml")) readResponse.Format = oEmbedFormat.Xml; if (response.ContentType.ToLower().Contains("json")) readResponse.Format = oEmbedFormat.Json; if (response.StatusCode == HttpStatusCode.OK) { readResponse.oEmbed = oEmbedSerializer.Deserialize(readResponse); ValidateResponse(readResponse, request.MaxWidth ?? int.MaxValue, request.MaxHeight ?? int.MaxValue, request.Format); } response.Close(); } } return readResponse; }