예제 #1
0
        /// <summary>
        /// Downloads and returns the response as a json string for the specified action and parameters
        /// </summary>
        /// <param name="action">Strava API action to download the response for</param>
        /// <param name="querystringParameters">querystring parameters for the specified action</param>
        /// <param name="postParameters">post parameters for the specified action</param>
        /// <param name="isSecure">Boolean indicating if the download should be over SSL</param>
        /// <returns>Json response for the specified action</returns>
        public string Download(string action, NameValueCollection querystringParameters = null, NameValueCollection postParameters = null,
                               bool isSecure = false, ApiVersionType versionType = ApiVersionType.VersionOne)
        {
            var data = DownloadData(action, querystringParameters, postParameters, isSecure, versionType);

            return(data != null?Encoding.UTF8.GetString(data) : null);
        }
예제 #2
0
        /// <summary>
        /// Uploads json and returns the respon as a byte array for the specified action and parameters
        /// </summary>
        /// <param name="action">Strava API action to download the response for</param>
        /// <param name="json">Json to upload</param>
        /// <param name="querystringParameters">querystring parameters for the specified action</param>
        /// <param name="isSecure">Boolean indicating if the download should be over SSL</param>
        /// <returns>Json response for the specified action</returns>
        public string UploadJson(string action, string json, NameValueCollection querystringParameters = null, bool isSecure = false,
                                 ApiVersionType versionType = ApiVersionType.VersionOne)
        {
            var data = UploadJsonData(action, Encoding.UTF8.GetBytes(json), querystringParameters, isSecure, versionType);

            return(data != null?Encoding.UTF8.GetString(data) : null);
        }
예제 #3
0
        private byte[] MakeRequest(string action, NameValueCollection querystringParameters, bool isSecure,
                                   ApiVersionType versionType, Func <string, WebClient, byte[]> actualRequest)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            byte[]    data   = null;
            WebClient client = null;

            try
            {
                string baseUrl;
                if (versionType == ApiVersionType.VersionOne)
                {
                    baseUrl = isSecure ? SecureBaseUrlV1 : BaseUrlV1;
                }
                else
                {
                    baseUrl = isSecure ? SecureBaseUrlV2 : BaseUrlV2;
                }

                var url = string.Concat(baseUrl, action);
                client             = GetClient();
                client.QueryString = querystringParameters ?? new NameValueCollection();

                data = actualRequest(url, client);
            }
            catch (WebException webException)
            {
                if (Log.IsDebugEnabled)
                {
                    Log.Debug(webException.Message, webException);
                }

                throw new ApiException(webException.Message);
            }
            finally
            {
                if (client != null)
                {
                    ReleaseClient(client);
                }
            }

            return(data);
        }
예제 #4
0
 /// <summary>
 /// Uploads json and returns the respon as a byte array for the specified action and parameters
 /// </summary>
 /// <param name="action">Strava API action to download the response for</param>
 /// <param name="jsonData">Json to upload</param>
 /// <param name="querystringParameters">querystring parameters for the specified action</param>
 /// <param name="isSecure">Boolean indicating if the download should be over SSL</param>
 /// <returns>Json response for the specified action</returns>
 public byte[] UploadJsonData(string action, byte[] jsonData, NameValueCollection querystringParameters = null, bool isSecure = false,
                              ApiVersionType versionType = ApiVersionType.VersionOne)
 {
     return(MakeRequest(action, querystringParameters, isSecure, versionType,
                        (url, client) => client.UploadData(url, "POST", jsonData)));
 }
예제 #5
0
 /// <summary>
 /// Downloads and returns the response as a byte array for the specified action and parameters
 /// </summary>
 /// <param name="action">Strava API action to download the response for</param>
 /// <param name="querystringParameters">querystring parameters for the specified action</param>
 /// <param name="postParameters">post parameters for the specified action</param>
 /// <param name="isSecure">Boolean indicating if the download should be over SSL</param>
 /// <returns>byte array response for the specified action</returns>
 public byte[] DownloadData(string action, NameValueCollection querystringParameters = null, NameValueCollection postParameters = null,
                            bool isSecure = false, ApiVersionType versionType = ApiVersionType.VersionOne)
 {
     return(MakeRequest(action, querystringParameters, isSecure, versionType,
                        (url, client) => postParameters != null ? client.UploadValues(url, postParameters) : client.DownloadData(url)));
 }
예제 #6
0
        private byte[] MakeRequest(string action, NameValueCollection querystringParameters, bool isSecure,
            ApiVersionType versionType, Func<string, WebClient, byte[]> actualRequest)
        {
            if (action == null)
                throw new ArgumentNullException("action");

            byte[] data = null;
            WebClient client = null;

            try
            {
                string baseUrl;
                if (versionType == ApiVersionType.VersionOne)
                {
                    baseUrl = isSecure ? SecureBaseUrlV1 : BaseUrlV1;
                }
                else
                {
                    baseUrl = isSecure ? SecureBaseUrlV2 : BaseUrlV2;
                }

                var url = string.Concat(baseUrl, action);
                client = GetClient();
                client.QueryString = querystringParameters ?? new NameValueCollection();

                data = actualRequest(url, client);
            }
            catch (WebException webException)
            {
                if (Log.IsDebugEnabled)
                    Log.Debug(webException.Message, webException);

                throw new ApiException(webException.Message);
            }
            finally
            {
                if (client != null)
                    ReleaseClient(client);
            }

            return data;
        }
예제 #7
0
 /// <summary>
 /// Uploads json and returns the respon as a byte array for the specified action and parameters
 /// </summary>
 /// <param name="action">Strava API action to download the response for</param>
 /// <param name="jsonData">Json to upload</param>
 /// <param name="querystringParameters">querystring parameters for the specified action</param>
 /// <param name="isSecure">Boolean indicating if the download should be over SSL</param>
 /// <returns>Json response for the specified action</returns>
 public byte[] UploadJsonData(string action, byte[] jsonData, NameValueCollection querystringParameters = null, bool isSecure = false,
     ApiVersionType versionType = ApiVersionType.VersionOne)
 {
     return MakeRequest(action, querystringParameters, isSecure, versionType,
         (url, client) => client.UploadData(url, "POST", jsonData));
 }
예제 #8
0
        /// <summary>
        /// Uploads json and returns the respon as a byte array for the specified action and parameters
        /// </summary>
        /// <param name="action">Strava API action to download the response for</param>
        /// <param name="json">Json to upload</param>
        /// <param name="querystringParameters">querystring parameters for the specified action</param>
        /// <param name="isSecure">Boolean indicating if the download should be over SSL</param>
        /// <returns>Json response for the specified action</returns>
        public string UploadJson(string action, string json, NameValueCollection querystringParameters = null, bool isSecure = false,
            ApiVersionType versionType = ApiVersionType.VersionOne)
        {
            var data = UploadJsonData(action, Encoding.UTF8.GetBytes(json), querystringParameters, isSecure, versionType);

            return data != null ? Encoding.UTF8.GetString(data) : null;
        }
예제 #9
0
 /// <summary>
 /// Downloads and returns the response as a byte array for the specified action and parameters
 /// </summary>
 /// <param name="action">Strava API action to download the response for</param>
 /// <param name="querystringParameters">querystring parameters for the specified action</param>
 /// <param name="postParameters">post parameters for the specified action</param>
 /// <param name="isSecure">Boolean indicating if the download should be over SSL</param>
 /// <returns>byte array response for the specified action</returns>
 public byte[] DownloadData(string action, NameValueCollection querystringParameters = null, NameValueCollection postParameters = null,
     bool isSecure = false, ApiVersionType versionType = ApiVersionType.VersionOne)
 {
     return MakeRequest(action, querystringParameters, isSecure, versionType,
         (url, client) => postParameters != null ? client.UploadValues(url, postParameters) : client.DownloadData(url));
 }
예제 #10
0
        /// <summary>
        /// Downloads and returns the response as a json string for the specified action and parameters
        /// </summary>
        /// <param name="action">Strava API action to download the response for</param>
        /// <param name="querystringParameters">querystring parameters for the specified action</param>
        /// <param name="postParameters">post parameters for the specified action</param>
        /// <param name="isSecure">Boolean indicating if the download should be over SSL</param>
        /// <returns>Json response for the specified action</returns>
        public string Download(string action, NameValueCollection querystringParameters = null, NameValueCollection postParameters = null,
            bool isSecure = false, ApiVersionType versionType = ApiVersionType.VersionOne)
        {
            var data = DownloadData(action, querystringParameters, postParameters, isSecure, versionType);

            return data != null ? Encoding.UTF8.GetString(data) : null;
        }