Skip to content

Commit 6f46bd7

Browse files
authored
Merge pull request #6 from docusign/RoomsApi
Rooms API merge PR
2 parents 60a4dd4 + f885607 commit 6f46bd7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1341
-38
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace DocuSign.CodeExamples.Common
2+
{
3+
public enum ExamplesAPIType
4+
{
5+
Rooms,
6+
ESignature
7+
}
8+
}

launcher-csharp/Common/LocalsFilter.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.AspNetCore.Mvc.Filters;
44
using Microsoft.Extensions.Caching.Memory;
5+
using Microsoft.Extensions.Configuration;
56
using System;
67
using System.Linq;
78
using System.Security.Claims;
@@ -10,15 +11,21 @@ namespace DocuSign.CodeExamples.Common
1011
{
1112
public class LocalsFilter : IActionFilter
1213
{
13-
DSConfiguration Config { get; }
14+
DSConfiguration DocuSignConfiguration { get; }
1415

1516
private readonly IRequestItemsService _requestItemsService;
1617
private IMemoryCache _cache;
18+
private readonly IConfiguration _configuration;
1719

18-
public LocalsFilter(DSConfiguration config, IRequestItemsService requestItemsService, IMemoryCache cache)
20+
public LocalsFilter(
21+
DSConfiguration docuSignConfiguration,
22+
IRequestItemsService requestItemsService,
23+
IMemoryCache cache,
24+
IConfiguration configuration)
1925
{
20-
Config = config;
26+
DocuSignConfiguration = docuSignConfiguration;
2127
_cache = cache;
28+
_configuration = configuration;
2229
_requestItemsService = requestItemsService;
2330
}
2431

@@ -38,14 +45,13 @@ public void OnActionExecuting(ActionExecutingContext context)
3845
var httpContext = context.HttpContext;
3946

4047
var locals = httpContext.Session.GetObjectFromJson<Locals>("locals") ?? new Locals();
41-
locals.DsConfig = Config;
48+
locals.DsConfig = DocuSignConfiguration;
4249
locals.Session = httpContext.Session.GetObjectFromJson<Session>("session") ?? null;
4350
locals.Messages = "";
4451
locals.Json = null;
4552
locals.User = null;
4653
viewBag.Locals = locals;
47-
viewBag.showDoc = Config.documentation != null;
48-
54+
viewBag.showDoc = DocuSignConfiguration.documentation != null;
4955

5056
var identity = httpContext.User.Identity as ClaimsIdentity;
5157
if (identity != null && !identity.IsAuthenticated && (_requestItemsService.User?.AccessToken == null))
@@ -75,7 +81,7 @@ public void OnActionExecuting(ActionExecutingContext context)
7581
ExpireIn = _requestItemsService.User?.ExpireIn
7682
};
7783

78-
_requestItemsService.User = locals.User;
84+
_requestItemsService.User = locals.User;
7985
}
8086

8187
if (locals.Session == null)
@@ -85,18 +91,20 @@ public void OnActionExecuting(ActionExecutingContext context)
8591
{
8692
AccountId = identity.FindFirst(x => x.Type.Equals("account_id")).Value,
8793
AccountName = identity.FindFirst(x => x.Type.Equals("account_name")).Value,
88-
BasePath = identity.FindFirst(x => x.Type.Equals("base_uri")).Value
94+
BasePath = identity.FindFirst(x => x.Type.Equals("base_uri")).Value,
95+
RoomsApiBasePath = _configuration["DocuSign:RoomsApiEndpoint"]
8996
}
9097
:
9198
new Session
9299
{
93100
AccountId = _requestItemsService.Session.AccountId,
94101
AccountName = _requestItemsService.Session.AccountName,
95102
BasePath = _requestItemsService.Session.BasePath,
103+
RoomsApiBasePath = _requestItemsService.Session.RoomsApiBasePath,
96104
};
97105

98-
_requestItemsService.Session = locals.Session;
99-
}
106+
_requestItemsService.Session = locals.Session;
107+
}
100108
}
101109
}
102110
}

launcher-csharp/Common/RequestItemService.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
using Microsoft.Extensions.Caching.Memory;
66
using Microsoft.Extensions.Configuration;
77
using System;
8+
using System.Collections.Generic;
89
using System.Linq;
910
using System.Security.Claims;
10-
using System.Text;
1111
using static DocuSign.eSign.Client.Auth.OAuth;
1212
using static DocuSign.eSign.Client.Auth.OAuth.UserInfo;
1313

@@ -29,7 +29,7 @@ public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCach
2929
_configuration = configuration;
3030
_cache = cache;
3131
Status = "sent";
32-
_apiClient = _apiClient ?? new ApiClient();
32+
_apiClient ??= new ApiClient();
3333
var identity = httpContextAccessor.HttpContext.User.Identity as ClaimsIdentity;
3434

3535
if (identity != null && identity.IsAuthenticated)
@@ -45,8 +45,21 @@ public void UpdateUserFromJWT()
4545
this._configuration["DocuSignJWT:ImpersonatedUserId"],
4646
this._configuration["DocuSignJWT:AuthServer"],
4747
DSHelper.ReadFileContent(DSHelper.PrepareFullPrivateKeyFilePath(this._configuration["DocuSignJWT:PrivateKeyFile"])),
48-
1);
49-
48+
1,
49+
new List<string>
50+
{
51+
"signature",
52+
"impersonation",
53+
"dtr.rooms.read",
54+
"dtr.rooms.write",
55+
"dtr.documents.read",
56+
"dtr.documents.write",
57+
"dtr.profile.read",
58+
"dtr.profile.write",
59+
"dtr.company.read",
60+
"dtr.company.write",
61+
"room_forms"
62+
});
5063
_account = GetAccountInfo(_authToken);
5164

5265
this.User = new User
@@ -61,7 +74,8 @@ public void UpdateUserFromJWT()
6174
{
6275
AccountId = _account.AccountId,
6376
AccountName = _account.AccountName,
64-
BasePath = _account.BaseUri
77+
BasePath = _account.BaseUri,
78+
RoomsApiBasePath = _configuration["DocuSign:RoomsApiEndpoint"]
6579
};
6680
}
6781

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using DocuSign.CodeExamples.Controllers;
4+
using DocuSign.CodeExamples.Models;
5+
using DocuSign.CodeExamples.Rooms.Models;
6+
using DocuSign.Rooms.Api;
7+
using DocuSign.Rooms.Client;
8+
using DocuSign.Rooms.Model;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Newtonsoft.Json;
11+
12+
namespace DocuSign.CodeExamples.Rooms.Controllers
13+
{
14+
[Area("Rooms")]
15+
[Route("Eg01")]
16+
public class Eg01CreateRoomWithDataController : EgController
17+
{
18+
public Eg01CreateRoomWithDataController(
19+
DSConfiguration dsConfig,
20+
IRequestItemsService requestItemsService) : base(dsConfig, requestItemsService)
21+
{
22+
}
23+
24+
public override string EgName => "Eg01";
25+
26+
[BindProperty]
27+
public RoomModel RoomModel { get; set; }
28+
29+
protected override void InitializeInternal()
30+
{
31+
RoomModel = new RoomModel();
32+
}
33+
34+
[MustAuthenticate]
35+
[Route("Create")]
36+
[HttpPost]
37+
[ValidateAntiForgeryToken]
38+
public ActionResult Create(RoomModel model)
39+
{
40+
// Step 1. Obtain your OAuth token
41+
var accessToken = RequestItemsService.User.AccessToken; // Represents your {ACCESS_TOKEN}
42+
var basePath = $"{RequestItemsService.Session.RoomsApiBasePath}/restapi"; // Base API path
43+
44+
// Step 2: Construct your API headers
45+
var apiClient = new ApiClient(basePath);
46+
apiClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
47+
var roomsApi = new RoomsApi(apiClient);
48+
var rolesApi = new RolesApi(apiClient);
49+
50+
var accountId = RequestItemsService.Session.AccountId; // Represents your {ACCOUNT_ID}
51+
52+
try
53+
{
54+
// Step 3: Obtain Role
55+
RoleSummary clientRole = rolesApi.GetRoles(accountId, new RolesApi.GetRolesOptions { filter = "Default Admin" }).Roles.First();
56+
57+
// Step 4: Construct the request body for your room
58+
RoomForCreate newRoom = BuildRoom(model, clientRole);
59+
60+
// Step 5: Call the Rooms API to create a room
61+
Room room = roomsApi.CreateRoom(accountId, newRoom);
62+
63+
ViewBag.h1 = "The room was successfully created";
64+
ViewBag.message = $"The room was created! Room ID: {room.RoomId}, Name: {room.Name}.";
65+
ViewBag.Locals.Json = JsonConvert.SerializeObject(room, Formatting.Indented);
66+
67+
return View("example_done");
68+
}
69+
catch (ApiException apiException)
70+
{
71+
ViewBag.errorCode = apiException.ErrorCode;
72+
ViewBag.errorMessage = apiException.Message;
73+
74+
return View("Error");
75+
}
76+
}
77+
78+
private static RoomForCreate BuildRoom(RoomModel model, RoleSummary clientRole)
79+
{
80+
var newRoom = new RoomForCreate
81+
{
82+
Name = model.Name,
83+
RoleId = clientRole.RoleId,
84+
TransactionSideId = "buy",
85+
FieldData = new FieldDataForCreate
86+
{
87+
Data = new Dictionary<string, object>
88+
{
89+
{"address1", "Street 1"},
90+
{"address2", "Unit 10"},
91+
{"city", "New York"},
92+
{"postalCode", "11112"},
93+
{"companyRoomStatus", "5"},
94+
{"state", "US-NY"},
95+
{
96+
"comments", @"New room for sale."
97+
}
98+
}
99+
}
100+
};
101+
102+
return newRoom;
103+
}
104+
}
105+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using DocuSign.CodeExamples.Controllers;
4+
using DocuSign.CodeExamples.Models;
5+
using DocuSign.CodeExamples.Rooms.Models;
6+
using DocuSign.Rooms.Api;
7+
using DocuSign.Rooms.Client;
8+
using DocuSign.Rooms.Model;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Newtonsoft.Json;
11+
12+
namespace DocuSign.CodeExamples.Rooms.Controllers
13+
{
14+
[Area("Rooms")]
15+
[Route("Eg02")]
16+
public class Eg02CreateRoomsFromTemplateController : EgController
17+
{
18+
19+
public Eg02CreateRoomsFromTemplateController(
20+
DSConfiguration dsConfig,
21+
IRequestItemsService requestItemsService) : base(dsConfig, requestItemsService)
22+
{}
23+
24+
public override string EgName => "Eg02";
25+
26+
[BindProperty]
27+
public RoomModel RoomModel { get; set; }
28+
29+
protected override void InitializeInternal()
30+
{
31+
RoomModel = new RoomModel();
32+
}
33+
34+
[MustAuthenticate]
35+
[HttpGet]
36+
public override IActionResult Get()
37+
{
38+
// Step 1. Obtain your OAuth token
39+
var accessToken = RequestItemsService.User.AccessToken; // Represents your {ACCESS_TOKEN}
40+
var basePath = $"{RequestItemsService.Session.RoomsApiBasePath}/restapi"; // Base API path
41+
42+
// Step 2: Construct your API headers
43+
var apiClient = new ApiClient(basePath);
44+
apiClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
45+
var roomTemplatesApi = new RoomTemplatesApi(apiClient);
46+
47+
var accountId = RequestItemsService.Session.AccountId; // Represents your {ACCOUNT_ID}
48+
49+
try
50+
{ //Step 3: Get Templates
51+
var templates = roomTemplatesApi.GetRoomTemplates(accountId);
52+
53+
RoomModel = new RoomModel { Templates = templates.RoomTemplates };
54+
55+
return View("Eg02", this);
56+
57+
}
58+
catch (ApiException apiException)
59+
{
60+
ViewBag.errorCode = apiException.ErrorCode;
61+
ViewBag.errorMessage = apiException.Message;
62+
return View("Error");
63+
}
64+
}
65+
66+
[MustAuthenticate]
67+
[Route("Create")]
68+
[HttpPost]
69+
[ValidateAntiForgeryToken]
70+
public ActionResult Create(RoomModel model)
71+
{
72+
// Step 1. Obtain your OAuth token
73+
var accessToken = RequestItemsService.User.AccessToken; // Represents your {ACCESS_TOKEN}
74+
var basePath = $"{RequestItemsService.Session.RoomsApiBasePath}/restapi"; // Base API path
75+
76+
// Step 2: Construct your API headers
77+
var apiClient = new ApiClient(basePath);
78+
apiClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
79+
var roomsApi = new RoomsApi(apiClient);
80+
var rolesApi = new RolesApi(apiClient);
81+
82+
var accountId = RequestItemsService.Session.AccountId; // Represents your {ACCOUNT_ID}
83+
84+
// Step 4: Obtain Role
85+
RoleSummary clientRole = rolesApi.GetRoles(accountId, new RolesApi.GetRolesOptions { filter = "Default Admin" }).Roles.First();
86+
87+
// Step 5: Construct the request body for your room with using selected template Id
88+
RoomForCreate newRoom = BuildRoom(model, clientRole, model.TemplateId);
89+
90+
try
91+
{
92+
// Step 6: Call the Rooms API to create a room
93+
Room room = roomsApi.CreateRoom(accountId, newRoom);
94+
95+
ViewBag.h1 = "The room was successfully created";
96+
ViewBag.message = $"The room was created! Room ID: {room.RoomId}, name:{room.Name}.";
97+
ViewBag.Locals.Json = JsonConvert.SerializeObject(room, Formatting.Indented);
98+
99+
return View("example_done");
100+
}
101+
catch (ApiException apiException)
102+
{
103+
ViewBag.errorCode = apiException.ErrorCode;
104+
ViewBag.errorMessage = apiException.Message;
105+
return View("Error");
106+
}
107+
}
108+
109+
private static RoomForCreate BuildRoom(RoomModel model, RoleSummary clientRole, int? templateId)
110+
{
111+
var newRoom = new RoomForCreate
112+
{
113+
Name = model.Name,
114+
RoleId = clientRole.RoleId,
115+
TemplateId = templateId,
116+
FieldData = new FieldDataForCreate
117+
{
118+
Data = new Dictionary<string, object>
119+
{
120+
{"address1", "Street 1"},
121+
{"address2", "Unit 10"},
122+
{"city", "New York"},
123+
{"postalCode", "11112"},
124+
{"companyRoomStatus", "5"},
125+
{"state", "US-NY"},
126+
{
127+
"comments", @"New room for sale."
128+
}
129+
}
130+
}
131+
};
132+
133+
return newRoom;
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)