Modifica dei dati del cliente
Come seguire questo capitolo
Dati richiesti: userId, un token autorizzato a modificare il cliente e i campi da cambiare.
Passaggi e risultato atteso: Invia soltanto le modifiche desiderate. Una risposta positiva conferma la richiesta di aggiornamento. Recupera nuovamente il cliente se l’interfaccia necessita dei valori aggiornati.
Alternative e operazioni successive: I dati personali e l’indirizzo si modificano con procedure distinte. Usa quella adatta alla modifica richiesta.
Codice sorgente dei DTO: CountryOutputModel
Le variabili assegnate in un blocco precedente vengono riutilizzate nei successivi. I dati inseriti dal cliente provengono dalla tua interfaccia. Conserva identificativi e valori di riferimento nel contesto del cliente; non ricreare il contesto a ogni passaggio.
Importante
Modifica dei dati personali
Operazione: Invia il payload di aggiornamento.
Da conservare / risultato atteso: Gestisci gli errori di validazione prima di aggiornare l’interfaccia.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
// Assuming this is our client ID
// userId is the identifier returned by registration.
var clientUpdateInputModel = new
{
Name = name,
Surname = surname,
Email = email,
PhoneNumberPrefix = phoneNumberPrefix,
PhoneNumberNumber = phoneNumberNumber,
BirthDate = birthDate,
GenderId = genderId,
LanguageId = languageId,
Nationality1Id = nationalityId,
DeliverTaxStatement = deliverTaxStatement,
PensionSituationId = pensionSituationId
};
var clientUpdateInputModelAsString = JsonConvert.SerializeObject(clientUpdateInputModel);
using var clientUpdateContent = new MultipartFormDataContent();
clientUpdateContent.Add(new StringContent(clientUpdateInputModelAsString, Encoding.UTF8, "application/json"), "jsonPayload");
(await httpClient.PutAsync($"api/v1/users/user-id/{userId}", clientUpdateContent)).EnsureSuccessStatusCode();
// Assuming this is our client ID
// userId is the identifier returned by registration.
JsonObject clientChangeDataPayload = new JsonObject();
clientChangeDataPayload.addProperty("Name", name);
clientChangeDataPayload.addProperty("Surname", surname);
clientChangeDataPayload.addProperty("Email", email);
clientChangeDataPayload.addProperty("PhoneNumberPrefix", phoneNumberPrefix);
clientChangeDataPayload.addProperty("PhoneNumberNumber", phoneNumberNumber);
clientChangeDataPayload.addProperty("BirthDate", birthDate);
clientChangeDataPayload.addProperty("GenderId", genderId);
clientChangeDataPayload.addProperty("LanguageId", languageId);
clientChangeDataPayload.addProperty("Nationality1Id", nationalityId);
clientChangeDataPayload.addProperty("DeliverTaxStatement", deliverTaxStatement);
clientChangeDataPayload.addProperty("PensionSituationId", pensionSituationId);
MultiPartBodyPublisher multipartClientChangeOrderBody = new MultiPartBodyPublisher()
.addPart("jsonPayload", clientChangeDataPayload.toString());
// Add the required identification file as userIdentities when the API requires it.
HttpRequest clientDataChangeRequest = HttpRequest.newBuilder()
.timeout(Duration.ofMinutes(1))
.uri(Settings.wmsUri("users/user-id/" + userId))
.header("Accept-Language", "de-DE")
.header("Authorization", "Bearer " + accessToken)
.header("Content-Type", "multipart/form-data; boundary=" + multipartClientChangeOrderBody.getBoundary())
.PUT(multipartClientChangeOrderBody.build())
.build();
var response = httpClient.send(clientDataChangeRequest, BodyHandlers.ofString());
Api.requireSuccess(response);
Modifica dell’indirizzo
Operazione: Invia la modifica dell’indirizzo.
Da conservare / risultato atteso: Recupera nuovamente il cliente se l’interfaccia necessita dei valori aggiornati dal server.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var userAddressInputModel = new
{
Street = street,
StreetNr = streetNr,
City = city,
CountryId = countryId,
Zip = zip
};
// userId is the identifier returned by registration.
var userAddressInputModelAsString = JsonConvert.SerializeObject(userAddressInputModel);
(await httpClient.PutAsync($"api/v1/users/update-address/user-id/{userId}", new StringContent(userAddressInputModelAsString, Encoding.UTF8, "application/json"))).EnsureSuccessStatusCode();
HttpRequest countryRequest = HttpRequest.newBuilder()
.timeout(Duration.ofMinutes(1))
.uri(Settings.wmsUri("countries/code/FR"))
.header("Accept-Language", "de-DE")
.header("Authorization", "Bearer " + Settings.GUEST_TOKEN) // or CLIENT/ADMIN ACCESS TOKEN
.header("Content-Type", "application/json")
.build();
var response = httpClient.send(countryRequest, BodyHandlers.ofString());
Api.requireSuccess(response);
CountryOutputModel countryObj = new Gson().fromJson(response.body(), CountryOutputModel.class);
var countryId = countryObj.Id;
// Assuming this is our client ID
// userId is the identifier returned by registration.
JsonObject newAddressPayload = new JsonObject();
newAddressPayload.addProperty("Street", street);
newAddressPayload.addProperty("StreetNr", streetNr);
newAddressPayload.addProperty("City", city);
newAddressPayload.addProperty("CountryId", countryId);
newAddressPayload.addProperty("Zip", zip);
HttpRequest theRequest = HttpRequest.newBuilder()
.timeout(Duration.ofMinutes(1))
.uri(Settings.wmsUri("users/update-address/user-id/" + userId))
.header("Accept-Language", "de-DE")
.header("Authorization", "Bearer " + accessToken) // or CLIENT ACCESS TOKEN
.header("Content-Type", "application/json")
.PUT(HttpRequest.BodyPublishers.ofString(newAddressPayload.toString()))
.build();
response = httpClient.send(theRequest, BodyHandlers.ofString());
Api.requireSuccess(response);