Λήψη και Ενημέρωση Παραλήπτη
Ας φτιάξουμε μια επαναχρησιμοποιήσιμη κλάση για να εκτελούμε αιτήματα προς το GraphQL API. Για αυθεντικοποίηση, δείτε Authenticate with the API.
Επαναχρησιμοποιήσιμο Βοηθητικό Μέσο GraphQL
Πρώτα, δημιουργήστε μια βοηθητική κλάση που περιτυλίγει την εκτέλεση ερωτημάτων:
GraphQLLegalesign.cs
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
namespace CLILegalesignExamples
{
public static class GraphQLLegalesign
{
private static readonly HttpClient _httpClient = new();
public static async Task<string?> QueryAsync(string query, object variables, string token)
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://graphql.uk.legalesign.com/graphql"),
Content = new StringContent(
JsonSerializer.Serialize(new { query, variables }),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
using var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
}
Εργαλείο Ενημέρωσης Παραλήπτη
CLIUpdateRecipient.cs
using System.Text.Json;
namespace CLILegalesignExamples
{
class CLIUpdateRecipient
{
static async Task Main(string[] args)
{
Console.WriteLine("Legalesign C# Update Recipient Tool");
string token = "<token-or-api-key>";
Console.WriteLine($"Fetching recipients for document {args[0]}.");
var data = await GraphQLLegalesign.QueryAsync(@"query getDocument($documentId:ID) {
document(id: $documentId) {
id
recipients {
id
email
firstName
lastName
}
}
}", new { documentId = args[0] }, token);
Console.WriteLine(data);
if (data == null) return;
QLResponse? d = JsonSerializer.Deserialize<QLResponse>(data);
QLRecipient? oldRecipient = d.data.document.recipients.Find(r => r.email == args[1]);
if (oldRecipient != null)
{
dynamic newRecipient = new
{
recipientId = oldRecipient.id,
email = args[2],
expiryDate = "2026-10-10T00:00:00.000Z",
firstName = args[3],
lastName = args[4]
};
await UpdateRecipientAsync(newRecipient, token);
}
else
{
Console.WriteLine($"WARNING::Unable to find recipient {args[1]} on document {args[0]}.");
}
async Task UpdateRecipientAsync(dynamic newRecipient, string token)
{
var data = await GraphQLLegalesign.QueryAsync(@"mutation ChangeRecipient(
$recipientId: ID!,
$email: String,
$expiryDate: AWSDateTime,
$firstName: String,
$lastName: String
) {
updateRecipient(
input: {
recipientId: $recipientId,
email: $email,
emailPreviousIfReplaced: false,
expiryDate: $expiryDate,
firstName: $firstName,
lastName: $lastName
}
)
}", newRecipient, token);
QLMutation mut = JsonSerializer.Deserialize<QLMutation>(data);
if (mut.errors != null) Console.WriteLine("ERROR::" + mut.errors[0].message);
else Console.WriteLine("Recipient updated.");
}
}
}
}
Βοηθητικές Κλάσεις
Models.cs
namespace CLILegalesignExamples
{
public class QLRecipient
{
public string? id { get; set; }
public string? firstName { get; set; }
public string? lastName { get; set; }
public string? email { get; set; }
}
public class QLDocument
{
public string? id { get; set; }
public List<QLRecipient>? recipients { get; set; }
}
public class QLData
{
public QLDocument? document { get; set; }
}
public class QLResponse
{
public QLData? data { get; set; }
}
public class Data
{
public object updateRecipient { get; set; }
}
public class Error
{
public List<string> path { get; set; }
public object data { get; set; }
public string errorType { get; set; }
public object errorInfo { get; set; }
public List<Location> locations { get; set; }
public string message { get; set; }
}
public class Location
{
public int line { get; set; }
public int column { get; set; }
public object sourceName { get; set; }
}
public class QLMutation
{
public Data data { get; set; }
public List<Error>? errors { get; set; }
}
}
Βρείτε όλα αυτά στα παραδείγματα έργων: https://github.com/legalesign/CsharpGraphQLExamples
προειδοποίηση
Σημειώστε τον έλεγχο σφαλμάτων μετά την ενημέρωση. Το API θα σας προειδοποιεί κάποιες φορές όταν παραβιάζονται επιχειρηματικοί κανόνες. Ορισμένοι παραλήπτες δεν μπορούν να ενημερωθούν, για παράδειγμα αν οι ρυθμίσεις απαιτούν να μαρτυρούν στη συσκευή που υπογράφει (κλείδωμα συσκευής).