/// <summary>
        /// Makes the API request
        /// </summary>
        /// <param name="method">The method to call.</param>
        /// <param name="appId">The app id.</param>
        /// <param name="appCode">The app code.</param>
        /// <param name="countryCode">The country code.</param>
        /// <param name="pathParams">The path params.</param>
        /// <param name="querystringParams">The querystring params.</param>
        /// <param name="callback">The callback to hit when done.</param>
        public void SendRequestAsync(ApiMethod method, string appId, string appCode, string countryCode, Dictionary<string, string> pathParams, Dictionary<string, string> querystringParams, Action<Response<JObject>> callback)
        {
            // Check for bad api cred case...
            if (string.Compare(appId, "badkey", StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                JObject nullJson = null;
                callback(new Response<JObject>(HttpStatusCode.Forbidden, nullJson));
                return;
            }

            switch (method)
            {
                case ApiMethod.CountryLookup:
                    this.FakeCountryLookupResponse(querystringParams["countrycode"], callback);
                    break;

                case ApiMethod.Search:
                    this.FakeSearchResponse(querystringParams, callback);
                    break;

                default:
                    JObject json = null;
                    callback(new Response<JObject>(HttpStatusCode.InternalServerError, json));
                    break;
            }
        }
        /// <summary>
        /// Validates and adds country code if required
        /// </summary>
        /// <param name="url">The url being built</param>
        /// <param name="method">The method to call.</param>
        /// <param name="countryCode">The country code.</param>
        private static void AddCountryCode(StringBuilder url, ApiMethod method, string countryCode)
        {
            if (method.RequiresCountryCode)
            {
                if (string.IsNullOrEmpty(countryCode))
                {
                    throw new CountryCodeRequiredException();
                }

                url.AppendFormat("{0}/", countryCode);
            }
        }
        /// <summary>
        /// Builds an API URI
        /// </summary>
        /// <param name="method">The method to call.</param>
        /// <param name="appId">The app id.</param>
        /// <param name="appCode">The app code.</param>
        /// <param name="countryCode">The country code.</param>
        /// <param name="pathParams">The path parameters.</param>
        /// <param name="querystringParams">The querystring parameters.</param>
        /// <returns>
        /// A Uri to call
        /// </returns>
        public Uri BuildUri(ApiMethod method, string appId, string appCode, string countryCode, Dictionary<string, string> pathParams, Dictionary<string, string> querystringParams)
        {
            DirectoryInfo jsonDir = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, @"..\..\json"));
            if (jsonDir.Exists)
            {
                FileInfo[] json = jsonDir.GetFiles(this._filename);
                if (json.Length > 0)
                {
                    return new Uri("file://" + json[0].FullName.Replace(@"\", @"/"));
                }
            }

            throw new FileNotFoundException("Could not find required test file in " + jsonDir.FullName);
        }
        /// <summary>
        /// Builds an API URI
        /// </summary>
        /// <param name="method">The method to call.</param>
        /// <param name="settings">The music client settings.</param>
        /// <param name="pathParams">The path parameters.</param>
        /// <param name="querystringParams">The querystring parameters.</param>
        /// <returns>
        /// A Uri to call
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown when an unknown method is used</exception>
        /// <exception cref="CountryCodeRequiredException">Thrown when a CountryCode is required but not supplied</exception>
        /// <exception cref="ApiCredentialsRequiredException">Thrown when an API Key has not been supplied</exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        public Uri BuildUri(ApiMethod method, IMusicClientSettings settings, Dictionary<string, string> pathParams, Dictionary<string, string> querystringParams)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            // Build API url
            StringBuilder url = new StringBuilder();

            url.Append(method.BaseApiUri);
            AddCountryCode(url, method, settings.CountryCode);
            method.AppendUriPath(url, pathParams);
            this.AppendQueryString(url, settings, querystringParams);

            return new Uri(url.ToString());
        }
        /// <summary>
        /// Makes the API request
        /// </summary>
        /// <param name="method">The method to call.</param>
        /// <param name="appId">The app id.</param>
        /// <param name="appCode">The app code.</param>
        /// <param name="countryCode">The country code.</param>
        /// <param name="pathParams">The path params.</param>
        /// <param name="querystringParams">The querystring params.</param>
        /// <param name="callback">The callback to hit when done.</param>
        public void SendRequestAsync(ApiMethod method, string appId, string appCode, string countryCode, Dictionary<string, string> pathParams, Dictionary<string, string> querystringParams, Action<Response<JObject>> callback)
        {
            switch (method)
            {
                case ApiMethod.CountryLookup:
                    this.FakeResponse(Resources.country, callback);
                    break;

                case ApiMethod.Search:
                    this.FakeSearchResponse(querystringParams, callback);
                    break;

                case ApiMethod.SimilarArtists:
                    this.FakeResponse(Resources.artist_similar, callback);
                    break;

                case ApiMethod.ArtistProducts:
                case ApiMethod.ProductChart:
                case ApiMethod.ProductNewReleases:
                case ApiMethod.Recommendations:
                    this.FakeResponse(Resources.artist_products, callback);
                    break;

                case ApiMethod.Genres:
                    this.FakeResponse(Resources.genres, callback);
                    break;

                case ApiMethod.MixGroups:
                    this.FakeResponse(Resources.mixgroups, callback);
                    break;

                case ApiMethod.Mixes:
                    this.FakeResponse(Resources.mixes, callback);
                    break;
            }
        }
 /// <summary>
 /// Builds an API URI
 /// </summary>
 /// <param name="method">The method to call.</param>
 /// <param name="settings">The music client settings.</param>
 /// <param name="pathParams">The path parameters.</param>
 /// <param name="querystringParams">The querystring parameters.</param>
 /// <returns>
 /// A Uri to call
 /// </returns>
 public Uri BuildUri(ApiMethod method, IMusicClientSettings settings, Dictionary<string, string> pathParams, Dictionary<string, string> querystringParams)
 {
     return this._uri;
 }
        /// <summary>
        /// Makes the API request
        /// </summary>
        /// <param name="method">The method to call.</param>
        /// <param name="settings">The music client settings.</param>
        /// <param name="pathParams">The path params.</param>
        /// <param name="querystringParams">The querystring params.</param>
        /// <param name="callback">The callback to hit when done.</param>
        /// <param name="requestHeaders">HTTP headers to add to the request</param>
        /// <exception cref="System.ArgumentNullException">Thrown when no callback is specified</exception>
        public void SendRequestAsync(
                                     ApiMethod method,
                                     IMusicClientSettings settings,
                                     Dictionary<string, string> pathParams,
                                     Dictionary<string, string> querystringParams,
                                     Action<Response<JObject>> callback,
                                     Dictionary<string, string> requestHeaders = null)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            Uri uri = this.UriBuilder.BuildUri(method, settings, pathParams, querystringParams);

            Debug.WriteLine("Calling " + uri.ToString());

            WebRequest request = WebRequest.Create(uri);
            this.AddRequestHeaders(request, requestHeaders);
            request.BeginGetResponse(
                (IAsyncResult ar) =>
                {
                    WebResponse response = null;
                    HttpWebResponse webResponse = null;
                    JObject json = null;
                    HttpStatusCode? statusCode = null;
                    Exception error = null;

                    try
                    {
                        response = request.EndGetResponse(ar);
                        webResponse = response as HttpWebResponse;
                        if (webResponse != null)
                        {
                            statusCode = webResponse.StatusCode;
                        }
                    }
                    catch (WebException ex)
                    {
                        error = ex;
                        if (ex.Response != null)
                        {
                            webResponse = (HttpWebResponse)ex.Response;
                            statusCode = webResponse.StatusCode;
                        }
                    }

                    string contentType = null;
                    string result = null;

                    if (response != null)
                    {
                        contentType = response.ContentType;
                        using (Stream responseStream = response.GetResponseStream())
                        {
                            result = responseStream.AsString();
                            if (!string.IsNullOrEmpty(result))
                            {
                                try
                                {
                                    json = JObject.Parse(result);
                                }
                                catch (Exception ex)
                                {
                                    error = ex;
                                    json = null;
                                }
                            }
                        }
                    }

                    if (json != null)
                    {
                        callback(new Response<JObject>(statusCode, contentType, json, method.RequestId));
                    }
                    else
                    {
                        callback(new Response<JObject>(statusCode, error, method.RequestId));
                    }
                },
                request);
        }
 /// <summary>
 /// Builds an API URI
 /// </summary>
 /// <param name="method">The method to call.</param>
 /// <param name="appId">The app id.</param>
 /// <param name="appCode">The app code.</param>
 /// <param name="countryCode">The country code.</param>
 /// <param name="pathParams">The path parameters.</param>
 /// <param name="querystringParams">The querystring parameters.</param>
 /// <returns>
 /// A Uri to call
 /// </returns>
 public Uri BuildUri(ApiMethod method, string appId, string appCode, string countryCode, Dictionary<string, string> pathParams, Dictionary<string, string> querystringParams)
 {
     return this._uri;
 }
 /// <summary>
 /// Makes the API request
 /// </summary>
 /// <param name="method">The method to call.</param>
 /// <param name="settings">The app id.</param>
 /// <param name="pathParams">The path params.</param>
 /// <param name="querystringParams">The querystring params.</param>
 /// <param name="callback">The callback to hit when done.</param>
 /// <param name="requestHeaders">HTTP headers to add to the request</param>
 public void SendRequestAsync(
                              ApiMethod method,
                              IMusicClientSettings settings,
                              Dictionary<string, string> pathParams,
                              Dictionary<string, string> querystringParams,
                              Action<Response<JObject>> callback,
                              Dictionary<string, string> requestHeaders = null)
 {
     this._lastSettings = settings;
     this.NextFakeResponse.DoCallback(callback);
 }
Example #10
0
        /// <summary>
        /// Builds an API URI
        /// </summary>
        /// <param name="method">The method to call.</param>
        /// <param name="appId">The app id.</param>
        /// <param name="appCode">The app code.</param>
        /// <param name="countryCode">The country code.</param>
        /// <param name="pathParams">The path parameters.</param>
        /// <param name="querystringParams">The querystring parameters.</param>
        /// <returns>
        /// A Uri to call
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown when an unknown method is used</exception>
        /// <exception cref="CountryCodeRequiredException">Thrown when a CountryCode is required but not supplied</exception>
        /// <exception cref="ApiCredentialsRequiredException">Thrown when an API Key has not been supplied</exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        public Uri BuildUri(ApiMethod method, string appId, string appCode, string countryCode, Dictionary<string, string> pathParams, Dictionary<string, string> querystringParams)
        {
            // Validate Method and Country Code if it's required
            switch (method)
            {
                case ApiMethod.Unknown:
                    throw new ArgumentOutOfRangeException("method");

                case ApiMethod.CountryLookup:
                    // No country code required
                    break;

                default:
                    if (string.IsNullOrEmpty(countryCode))
                    {
                        throw new CountryCodeRequiredException();
                    }

                    break;
            }

            // Validate API Credentials...
            if (string.IsNullOrEmpty(appId) || string.IsNullOrEmpty(appCode))
            {
                throw new ApiCredentialsRequiredException();
            }

            // Build API url
            StringBuilder url = new StringBuilder();
            url.Append(@"http://api.ent.nokia.com/1.x/");

            switch (method)
            {
                case ApiMethod.CountryLookup:
                    // nothing extra needed
                    break;

                case ApiMethod.Search:
                    url.AppendFormat("{0}/", countryCode);
                    break;

                case ApiMethod.ArtistProducts:
                    if (pathParams != null && pathParams.ContainsKey("id"))
                    {
                        url.AppendFormat("{0}/creators/{1}/products/", countryCode, pathParams["id"]);
                    }
                    else
                    {
                        throw new ArgumentNullException("id");
                    }

                    break;

                case ApiMethod.SimilarArtists:
                    if (pathParams != null && pathParams.ContainsKey("id"))
                    {
                        url.AppendFormat("{0}/creators/{1}/similar/", countryCode, pathParams["id"]);
                    }
                    else
                    {
                        throw new ArgumentNullException("id");
                    }

                    break;

                case ApiMethod.Genres:
                    url.AppendFormat("{0}/genres/", countryCode);
                    break;

                case ApiMethod.MixGroups:
                    url.AppendFormat("{0}/mixes/groups/", countryCode);
                    break;

                case ApiMethod.Mixes:
                    if (pathParams != null && pathParams.ContainsKey("id"))
                    {
                        url.AppendFormat("{0}/mixes/groups/{1}/", countryCode, pathParams["id"]);
                    }
                    else
                    {
                        throw new ArgumentNullException();
                    }

                    break;

                case ApiMethod.ProductChart:
                    if (pathParams != null && pathParams.ContainsKey("category"))
                    {
                        url.AppendFormat("{0}/products/charts/{1}/", countryCode, pathParams["category"]);
                    }
                    else
                    {
                        throw new ArgumentNullException("category");
                    }

                    break;

                case ApiMethod.ProductNewReleases:
                    if (pathParams != null && pathParams.ContainsKey("category"))
                    {
                        url.AppendFormat("{0}/products/new/{1}/", countryCode, pathParams["category"]);
                    }
                    else
                    {
                        throw new ArgumentNullException("category");
                    }

                    break;

                case ApiMethod.Recommendations:
                    url.AppendFormat("{0}/recommendations/", countryCode);
                    break;
            }

            // Add required parameters...
            url.AppendFormat(@"?app_id={0}&app_code={1}&domain=music", appId, appCode);

            // Add other parameters...
            if (querystringParams != null)
            {
                foreach (string key in querystringParams.Keys)
                {
                    url.AppendFormat(@"&{0}={1}", key, querystringParams[key]);
                }
            }

            return new Uri(url.ToString());
        }