Skip to content

Commit

Permalink
fixes to use the right authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
BiBi committed Jun 4, 2019
1 parent 2814e13 commit 81d57f1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
21 changes: 10 additions & 11 deletions Nexmo.Api/ApplicationV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class ApplicationV2
/// <returns></returns>
public static AppResponse Create(AppRequest request, Credentials credentials = null)
{
var response = ApiRequest.DoPostRequest(ApiRequest.GetBaseUriFor(typeof(ApplicationV2), "/v2/applications"), request, credentials);
var response = VersionedApiRequest.DoRequest("POST",ApiRequest.GetBaseUriFor(typeof(ApplicationV2), "/v2/applications"), request, credentials);

return JsonConvert.DeserializeObject<AppResponse>(response.JsonResponse);
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public static List<AppResponse> List(int PageSize = 10, int PageIndex = 0, strin
return new List<AppResponse>
{
JsonConvert.DeserializeObject<AppResponse>(
ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(ApplicationV2),
VersionedApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(ApplicationV2),
$"/v2/applications/{AppId}"),
// TODO: using this method sig allows us to have the api auth injected at the expense of opaque code here
new Dictionary<string, string>(),
Expand All @@ -179,27 +179,27 @@ public static List<AppResponse> List(int PageSize = 10, int PageIndex = 0, strin

}

var json = ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(ApplicationV2), "/v2/applications"), new Dictionary<string, string>
var json = VersionedApiRequest.DoRequest("GET", ApiRequest.GetBaseUriFor(typeof(ApplicationV2), "/v2/applications"), new Dictionary<string, string>
{
{ "page_size", PageSize.ToString()},
{ "page_index", PageIndex.ToString()}
},
credentials);
var response = JsonConvert.DeserializeObject<AppListResponse>(json);
var response = JsonConvert.DeserializeObject<AppListResponse>(json.JsonResponse);
return response._embedded.Applications;
}

/// <summary>
/// Modify a single application
/// </summary>
/// <param name="appId">Id of the application to be updated</param>
/// <param name="request">Application request</param>
/// <param name="credentials">(Optional) Overridden credentials for only this request</param>
/// <returns></returns>
public static AppResponse Update(AppRequest request, Credentials credentials = null)
public static AppResponse Update(string appId, AppRequest request, Credentials credentials = null)
{
var sb = ApiRequest.GetQueryStringBuilderFor(request);
var response = ApiRequest.DoPutRequest(ApiRequest.GetBaseUriFor(typeof(ApplicationV2),
$"/v2/applications/{request.Id}?{sb}"), null, credentials);
var response = VersionedApiRequest.DoRequest("PUT",ApiRequest.GetBaseUriFor(typeof(ApplicationV2),
$"/v2/applications/{appId}"), request, credentials);

return JsonConvert.DeserializeObject<AppResponse>(response.JsonResponse);
}
Expand All @@ -212,9 +212,8 @@ public static AppResponse Update(AppRequest request, Credentials credentials = n
/// <returns></returns>
public static bool Delete(string appId, Credentials credentials = null)
{
var sb = ApiRequest.GetQueryStringBuilderFor(new object());
var response = ApiRequest.DoDeleteRequest(ApiRequest.GetBaseUriFor(typeof(ApplicationV2),
$"/v2/applications/{appId}?{sb}"), null, credentials);
var response = VersionedApiRequest.DoRequest("DELETE",ApiRequest.GetBaseUriFor(typeof(ApplicationV2),
$"/v2/applications/{appId}"), null, credentials);

return response.Status == HttpStatusCode.NoContent;
}
Expand Down
4 changes: 2 additions & 2 deletions Nexmo.Api/Client/ApplicationV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public List<AppResponse> List(int PageSize = 10, int PageIndex = 0, string AppId
/// <param name="request">Application request</param>
/// <param name="credentials">(Optional) Overridden credentials for only this request</param>
/// <returns></returns>
public AppResponse Update(AppRequest request, Credentials credentials = null)
public AppResponse Update(string appId, AppRequest request, Credentials credentials = null)
{
return Api.ApplicationV2.Update(request, credentials ?? Credentials);
return Api.ApplicationV2.Update(appId, request, credentials ?? Credentials);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Nexmo.Api/Request/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ internal static string DoRequest(Uri uri, Credentials creds)

// 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/"))
if (uri.AbsolutePath.StartsWith("/accounts/") || uri.AbsolutePath.StartsWith("/v2/applications"))
{
var authBytes = Encoding.UTF8.GetBytes(creds.ApiKey + ":" + creds.ApiSecret);
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic",
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/"))
if (uri.AbsolutePath.StartsWith("/accounts/") || uri.AbsolutePath.StartsWith("/v2/applications"))
{
var authBytes = Encoding.UTF8.GetBytes(creds.ApiKey + ":" + creds.ApiSecret);
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic",
Expand Down

0 comments on commit 81d57f1

Please sign in to comment.