示例#1
0
        public void ApiRequest_FromQuery_CompositeIds()
        {
            // Arrange
            var id = GetId();
            var id2 = GetId();
            var query = new Query<ODataObject>(GetShareFileClient())
                .From("Items")
                .Ids("id", id)
                .Ids("id2", id2);

            // Act
            var apiRequest = ApiRequest.FromQuery(query);

            // Assert
            apiRequest.Body.Should().BeNull();
            var expectedUri = string.Format("https://release.sf-api.com/sf/v3/Items(id={0},id2={1})", id, id2);
            apiRequest.GetComposedUri().ToString().Should().Be(expectedUri);
        }
        /// <summary>
        /// Remove sharefile item
        /// </summary>
        private bool RemoveShareFileItem(ShareFileClient client, Item item)
        {
            Query<ODataObject> query = new Query<ODataObject>(client);

            query.HttpMethod = "DELETE";
            query.Id(item.Id);
            query.From("Items");

            try
            {
                client.Execute(query);
            }
            catch
            {
                return false;
            }

            return true;
        }
示例#3
0
        public void ApiRequest_FromQuery_WithBaseUri()
        {
            // Arrange
            var id = GetId();
            var query = new Query<ODataObject>(GetShareFileClient())
                .From("Items")
                .Action("Test")
                .ActionIds("id", id)
                .WithBaseUri(new Uri("https://test.sf-api.com/sf/v3/RandomEntity(folderId)"));

            // Act
            var apiRequest = ApiRequest.FromQuery(query);

            // Assert
            apiRequest.Body.Should().BeNull();
            var expectedUri = string.Format("https://test.sf-api.com/sf/v3/Items/Test(id={0})", id);
            apiRequest.GetComposedUri().ToString().Should().Be(expectedUri);
        }
示例#4
0
        public void ApiRequest_FromQuery_DateTimeFormat()
        {
            var dateTime = DateTime.Now;
            var query = new Query<ODataObject>(GetShareFileClient())
                .Uri(new Uri("https://release.sf-api.com/sf/v3/Items(folder)?qsParam=1"))
                .QueryString("datetime", dateTime);
            var apiRequest = ApiRequest.FromQuery(query);

            var composedUri = apiRequest.GetComposedUri().ToString();
            composedUri.Should().Contain("datetime=" + dateTime.ToString("O"));
        }
示例#5
0
        public void ApiRequest_FromQuery_WithQueryStringOnUri()
        {
            var query = new Query<ODataObject>(GetShareFileClient())
                .Uri(new Uri("https://release.sf-api.com/sf/v3/Items(folder)?qsParam=1"))
                .QueryString("testKey", "testValue");
            var apiRequest = ApiRequest.FromQuery(query);

            var composedUri = apiRequest.GetComposedUri().ToString();
            composedUri.Should().Contain("qsParam=1");
            composedUri.Should().Contain("testKey=testValue");
        }
示例#6
0
        public void ApiRequest_FromQuery_WithUri()
        {
            // Arrange
            var id = GetId();
            var query = new Query<ODataObject>(GetShareFileClient());
            query.Uri(new Uri("https://release.sf-api.com/sf/v3/Items(" + id + ")"))
                .Action("Download");

            // Act
            var apiRequest = ApiRequest.FromQuery(query);

            // Assert
            apiRequest.Body.Should().BeNull();
            apiRequest.HttpMethod.Should().Be("GET");
            var expectedUri = "https://release.sf-api.com/sf/v3/Items(" + id + ")/Download";
            apiRequest.GetComposedUri().ToString().Should().Be(expectedUri);
        }
示例#7
0
        public void ApiRequest_FromQuery_WithQueryString()
        {
            // Arrange
            var id = GetId();
            var query = new Query<ODataObject>(GetShareFileClient());
            query.Uri(new Uri("https://release.sf-api.com/sf/v3/Items(" + id + ")"));
            query.QueryString("key1", "value1");
            query.QueryString("key2", "value2");

            // Act
            var apiRequest = ApiRequest.FromQuery(query);

            // Assert
            apiRequest.Body.Should().BeNull();
            var expectedUri = "https://release.sf-api.com/sf/v3/Items(" + id + ")?key1=value1&key2=value2";
            apiRequest.GetComposedUri().ToString().Should().Be(expectedUri);
        }
示例#8
0
        public void ApiRequest_FromQuery_WithHeader()
        {
            // Arrange
            var id = GetId();
            var query = new Query<ODataObject>(GetShareFileClient());
            query.Uri(new Uri("https://release.sf-api.com/sf/v3/Items(" + id + ")"));
            query.AddHeader("key1", "value1");

            // Act
            var apiRequest = ApiRequest.FromQuery(query);

            // Assert
            apiRequest.Body.Should().BeNull();
            var expectedUri = "https://release.sf-api.com/sf/v3/Items(" + id + ")";
            apiRequest.GetComposedUri().ToString().Should().Be(expectedUri);
            apiRequest.HeaderCollection.Should().ContainKey("key1");
            apiRequest.HeaderCollection.Should().ContainValue("value1");
        }
示例#9
0
        public void ApiRequest_FromQuery_WithBody_AsPost()
        {
            // Arrange
            var id = GetId();
            var query = new Query<ODataObject>(GetShareFileClient());
            query.Uri(new Uri("https://release.sf-api.com/sf/v3/Items(" + id + ")"))
                .Action("CreateFolder");
            query.HttpMethod = "POST";
            query.Body = new Folder();

            // Act
            var apiRequest = ApiRequest.FromQuery(query);

            // Assert
            apiRequest.Body.Should().Be(query.Body);
            apiRequest.HttpMethod.Should().Be("POST");
            var expectedUri = "https://release.sf-api.com/sf/v3/Items(" + id + ")/CreateFolder";
            apiRequest.GetComposedUri().ToString().Should().Be(expectedUri);
        }
示例#10
0
        public void ApiRequest_FromQuery_WithBaseUri_Fails()
        {
            // Arrange
            var id = GetId();

            try
            {
                // Act
                var query = new Query<ODataObject>(GetShareFileClient())
                    .From("Items")
                    .Action("Test")
                    .ActionIds("id", id)
                    .WithBaseUri(new Uri("https://test.sf-api.com/sfItems(folderId)"));
            }
            catch (ArgumentException argumentException)
            {
                Assert.Pass();
            }
            catch (Exception)
            {

            }

            Assert.Fail();
        }
        protected override void ProcessRecord()
        {
            if (Id != null && Uri != null) throw new Exception("Set only Id or Uri");
            if (Action == null && Cast != null) Action = Cast;
            if (Action == null && Navigation != null) Action = Navigation;
            if (Method == null) Method = "GET";
            Method = Method.ToUpper();

            Query<ODataObject> query = new Query<ODataObject>(Client.Client);
            query.HttpMethod = Method;

            if (string.IsNullOrWhiteSpace(Filter) == false) query.Filter(AddFilter());
            if (Entity != null) query = query.From(Entity);
            if (Action != null) query = query.Action(Action);
            if (Id != null) query = query.Id(Id);
            else if (Uri != null) query = query.Id(Uri.ToString());

            if (Parameters != null)
            {
                foreach (var key in Parameters.Keys)
                {
                    if (!(key is string)) throw new Exception("Use strings for parameter keys");
                    query = query.QueryString((string)key, Parameters[key].ToString());
                }
            }
            if (Expand != null) query = query.Expand(Expand);
            if (Select != null) query = query.Select(Select);
            if (Body != null)
            {
                query.Body = Body;
            }
            else if (BodyText != null)
            {
                query.Body = BodyText;
            }
            try
            {
                var response = query.Execute();
                if (response != null)
                {
                    Type t = response.GetType();
                    if (t.IsGenericType)
                    {
                        if (t.GetGenericTypeDefinition() == typeof(ODataFeed<>))
                        {
                            var feed = t.GetProperty("Feed").GetValue(response, null) as IEnumerable<ODataObject>;
                            foreach (var o in feed)
                            {
                                WriteObject(o);
                            }
                        }
                    }
                    else
                    {
                        WriteObject(response);
                    }
                }
            }
            catch (ODataException e)
            {
                WriteError(new ErrorRecord(new Exception(e.Code.ToString() + ": " + e.ODataExceptionMessage.Message), e.Code.ToString(), ErrorCategory.NotSpecified, query.GetEntity()));
            }
            catch(Exception e)
            {
                ShowSuggestion(Entity,Action,Parameters);
                throw;
            }
        }