Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Updating some api methods version V1, to the corresponding V2 methods. #5

Merged
merged 6 commits into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/SymphonyOSS.RestApiClient/Api/AgentApi/AttachmentsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
// specific language governing permissions and limitations
// under the License.

using System;
using System.IO;
using RestSharp;
using RestSharp.Extensions;

namespace SymphonyOSS.RestApiClient.Api.AgentApi
{
using Authentication;
using Generated.OpenApi.AgentApi.Client;
using Generated.OpenApi.AgentApi.Model;
using System.IO;
using RestSharp;
using RestSharp.Extensions;

/// <summary>
/// Provides methods for getting attachments from messages and uploading attachments to streams, by
Expand Down
8 changes: 4 additions & 4 deletions src/SymphonyOSS.RestApiClient/Api/AgentApi/DatafeedApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void Stop()
_shouldStop = true;
}

private async void NotifyAsync(EventHandler<MessageEventArgs> messageEventHandler, MessageList messageList)
private async void NotifyAsync(EventHandler<MessageEventArgs> messageEventHandler, V2MessageList messageList)
{
// Notify each handler in a separate task, maintaining the order of messages in the list, and
// get back to reading the data feed again without waiting for listeners to process messages.
Expand All @@ -126,7 +126,7 @@ private async void NotifyAsync(EventHandler<MessageEventArgs> messageEventHandle
_tasks[messageEventHandler] = Task.Run(() => Notify(messageEventHandler, messageList));
}

private void Notify(EventHandler<MessageEventArgs> messageEventHandler, MessageList messageList)
private void Notify(EventHandler<MessageEventArgs> messageEventHandler, V2MessageList messageList)
{
foreach (var message in messageList)
{
Expand All @@ -146,9 +146,9 @@ private Datafeed CreateDatafeed()
return _apiExecutor.Execute(_datafeedApi.V1DatafeedCreatePost, _authTokens.SessionToken, _authTokens.KeyManagerToken);
}

private MessageList ReadDatafeed(string id, int? maxMessages = null)
private V2MessageList ReadDatafeed(string id, int? maxMessages = null)
{
return _apiExecutor.Execute(_datafeedApi.V1DatafeedIdReadGet, id, _authTokens.SessionToken, _authTokens.KeyManagerToken, maxMessages);
return _apiExecutor.Execute(_datafeedApi.V2DatafeedIdReadGet, id, _authTokens.SessionToken, _authTokens.KeyManagerToken, maxMessages);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ namespace SymphonyOSS.RestApiClient.Api.AgentApi
/// </summary>
public class MessageEventArgs : EventArgs
{
public MessageEventArgs(Message message)
public MessageEventArgs(V2BaseMessage message)
{
Message = message;
}

public Message Message { get; private set; }
public V2BaseMessage Message { get; private set; }
}
}
4 changes: 2 additions & 2 deletions src/SymphonyOSS.RestApiClient/Api/AgentApi/MessagesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public V2Message PostMessage(string sid, V2MessageSubmission message)
/// <param name="offset">Number of messages to skip.</param>
/// <param name="maxMessages">Max number of messages to return. If no value is provided, 50 is the default.</param>
/// <returns>The list of messages.</returns>
public MessageList GetMessages(string sid, long? since, int? offset = null, int? maxMessages = null)
public V2MessageList GetMessages(string sid, long? since, int? offset = null, int? maxMessages = null)
{
return _apiExecutor.Execute(_messagesApi.V1StreamSidMessageGet, sid, since, _authTokens.SessionToken, _authTokens.KeyManagerToken, offset, maxMessages);
return _apiExecutor.Execute(_messagesApi.V2StreamSidMessageGet, sid, since, _authTokens.SessionToken, _authTokens.KeyManagerToken, offset, maxMessages);
}
}
}
64 changes: 64 additions & 0 deletions src/SymphonyOSS.RestApiClient/Api/AgentApi/UtilApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the Symphony Software Foundation (SSF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SSF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

namespace SymphonyOSS.RestApiClient.Api.AgentApi
{
using Authentication;
using Generated.OpenApi.AgentApi.Client;
using Generated.OpenApi.AgentApi.Model;

/// <summary>
/// Provides methods for testing endpoint, or use an example of an obsolete endpoint.
/// Encapsulates <see cref="Generated.OpenApi.AgentApi.Api.UtilApi"/>,
/// adding authentication token management and a custom execution strategy.
/// </summary>
public class UtilApi
{
private readonly Generated.OpenApi.AgentApi.Api.IUtilApi _datafeedApi;

private readonly IAuthTokens _authTokens;

private readonly IApiExecutor _apiExecutor;

public UtilApi(IAuthTokens authTokens, Configuration configuration, IApiExecutor apiExecutor)
{
_datafeedApi = new Generated.OpenApi.AgentApi.Api.UtilApi(configuration);
_authTokens = authTokens;
_apiExecutor = apiExecutor;
}

/// <summary>
/// Will return a message with the same content as the one sent.
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public SimpleMessage Echo(string msg)
{
return _apiExecutor.Execute(_datafeedApi.V1UtilEchoPost, _authTokens.SessionToken, _authTokens.KeyManagerToken, new SimpleMessage(msg));
}

/// <summary>
/// Example of what an obsolete endpoint is like (returns the input).
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public SimpleMessage Obsolete(string msg)
{
return _apiExecutor.Execute(_datafeedApi.V1UtilObsoletePost, _authTokens.SessionToken, _authTokens.KeyManagerToken, new SimpleMessage(msg));
}
}
}
24 changes: 18 additions & 6 deletions src/SymphonyOSS.RestApiClient/Api/PodApi/StreamsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ public Stream CreateStream(UserIdList uidList)
/// </summary>
/// <param name="payload">Room attributes.</param>
/// <returns>The created room.</returns>
public RoomDetail CreateRoom(RoomCreate payload)
public V2RoomDetail CreateRoom(V2RoomAttributes payload)
{
return _apiExecutor.Execute(_streamsApi.V1RoomCreatePost, payload, _authTokens.SessionToken);
return _apiExecutor.Execute(_streamsApi.V2RoomCreatePost, payload, _authTokens.SessionToken);
}

/// <summary>
/// Get information about a chatrooom.
/// </summary>
/// <param name="id">The room ID.</param>
/// <returns>The room details.</returns>
public RoomDetail GetRoomInfo(string id)
public V2RoomDetail GetRoomInfo(string id)
{
return _apiExecutor.Execute(_streamsApi.V1RoomIdInfoGet, id, _authTokens.SessionToken);
return _apiExecutor.Execute(_streamsApi.V2RoomIdInfoGet, id, _authTokens.SessionToken);
}

/// <summary>
Expand All @@ -102,9 +102,21 @@ public RoomDetail SetRoomActive(string id, bool active)
/// <param name="id">Room ID.</param>
/// <param name="payload">Room attributes.</param>
/// <returns>The room.</returns>
public RoomDetail UpdateRoom(string id, RoomAttributes payload)
public V2RoomDetail UpdateRoom(string id, V2RoomAttributes payload)
{
return _apiExecutor.Execute(_streamsApi.V1RoomIdUpdatePost, id, payload, _authTokens.SessionToken);
return _apiExecutor.Execute(_streamsApi.V2RoomIdUpdatePost, id, payload, _authTokens.SessionToken);
}

/// <summary>
/// Searches for a room based on certain search criteria given.
/// </summary>
/// <param name="query">Search criteria.</param>
/// <param name="skip">Number or results to skip.</param>
/// <param name="limit">Max number of results.</param>
/// <returns>The search results.</returns>
public RoomSearchResults SearchRoom(RoomSearchCriteria query, int? skip = null, int? limit = null)
{
return _apiExecutor.Execute(_streamsApi.V2RoomSearchPost, _authTokens.SessionToken, query, skip, limit);
}
}
}
5 changes: 5 additions & 0 deletions src/SymphonyOSS.RestApiClient/Factories/AgentApiFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public MessagesApi CreateMessagesApi(ISessionManager sessionManager, IApiExecuto
return Create<MessagesApi>(sessionManager, apiExecutor);
}

public UtilApi CreateUtilApi(ISessionManager sessionManager, IApiExecutor apiExecutor = null)
{
return Create<UtilApi>(sessionManager, apiExecutor);
}

private T Create<T>(ISessionManager sessionManager, IApiExecutor apiExecutor = null)
{
var apiClient = new ApiClient(_baseUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="Api\AgentApi\MessageEventArgs.cs" />
<Compile Include="Api\AgentApi\AttachmentsApi.cs" />
<Compile Include="Api\AgentApi\MessagesApi.cs" />
<Compile Include="Api\AgentApi\UtilApi.cs" />
<Compile Include="Api\IRetryStrategy.cs" />
<Compile Include="Api\RefreshTokensRetryStrategy.cs" />
<Compile Include="Api\PodApi\MessageSuppressionApi.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

namespace SymphonyOSS.RestApiClient.Tests
{
using Api.AgentApi;
using Authentication;
using Factories;
using System.Security.Cryptography.X509Certificates;
Expand Down
18 changes: 9 additions & 9 deletions test/SymphonyOSS.RestApiClient.Tests/DatafeedApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public void EnsureHandler_receives_messages()
var semaphore = new Semaphore(0, int.MaxValue);
var messageList = CreateMessageList(2);
_apiExecutorMock.Setup(obj => obj.Execute(It.IsAny<Func<string, string, Datafeed>>(), "sessionToken", "keyManagerToken")).Returns(new Datafeed("streamId"));
_apiExecutorMock.Setup(obj => obj.Execute(It.IsAny<Func<string, string, string, int?, MessageList>>(), "streamId", "sessionToken", "keyManagerToken", (int?)null))
_apiExecutorMock.Setup(obj => obj.Execute(It.IsAny<Func<string, string, string, int?, V2MessageList>>(), "streamId", "sessionToken", "keyManagerToken", (int?)null))
.Returns(messageList);
var messagesReceived = 0;
_datafeedApi.OnMessage += (_, messageEventArgs) =>
{
++messagesReceived;
if (messageEventArgs.Message.Id == "msg2")
if ((messageEventArgs.Message as V2Message)?.Id == "msg2")
{
semaphore.Release(1);
}
Expand All @@ -78,7 +78,7 @@ public void EnsureRemoved_handler_does_not_receive_messages()
var mainSemaphore = new Semaphore(0, int.MaxValue);
var messagesSent = 0;
_apiExecutorMock.Setup(obj => obj.Execute(It.IsAny<Func<string, string, Datafeed>>(), "sessionToken", "keyManagerToken")).Returns(new Datafeed("streamId"));
_apiExecutorMock.Setup(obj => obj.Execute(It.IsAny<Func<string, string, string, int?, MessageList>>(), "streamId", "sessionToken", "keyManagerToken", (int?)null))
_apiExecutorMock.Setup(obj => obj.Execute(It.IsAny<Func<string, string, string, int?, V2MessageList>>(), "streamId", "sessionToken", "keyManagerToken", (int?)null))
.Returns(() =>
{
if (messagesSent <= 1)
Expand Down Expand Up @@ -119,8 +119,8 @@ public void EnsureListen_can_be_stopped()
{
var semaphore = new Semaphore(0, int.MaxValue);
_apiExecutorMock.Setup(obj => obj.Execute(It.IsAny<Func<string, string, Datafeed>>(), "sessionToken", "keyManagerToken")).Returns(new Datafeed("streamId"));
_apiExecutorMock.Setup(obj => obj.Execute(It.IsAny<Func<string, string, string, int?, MessageList>>(), "streamId", "sessionToken", "keyManagerToken", (int?)null))
.Returns((MessageList)null)
_apiExecutorMock.Setup(obj => obj.Execute(It.IsAny<Func<string, string, string, int?, V2MessageList>>(), "streamId", "sessionToken", "keyManagerToken", (int?)null))
.Returns((V2MessageList)null)
.Callback(() =>
{
semaphore.Release(1);
Expand All @@ -129,15 +129,15 @@ public void EnsureListen_can_be_stopped()
semaphore.WaitOne();
_datafeedApi.Stop();
task.Wait();
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, string, int?, MessageList>>(), "streamId", "sessionToken", "keyManagerToken", (int?)null));
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, string, int?, V2MessageList>>(), "streamId", "sessionToken", "keyManagerToken", (int?)null));
}

private MessageList CreateMessageList(int count, int startId = 1)
private V2MessageList CreateMessageList(int count, int startId = 1)
{
var result = new MessageList();
var result = new V2MessageList();
for (var i = 0; i < count; ++i)
{
result.Add(new Message("msg" + (startId + i), "timestamp", "messageType", "streamId", "message", 1));
result.Add(new V2Message("msg" + (startId + i), "timestamp", "messageType", "streamId", "message", 1));
};
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion test/SymphonyOSS.RestApiClient.Tests/MessagesApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void EnsureGet_uses_retry_strategy()
{
const string sid = "sid";
_messagesApi.GetMessages(sid, null);
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, long?, string, string, int?, int?, MessageList>>(), sid, (long?) null, "sessionToken", "keyManagerToken", (int?) null, (int?) null));
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, long?, string, string, int?, int?, V2MessageList>>(), sid, (long?) null, "sessionToken", "keyManagerToken", (int?) null, (int?) null));
}
}
}
20 changes: 15 additions & 5 deletions test/SymphonyOSS.RestApiClient.Tests/StreamsApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ public void EnsureCreateStream_uses_retry_strategy()
[Fact]
public void EnsureCreateRoom_uses_retry_strategy()
{
var payload = new RoomCreate();
var payload = new V2RoomAttributes();
_streamsApi.CreateRoom(payload);
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<RoomCreate, string, RoomDetail>>(), payload, "sessionToken"));
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<V2RoomAttributes, string, V2RoomDetail>>(), payload, "sessionToken"));
}

[Fact]
public void EnsureGetRoomInfo_uses_retry_strategy()
{
const string id = "id";
_streamsApi.GetRoomInfo(id);
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, RoomDetail>>(), id, "sessionToken"));
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, V2RoomDetail>>(), id, "sessionToken"));
}

[Fact]
Expand All @@ -78,9 +78,19 @@ public void EnsureSetRoomActive_uses_retry_strategy()
public void EnsureUpdateRoom_uses_retry_strategy()
{
const string id = "id";
var payload = new RoomAttributes();
var payload = new V2RoomAttributes();
_streamsApi.UpdateRoom(id, payload);
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, RoomAttributes, string, RoomDetail>>(), id, payload, "sessionToken"));
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, V2RoomAttributes, string, V2RoomDetail>>(), id, payload, "sessionToken"));
}

[Fact]
public void EnsureSearchRoom_uses_retry_strategy()
{
var searchCriteria = new RoomSearchCriteria(Query: "some_room");
int? skip = 0;
int? limit = 1;
_streamsApi.SearchRoom(searchCriteria, skip, limit);
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, RoomSearchCriteria, int?, int?, RoomSearchResults>>(), "sessionToken", searchCriteria, skip, limit));
}

}
Expand Down
50 changes: 50 additions & 0 deletions test/SymphonyOSS.RestApiClient.Tests/UtilApiTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SymphonyOSS.RestApiClient.Tests
{
using Api;
using Api.AgentApi;
using Authentication;
using Generated.OpenApi.AgentApi.Client;
using Generated.OpenApi.AgentApi.Model;
using Moq;
using Xunit;

/// <summary>
/// Summary description for UtilApiTest
/// </summary>
public class UtilApiTest
{
private readonly UtilApi _utilApi;

private readonly Mock<IApiExecutor> _apiExecutorMock;

public UtilApiTest()
{
var sessionManagerMock = new Mock<IAuthTokens>();
sessionManagerMock.Setup(obj => obj.SessionToken).Returns("sessionToken");
var configuration = new Configuration();
_apiExecutorMock = new Mock<IApiExecutor>();
_utilApi = new UtilApi(sessionManagerMock.Object, configuration, _apiExecutorMock.Object);
}

[Fact]
public void EnsureEcho_uses_retry_strategy()
{
const string msg = "Hello!";
_utilApi.Echo(msg);
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, SimpleMessage>>(), msg));
}

[Fact]
public void EnsureObsolete_uses_retry_strategy()
{
const string msg = "Obsolete!";
_utilApi.Obsolete(msg);
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, SimpleMessage>>(), msg));
}
}
}