Skip to content

Commit

Permalink
Merge pull request #163 from Nexmo/application_fixes
Browse files Browse the repository at this point in the history
Application Update / List, Redact, GetRecording fixed or added
  • Loading branch information
slorello89 authored Sep 24, 2019
2 parents f50aaff + 32b39cb commit c961129
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 20 deletions.
60 changes: 49 additions & 11 deletions Nexmo.Api/ApplicationV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,62 @@ public class Capabilities

public class VoiceWebhook
{
[JsonProperty("answer_url")]
public WebHook AnswerUrl { get; set; }
[JsonProperty("event_url")]
public WebHook EventUrl { get; set; }
[JsonProperty("webhooks")]
public webhooks Hooks { get; private set; }

public VoiceWebhook(Webhook answerUrl, Webhook eventUrl)
{
Hooks = new webhooks()
{
AnswerUrl = answerUrl,
EventUrl = eventUrl
};
}

public class webhooks
{
[JsonProperty("answer_url")]
public Webhook AnswerUrl { get; set; }
[JsonProperty("event_url")]
public Webhook EventUrl { get; set; }
}
}

public class RtcWebhook
{
[JsonProperty("event_url")]
public WebHook EventUrl { get; set; }
[JsonProperty("webhooks")]
public webhooks Hooks { get; private set; }

public RtcWebhook(Webhook eventUrl)
{
Hooks = new webhooks() { EventUrl = eventUrl };
}

public class webhooks
{
[JsonProperty("event_url")]
public Webhook EventUrl { get; set; }
}
}

public class MessagesWebhook
{
[JsonProperty("inbound_url")]
public WebHook InboundUrl { get; set; }
[JsonProperty("status_url")]
public WebHook StatusUrl { get; set; }
[JsonProperty("webhooks")]
public webhooks Hooks { get; private set; }

public MessagesWebhook(Webhook inboundUrl, Webhook statusUrl)
{
Hooks = new webhooks() { InboundUrl = inboundUrl, StatusUrl = statusUrl };
}

public class webhooks
{
[JsonProperty("inbound_url")]
public Webhook InboundUrl { get; set; }
[JsonProperty("status_url")]
public Webhook StatusUrl { get; set; }
}

}

public class VbcWebhook
Expand Down Expand Up @@ -173,7 +211,7 @@ public static List<AppResponse> List(int pageSize = 10, int page = 0, Credentia
var authBytes = Encoding.UTF8.GetBytes(credentials.ApiKey + ":" + credentials.ApiSecret);
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.Authorization] = "Basic" + Convert.ToBase64String(authBytes);
client.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(authBytes);
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.QueryString.Add("page_size", pageSize.ToString());
client.QueryString.Add("page", page.ToString());
Expand Down
12 changes: 12 additions & 0 deletions Nexmo.Api/Client/Call.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Newtonsoft.Json;
using Nexmo.Api.Request;
using System.IO;
using System;

namespace Nexmo.Api.ClientMethods
{
Expand Down Expand Up @@ -120,5 +122,15 @@ public Voice.Call.CallCommandResponse SendDtmf(string id, Voice.Call.DtmfCommand
return Voice.Call.SendDtmf(id, cmd, creds ?? Credentials);
}
#endregion

/// <summary>
/// Retrieves a Recording
/// <param name="recordingUrl">Url where the recording is stored</param>
/// <param name="creds">(Optional) Overridden credentials for only this request</param>
/// </summary>
public Voice.Call.CallGetRecordingResponse GetRecording(string recordingUrl, Credentials creds = null)
{
return Voice.Call.GetRecording(recordingUrl, creds ?? Credentials);
}
}
}
4 changes: 2 additions & 2 deletions Nexmo.Api/Client/Redact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public Redact(Credentials creds)
Credentials = creds;
}

public void RedactTransaction (RedactRequest redactRequest, Credentials creds = null)
public NexmoResponse RedactTransaction (RedactRequest redactRequest, Credentials creds = null)
{
Nexmo.Api.Redact.RedactTransaction(redactRequest, creds ?? Credentials);
return Nexmo.Api.Redact.RedactTransaction(redactRequest, creds ?? Credentials);
}
}
}
4 changes: 2 additions & 2 deletions Nexmo.Api/Nexmo.Api.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>Nexmo.Csharp.Client</id>
<version>3.0.1</version>
<version>4.1.0</version>
<title>Nexmo API Client</title>
<authors>Nexmo</authors>
<owners>Nexmo</owners>
Expand All @@ -12,7 +12,7 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Official C#/.NET wrapper for the Nexmo API</description>
<releaseNotes>
* Fix missing NumberInsight instantiation on client
* adding workflow_id to VerifyRequest
</releaseNotes>
<copyright>© Nexmo 2018</copyright>
<tags>SMS voice telephony phone nexmo</tags>
Expand Down
4 changes: 2 additions & 2 deletions Nexmo.Api/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.0.1.0")]
[assembly: AssemblyFileVersion("3.0.1.0")]
[assembly: AssemblyVersion("4.1.0.0")]
[assembly: AssemblyFileVersion("4.1.0.0")]
4 changes: 2 additions & 2 deletions Nexmo.Api/Redact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public RedactRequest(string id, string product, string type)
/// <param name="redactRequest"></param>
/// <param name="creds">(Optional) Overridden credentials for only this request</param>
/// <returns></returns>
public static void RedactTransaction(RedactRequest redactRequest, Credentials creds = null)
public static NexmoResponse RedactTransaction(RedactRequest redactRequest, Credentials creds = null)
{
ApiRequest.DoPostRequestWithContent(ApiRequest.GetBaseUriFor(typeof(Redact), "/v1/redact/transaction"), redactRequest, creds);
return VersionedApiRequest.DoRequest("POST",ApiRequest.GetBaseUriFor(typeof(Redact), "/v1/redact/transaction"), redactRequest, creds);
}
}
}
36 changes: 36 additions & 0 deletions Nexmo.Api/Request/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,42 @@ public static NexmoResponse DoRequest(string method, Uri uri, object requestBody
}
}

internal static HttpResponseMessage DoRequestJwt(Uri uri, Credentials creds)
{
var appId = creds?.ApplicationId ?? Configuration.Instance.Settings["appSettings:Nexmo.Application.Id"];
var appKeyPath = creds?.ApplicationKey ?? Configuration.Instance.Settings["appSettings:Nexmo.Application.Key"];

var req = new HttpRequestMessage
{
RequestUri = uri,
Method = HttpMethod.Get
};

VersionedApiRequest.SetUserAgent(ref req, creds);

req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",
Jwt.CreateToken(appId, appKeyPath));

using (LogProvider.OpenMappedContext("ApiRequest.DoRequestJwt", uri.GetHashCode()))
{
Logger.Debug($"GET {uri}");
var sendTask = Configuration.Instance.Client.SendAsync(req);
sendTask.Wait();

if (!sendTask.Result.IsSuccessStatusCode)
{
Logger.Error($"FAIL: {sendTask.Result.StatusCode}");

if (string.Compare(Configuration.Instance.Settings["appSettings:Nexmo.Api.EnsureSuccessStatusCode"],
"true", StringComparison.OrdinalIgnoreCase) == 0)
{
sendTask.Result.EnsureSuccessStatusCode();
}
}
return sendTask.Result;
}
}

internal static NexmoResponse DoPostRequest(Uri uri, object parameters, Credentials creds = null)
{
var apiParams = GetParameters(parameters);
Expand Down
2 changes: 1 addition & 1 deletion Nexmo.Api/Request/VersionedApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static NexmoResponse DoRequest(string method, Uri uri, object payload, Cr

// do we need to use basic auth?
// TODO / HACK: this is a newer auth method that needs to be incorporated better in the future
if (uri.AbsolutePath.StartsWith("/accounts/") || uri.AbsolutePath.StartsWith("/v2/applications"))
if (uri.AbsolutePath.StartsWith("/accounts/") || uri.AbsolutePath.StartsWith("/v2/applications") || uri.AbsolutePath.StartsWith("/v1/redact/transaction"))
{
var authBytes = Encoding.UTF8.GetBytes(creds.ApiKey + ":" + creds.ApiSecret);
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic",
Expand Down
35 changes: 35 additions & 0 deletions Nexmo.Api/Voice/Call.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Nexmo.Api.Helpers;
Expand Down Expand Up @@ -112,6 +114,19 @@ public class CallCommandResponse
public string uuid { get; set; }
}

public class CallGetRecordingResponse
{
/// <summary>
/// Response Status of the HTTP Request
/// </summary>
public HttpStatusCode Status { get; set; }

/// <summary>
/// Stream of bytes containg the recording file's content
/// </summary>
public byte[] ResultStream { get; set; }
}

public class CallEditCommand
{
/// <summary>
Expand Down Expand Up @@ -306,5 +321,25 @@ public static CallResponse Edit(string id, CallEditCommand cmd, Credentials cred

return JsonConvert.DeserializeObject<CallResponse>(response.JsonResponse);
}

public static CallGetRecordingResponse GetRecording(string url, Credentials creds = null)
{
using (var response = ApiRequest.DoRequestJwt(new Uri(url), creds))
{
var readTask = response.Content.ReadAsStreamAsync();
byte[] bytes;
readTask.Wait();
using (var ms = new MemoryStream())
{
readTask.Result.CopyTo(ms);
bytes = ms.ToArray();
}
return new CallGetRecordingResponse()
{
ResultStream = bytes,
Status = response.StatusCode
};
}
}
}
}

0 comments on commit c961129

Please # to comment.