Пример #1
0
        public ApiDetailsDto GetDetails(string id)
        {
            RequestParamteres requestParams = InitRequest();

            requestParams.ImdbId = id;

            string omdbapiUrl   = ConfigurationManager.AppSettings["omdbapiUrl"];
            string responseData = InvokeRequest(omdbapiUrl, requestParams);

            return(JsonConvert.DeserializeObject <ApiDetailsDto>(responseData));
        }
Пример #2
0
        public ApiSearchRootDto Search(string title, int?page)
        {
            RequestParamteres requestParams = InitRequest();

            requestParams.Title = title;
            requestParams.Page  = page;

            string omdbapiUrl   = ConfigurationManager.AppSettings["omdbapiUrl"];
            string responseData = InvokeRequest(omdbapiUrl, requestParams);

            return(JsonConvert.DeserializeObject <ApiSearchRootDto>(responseData));
        }
Пример #3
0
        private string ComposeParameters(RequestParamteres requestDto)
        {
            var parameterDictionary = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(requestDto.ImdbId))
            {
                parameterDictionary.Add("i", requestDto.ImdbId);
            }
            if (!string.IsNullOrEmpty(requestDto.Title))
            {
                parameterDictionary.Add("s", requestDto.Title);
            }
            if (requestDto.Page.HasValue)
            {
                parameterDictionary.Add("page", requestDto.Page.Value.ToString());
            }
            parameterDictionary.Add("r", requestDto.ResponseType.ToString());

            return ConcatenateParameters(parameterDictionary);
        }
Пример #4
0
        private string ComposeParameters(RequestParamteres requestDto)
        {
            var parameterDictionary = new Dictionary <string, string>();

            if (!string.IsNullOrEmpty(requestDto.ImdbId))
            {
                parameterDictionary.Add("i", requestDto.ImdbId);
            }
            if (!string.IsNullOrEmpty(requestDto.Title))
            {
                parameterDictionary.Add("s", requestDto.Title);
            }
            if (requestDto.Page.HasValue)
            {
                parameterDictionary.Add("page", requestDto.Page.Value.ToString());
            }
            parameterDictionary.Add("r", requestDto.ResponseType.ToString());

            return(ConcatenateParameters(parameterDictionary));
        }
Пример #5
0
        private string InvokeRequest(string apiUrl, RequestParamteres requestDto)
        {
            string composeParameters = ComposeParameters(requestDto);
            string requestUrl = apiUrl + "?" + composeParameters;

            WebRequest request = WebRequest.Create(requestUrl);
            request.Credentials = CredentialCache.DefaultCredentials;

            WebResponse response = request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            if (dataStream != null)
            {
                var reader = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                reader.Close();
                response.Close();

                return responseFromServer;
            }
            return null;
        }
Пример #6
0
        private string InvokeRequest(string apiUrl, RequestParamteres requestDto)
        {
            string composeParameters = ComposeParameters(requestDto);
            string requestUrl        = apiUrl + "?" + composeParameters;

            WebRequest request = WebRequest.Create(requestUrl);

            request.Credentials = CredentialCache.DefaultCredentials;

            WebResponse response   = request.GetResponse();
            Stream      dataStream = response.GetResponseStream();

            if (dataStream != null)
            {
                var    reader             = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                reader.Close();
                response.Close();

                return(responseFromServer);
            }
            return(null);
        }