Ejemplo n.º 1
0
        public StoryList ListStories(IDictionary <string, string> parameters = null, SamAuth auth = null)
        {
            var url      = string.Format("{0}/stories.xml", ApiBaseUrl);
            var response = request(url, parameters, auth);

            return(Utils.FromXml <StoryList>(response));
        }
Ejemplo n.º 2
0
        public User RetrieveUser(SamAuth auth = null)
        {
            var url      = string.Format("{0}/user.xml", ApiBaseUrl);
            var response = request(url, null, auth);

            return(Utils.FromXml <User>(response));
        }
Ejemplo n.º 3
0
        public UserList ListAccountUsers(SamAuth auth = null)
        {
            var url      = string.Format("{0}/account/users.xml", ApiBaseUrl);
            var response = request(url, null, auth);

            return(Utils.FromXml <UserList>(response));
        }
Ejemplo n.º 4
0
        public Account RetrieveAccount(SamAuth auth = null)
        {
            var url      = string.Format("{0}/account.xml", ApiBaseUrl);
            var response = request(url, new Dictionary <string, string>(), auth);

            return(Utils.FromXml <Account>(response));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes the upload by creating a persistent upload tracking ID.
        /// </summary>
        /// <param name="fileParams">Describes the file to be uploaded.</param>
        /// <param name="auth">Your SAM auth object, if not already set up.</param>
        /// <returns>An object with the uploaded media_id in it.</returns>
        public SamUpload StartUpload(SamStartUploadParams fileParams, SamAuth auth = null)
        {
            var body     = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(fileParams));
            var url      = string.Format("{0}/upload.xml", UploadBaseUrl);
            var response = request(url, body, null, auth);

            return(Utils.FromXml <SamUpload>(response));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a new story in your account.
        /// </summary>
        /// <param name="storyParams">Describes the story to be created.</param>
        /// <param name="auth">Your SAM auth object, if not already set up.</param>
        /// <returns>A story object containing the name and ID of the newly created story.</returns>
        public Story CreateStory(SamCreateStoryParams storyParams, SamAuth auth = null)
        {
            var body     = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(storyParams));
            var url      = string.Format("{0}/stories.xml", ApiBaseUrl);
            var response = request(url, body, null, auth);

            return(Utils.FromXml <Story>(response));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Appends a part of a file to an in-progress file upload.
        /// </summary>
        /// <param name="part">The byte array representing a part of the file. Parts must be a
        /// minimum of 5MB (except for the final part) and a maximum of 10MB.</param>
        /// <param name="partNumber">The index of the part (NOTE: the first part has an index of 1)</param>
        /// <param name="uploadId">The ID received from the InitMediaUpload function.</param>
        /// <param name="auth">Your SAM auth object, if not already set up.</param>
        public void AppendUpload(string mediaId, SamAppendUploadParams parameters, SamAuth auth = null)
        {
            var query = new Dictionary <string, string>()
            {
                { "part", parameters.part.ToString() }
            };
            var url = string.Format("{0}/upload/{1}/append.xml", UploadBaseUrl, mediaId);

            request(url, parameters.body, "POST", "application/octet-stream", query, auth);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Provides a wrapper method for the following upload methods that automatically
        /// initiates an upload, splits a byte array into appropriately sized parts, and
        /// finishes the upload when all of the parts have been uploaded. NOTE: not optimized
        /// for very large files. It's recommended that you read large files in parts from
        /// disk and use the separate functions below.
        /// </summary>
        /// <param name="fileParams">An object with the file info.</param>
        /// <param name="auth">Your SAM auth object, if not already set up.</param>
        /// <returns>An upload ID string.</returns>
        public string UploadMedia(byte[] bytes, string mimetype, string name = null, SamAuth auth = null)
        {
            var fileParams = new SamStartUploadParams();

            fileParams.mimetype = mimetype;
            fileParams.size     = bytes.Length;
            fileParams.name     = name;

            double parts = (double)bytes.Length / (double)Constants.PART_SIZE;

            fileParams.parts = (int)Math.Ceiling(parts);

            var uploadResult = StartUpload(fileParams, auth);

            using (var stream = new MemoryStream(bytes))
            {
                var    partNumber = 1;
                byte[] part       = new byte[Constants.PART_SIZE];

                while (stream.Position < stream.Length)
                {
                    var bytesRead = stream.Read(part, 0, Constants.PART_SIZE);
                    if (bytesRead < Constants.PART_SIZE)
                    {
                        Array.Resize(ref part, bytesRead);
                    }

                    var uploadParameters = new SamAppendUploadParams();
                    uploadParameters.part = partNumber;
                    uploadParameters.body = part;

                    AppendUpload(uploadResult.media_id, uploadParameters, auth);

                    partNumber++;
                }
            }

            CompleteUpload(uploadResult.media_id, auth);

            return(uploadResult.media_id);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Deletes the specified story from your accout.
        /// </summary>
        /// <param name="storyId">The ID of the story to be deleted.</param>
        /// <param name="auth">Your SAM auth object, if not already set up.</param>
        public void DeleteStory(string storyId, SamAuth auth = null)
        {
            var url = string.Format("{0}/stories/{1}.xml", ApiBaseUrl, storyId);

            request(url, null, "DELETE", null, null, auth);
        }
Ejemplo n.º 10
0
        public Story RetrieveStory(string storyId, IDictionary <string, string> parameters = null, SamAuth auth = null)
        {
            var url      = string.Format("{0}/stories/{1}.xml", ApiBaseUrl, storyId);
            var response = request(url, parameters, auth);

            return(Utils.FromXml <Story>(response));
        }
Ejemplo n.º 11
0
 // Shorthand for a GET request
 private string request(string url, IDictionary <string, string> parameters = null, SamAuth auth = null)
 {
     return(request(url, null, "GET", null, parameters, auth));
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Creates a new asset in the specified story.
        /// </summary>
        /// <param name="storyId">The ID of the story to add the asset to.</param>
        /// <param name="assetParams">An object describing the asset's properties.</param>
        /// <param name="auth">Your SAM auth object, if not already set up.</param>
        /// <returns>A SocialAsset object.</returns>
        public SocialAsset CreateAsset(string storyId, SamCreateAssetParams assetParams, SamAuth auth = null)
        {
            byte[] body     = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(assetParams));
            var    url      = string.Format("{0}/stories/{1}/assets.xml", ApiBaseUrl, storyId);
            var    response = request(url, body, null, auth);

            return(Utils.FromXml <SocialAsset>(response));
        }
Ejemplo n.º 13
0
 // Shorthand for a POST request
 private string request(string url, byte[] body, IDictionary <string, string> parameters = null, SamAuth auth = null)
 {
     return(request(url, body, "POST", "application/json", parameters, auth));
 }
Ejemplo n.º 14
0
        public SocialAsset RetrieveAsset(string storyId, string assetId, IDictionary <string, string> parameters = null, SamAuth auth = null)
        {
            var url      = string.Format("{0}/stories/{1}/assets/{2}.xml", ApiBaseUrl, storyId, assetId);
            var response = request(url, parameters, auth);

            return(Utils.FromXml <SocialAsset>(response));
        }
Ejemplo n.º 15
0
 public SamClient(SamAuth auth)
     : this()
 {
     ApiAuth = auth;
 }
Ejemplo n.º 16
0
 public void SetAuth(AuthType authType, string authToken)
 {
     this.ApiAuth = new SamAuth(authType, authToken);
 }
Ejemplo n.º 17
0
        private string request(string url, byte[] body, string method, string contentType, IDictionary <string, string> parameters = null, SamAuth auth = null)
        {
            if (auth == null)
            {
                auth = ApiAuth;
            }

            if (auth == null)
            {
                throw new SamException("No authentication provided. (HINT: you set your SAM client's API authentication using \"client.ApiAuth = new SamAuth(AuthType.API_KEY, {API_KEY})\".");
            }

            if (parameters == null)
            {
                parameters = new Dictionary <string, string>();
            }
            parameters[auth.Type.ToString().ToLower()] = auth.Token;

            url += generateQueryString(parameters);

            WebRequest request = WebRequest.Create(url);

            request.Method = method;

            if (request.Method == "POST")
            {
                request.ContentType = contentType;

                if (body != null)
                {
                    request.ContentLength = body.Length;
                    var requestStream = request.GetRequestStream();
                    requestStream.Write(body, 0, body.Length);
                }
            }

            // Read the response
            HttpWebResponse response;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException e)
            {
                response = (HttpWebResponse)e.Response;
                if (response == null)
                {
                    throw new SamConnectionException(e.Message);
                }
            }

            Stream       dataStream = response.GetResponseStream();
            StreamReader reader     = new StreamReader(dataStream);
            string       xml        = reader.ReadToEnd();

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
                return(xml);

            case HttpStatusCode.Created:
            case HttpStatusCode.NoContent:
                return(null);

            case HttpStatusCode.BadRequest:
                //case HttpStatusCode.NotFound:
                throw SamInvalidRequestException.Generate(xml);

            case HttpStatusCode.Unauthorized:
                throw SamAuthenticationException.Generate(xml);

            case HttpStatusCode.InternalServerError:
                throw SamApiException.Generate(xml);

            default:
                throw new SamException("Unexpected Response Code: " + (int)response.StatusCode);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Finalizes the file upload once all parts have been uploaded.
        /// </summary>
        /// <param name="uploadId">The upload ID string.</param>
        /// <param name="auth">Your SAM authorization object, if not already set up.</param>
        public void CompleteUpload(string mediaId, SamAuth auth = null)
        {
            var url = string.Format("{0}/upload/{1}/complete.xml", UploadBaseUrl, mediaId);

            request(url, null, "POST", "application/json", null, auth);
        }