Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use async methods for network calls #30

Closed
wants to merge 2 commits into from
Closed
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
51 changes: 26 additions & 25 deletions TinCan/ILRS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,38 @@ limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TinCan.Documents;
using TinCan.LRSResponses;

namespace TinCan
{
public interface ILRS
{
AboutLRSResponse About();

StatementLRSResponse SaveStatement(Statement statement);
StatementLRSResponse VoidStatement(Guid id, Agent agent);
StatementsResultLRSResponse SaveStatements(List<Statement> statements);
StatementLRSResponse RetrieveStatement(Guid id);
StatementLRSResponse RetrieveVoidedStatement(Guid id);
StatementsResultLRSResponse QueryStatements(StatementsQuery query);
StatementsResultLRSResponse MoreStatements(StatementsResult result);

ProfileKeysLRSResponse RetrieveStateIds(Activity activity, Agent agent, Nullable<Guid> registration = null);
StateLRSResponse RetrieveState(String id, Activity activity, Agent agent, Nullable<Guid> registration = null);
LRSResponse SaveState(StateDocument state);
LRSResponse DeleteState(StateDocument state);
LRSResponse ClearState(Activity activity, Agent agent, Nullable<Guid> registration = null);

ProfileKeysLRSResponse RetrieveActivityProfileIds(Activity activity);
ActivityProfileLRSResponse RetrieveActivityProfile(String id, Activity activity);
LRSResponse SaveActivityProfile(ActivityProfileDocument profile);
LRSResponse DeleteActivityProfile(ActivityProfileDocument profile);

ProfileKeysLRSResponse RetrieveAgentProfileIds(Agent agent);
AgentProfileLRSResponse RetrieveAgentProfile(String id, Agent agent);
LRSResponse SaveAgentProfile(AgentProfileDocument profile);
LRSResponse DeleteAgentProfile(AgentProfileDocument profile);
Task<AboutLRSResponse> About();

Task<StatementLRSResponse> SaveStatement(Statement statement);
Task<StatementLRSResponse> VoidStatement(Guid id, Agent agent);
Task<StatementsResultLRSResponse> SaveStatements(List<Statement> statements);
Task<StatementLRSResponse> RetrieveStatement(Guid id);
Task<StatementLRSResponse> RetrieveVoidedStatement(Guid id);
Task<StatementsResultLRSResponse> QueryStatements(StatementsQuery query);
Task<StatementsResultLRSResponse> MoreStatements(StatementsResult result);

Task<ProfileKeysLRSResponse> RetrieveStateIds(Activity activity, Agent agent, Nullable<Guid> registration = null);
Task<StateLRSResponse> RetrieveState(String id, Activity activity, Agent agent, Nullable<Guid> registration = null);
Task<LRSResponse> SaveState(StateDocument state);
Task<LRSResponse> DeleteState(StateDocument state);
Task<LRSResponse> ClearState(Activity activity, Agent agent, Nullable<Guid> registration = null);

Task<ProfileKeysLRSResponse> RetrieveActivityProfileIds(Activity activity);
Task<ActivityProfileLRSResponse> RetrieveActivityProfile(String id, Activity activity);
Task<LRSResponse> SaveActivityProfile(ActivityProfileDocument profile);
Task<LRSResponse> DeleteActivityProfile(ActivityProfileDocument profile);

Task<ProfileKeysLRSResponse> RetrieveAgentProfileIds(Agent agent);
Task<AgentProfileLRSResponse> RetrieveAgentProfile(String id, Agent agent);
Task<LRSResponse> SaveAgentProfile(AgentProfileDocument profile);
Task<LRSResponse> DeleteAgentProfile(AgentProfileDocument profile);
}
}
75 changes: 75 additions & 0 deletions TinCan/LRSHttpRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2014-2017 Rustici Software

Licensed 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.
*/

using System.Collections.Generic;
using System.Text;
using System.Net.Http;

namespace TinCan
{
/// <summary>
/// LRS HTTP request.
/// </summary>
sealed class LRSHttpRequest
{
/// <summary>
/// Gets or sets the method.
/// </summary>
/// <value>The method.</value>
public HttpMethod Method { get; internal set; }

/// <summary>
/// Gets or sets the resource.
/// </summary>
/// <value>The resource.</value>
public string Resource { get; internal set; }

/// <summary>
/// Gets or sets the query parameters.
/// </summary>
/// <value>The query parameters.</value>
public Dictionary<string, string> QueryParams { get; internal set; }

/// <summary>
/// Gets or sets the headers.
/// </summary>
/// <value>The headers.</value>
public Dictionary<string, string> Headers { get; internal set; }

/// <summary>
/// Gets or sets the type of the content.
/// </summary>
/// <value>The type of the content.</value>
public string ContentType { get; internal set; }

/// <summary>
/// Gets or sets the content.
/// </summary>
/// <value>The content.</value>
public byte[] Content { get; internal set; }

/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:TinCan.LRSHttpRequest"/>.
/// </summary>
/// <returns>A <see cref="T:System.String"/> that represents the current <see cref="T:TinCan.LRSHttpRequest"/>.</returns>
public override string ToString()
{
return string.Format(
"[MyHTTPRequest: method={0}, resource={1}, queryParams={2}, headers={3}, contentType={4}, content={5}]",
Method, Resource, string.Join(";", QueryParams), Headers, ContentType, Encoding.UTF8.GetString(Content, 0, Content.Length));
}
}
}
111 changes: 111 additions & 0 deletions TinCan/LRSHttpResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2014-2017 Rustici Software

Licensed 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.
*/

using System;
using System.Net;
using System.Net.Http;
using System.Text;

namespace TinCan
{
/// <summary>
/// LRS HTTP response.
/// </summary>
sealed class LRSHttpResponse
{
/// <summary>
/// Initializes a new instance of the <see cref="T:TinCan.MyHTTPResponse"/> class.
/// </summary>
public LRSHttpResponse() { }

/// <summary>
/// Initializes a new instance of the <see cref="T:TinCan.MyHTTPResponse"/> class.
/// </summary>
/// <param name="response">Web resp.</param>
public LRSHttpResponse(HttpResponseMessage response)
{
if (response == null)
{
throw new ArgumentNullException(nameof(response));
}

Status = response.StatusCode;

if (response.Content?.Headers?.ContentType != null)
{
ContentType = response.Content.Headers.ContentType.ToString();
}

if (response.Headers.ETag != null)
{
Etag = response.Headers.ETag.ToString();
}

if (response.Content?.Headers?.LastModified != null)
{
LastModified = response.Content.Headers.LastModified.Value.LocalDateTime;
}

Content = response.Content.ReadAsByteArrayAsync().Result;
}

/// <summary>
/// Gets or sets the status.
/// </summary>
/// <value>The status.</value>
public HttpStatusCode Status { get; internal set; }

/// <summary>
/// Gets or sets the type of the content.
/// </summary>
/// <value>The type of the content.</value>
public string ContentType { get; internal set; }

/// <summary>
/// Gets or sets the content.
/// </summary>
/// <value>The content.</value>
public byte[] Content { get; internal set; }

/// <summary>
/// Gets or sets the last modified.
/// </summary>
/// <value>The last modified.</value>
public DateTime LastModified { get; internal set; }

/// <summary>
/// Gets or sets the etag.
/// </summary>
/// <value>The etag.</value>
public string Etag { get; internal set; }

/// <summary>
/// Gets or sets the ex.
/// </summary>
/// <value>The ex.</value>
public Exception Exception { get; internal set; }

/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:TinCan.LRSHttpResponse"/>.
/// </summary>
/// <returns>A <see cref="T:System.String"/> that represents the current <see cref="T:TinCan.LRSHttpResponse"/>.</returns>
public override string ToString()
{
return string.Format("[MyHTTPResponse: Status={0}, ContentType={1}, Content={2}, LastModified={3}, Etag={4}, Exception={5}]",
Status, ContentType, Encoding.UTF8.GetString(Content, 0, Content.Length), LastModified, Etag, Exception);
}
}
}
Loading