# SDK C#

Il pacchetto NuGet `Tomba` è il client .NET ufficiale per l'[API Tomba](/introduction). Ogni area dell'API è una classe di servizio, e ogni metodo invia una richiesta e restituisce l'`HttpResponseMessage` grezzo.

## Requisiti

- .NET 10. La versione 1.0.1 del pacchetto ha come target solo `net10.0`.
- [`Newtonsoft.Json`](https://www.nuget.org/packages/Newtonsoft.Json), installato come dipendenza.

## Installa

```bash
dotnet add package Tomba
```

Le classi si trovano nel namespace `Tomba`.

## Autenticati

Crea una chiave API e copia il tuo secret nella pagina [API keys](https://app.tomba.io/api), poi esportali entrambi:

```bash
export TOMBA_API_KEY="ta_xxxx"
export TOMBA_SECRET_KEY="ts_xxxx"
```

L'SDK non legge le variabili d'ambiente. Passa i valori a `SetKey()` e `SetSecret()`, e il client li invia come header `X-Tomba-Key` e `X-Tomba-Secret`. Chiama ogni setter una sola volta per client: una seconda chiamata lancia `ArgumentException`. La scadenza e la rotazione delle chiavi sono trattate in [Autenticazione](/authentication).

## Prima richiesta

Verifica un indirizzo email.

```csharp
using Newtonsoft.Json.Linq;
using Tomba;

var client = new Client()
    .SetKey(Environment.GetEnvironmentVariable("TOMBA_API_KEY"))
    .SetSecret(Environment.GetEnvironmentVariable("TOMBA_SECRET_KEY"));

var response = await new Verifier(client).EmailVerifier("jane.doe@stripe.com");
var body = JObject.Parse(await response.Content.ReadAsStringAsync());
Console.WriteLine(body["data"]?["email"]?["status"]);
```

L'esempio è un programma `dotnet new console` con istruzioni di primo livello. I metodi restituiscono l'`HttpResponseMessage` senza analizzarlo, quindi leggi e analizza tu il corpo JSON. L'API racchiude il payload in un oggetto `data` ([Formato della risposta](/introduction#response-format)); lo stato della verifica è in `data.email.status`.

## Errori

L'SDK lancia `TombaException` quando l'API risponde con uno stato 400 o superiore e quando la richiesta non riesce. Per gli errori dell'API, l'eccezione è incapsulata: la `TombaException` esterna ha come `Message` l'`errors.message` dell'API, e la sua `InnerException` è una `TombaException` il cui `Code` è lo stato HTTP e la cui `Response` è il corpo grezzo della risposta.

```csharp
using Newtonsoft.Json.Linq;
using Tomba;

var client = new Client()
    .SetKey(Environment.GetEnvironmentVariable("TOMBA_API_KEY"))
    .SetSecret(Environment.GetEnvironmentVariable("TOMBA_SECRET_KEY"));

try
{
    var response = await new Verifier(client).EmailVerifier("jane.doe@stripe.com");
    var body = JObject.Parse(await response.Content.ReadAsStringAsync());
    Console.WriteLine(body["data"]?["email"]?["status"]);
}
catch (TombaException e)
{
    var inner = e.InnerException as TombaException;
    Console.WriteLine($"{inner?.Code} {e.Message}");
    Console.WriteLine(inner?.Response);
}
```

`inner` è `null` quando la richiesta non è riuscita prima che arrivasse una risposta. La `Response` grezza è il corpo JSON dell'errore; ogni `errors.type` che contiene è descritto in [Errori](/error-handling#error-types).

## Limiti di frequenza e nuovi tentativi

L'SDK non riprova le richieste. `Client.ParseRateLimit(response)` legge gli [header dei limiti di frequenza](/rate-limits#rate-limit-headers) di una risposta riuscita in un `RateLimit` con le proprietà `int?` `SecondRateLimit`, `MinuteRateLimit`, `DailyRateLimit`, `MinuteRequestLeft`, `DailyRequestLeft`, `MinuteResetSeconds`, `DailyResetSeconds` e `RetryAfter`, e le proprietà `string` `RateLimitPolicy` e `RateLimitValue`.

Un `429` lancia `TombaException` senza gli header della risposta. Attendi prima di riprovare, come descritto in [Gestisci le risposte 429](/rate-limits#handle-429-responses).

Le richieste vanno in timeout dopo 120 secondi.

## Riferimento dei metodi

Crea ogni servizio con il client, ad esempio `new Verifier(client)`. Ogni metodo restituisce `Task<HttpResponseMessage>`.

| Metodo                                                                               | Endpoint                                                                    |
| ------------------------------------------------------------------------------------ | --------------------------------------------------------------------------- |
| `Account.GetAccount()`                                                               | [`GET /me`](/api/account#get-account)                                       |
| `Usage.GetUsage()`                                                                   | [`GET /usage`](/api/account#retrieve-api-usage)                             |
| `Logs.GetLogs(int? page = null, int? limit = null)`                                  | [`GET /logs`](/api/account#retrieve-api-logs)                               |
| `Format.EmailFormatAsync(string domain)`                                             | [`GET /email-format`](/api/finder#email-format)                             |
| `Count.EmailCount(string domain)`                                                    | [`GET /email-count`](/api/finder#email-count)                               |
| `Verifier.EmailVerifier(string email, string webhookUrl = null)`                     | [`GET /email-verifier`](/api/verifier#email-verifier)                       |
| `Sources.EmailSources(string email)`                                                 | [`GET /email-sources`](/api/email#email-sources)                            |
| `Phone.FinderAsync(Dictionary<string, object> parameters, string webhookUrl = null)` | [`GET /phone-finder`](/api/phone#phone-finder)                              |
| `Phone.ValidatorAsync(string phone, string countryCode = null)`                      | [`GET /phone-validator`](/api/phone#phone-validator)                        |
| `Status.DomainStatus(string domain)`                                                 | [`GET /domain-status`](/api/domain#domain-status)                           |
| `Status.AutoComplete(string query)`                                                  | [`GET /domain-suggestions`](/api/domain-suggestions#get-domain-suggestions) |
| `Keys.GetKeys()`                                                                     | [`GET /keys`](/api/keys#retrieve-api-keys)                                  |
| `Keys.GetKey(string id)`                                                             | [`GET /keys/{id}`](/api/keys#retrieve-a-single-api-key)                     |
| `Keys.CreateKey()`                                                                   | [`POST /keys`](/api/keys#create-an-api-key)                                 |
| `Keys.ResetKey(string id)`                                                           | [`PUT /keys/{id}`](/api/keys#reset-an-api-key)                              |
| `Keys.DeleteKey(string id)`                                                          | [`DELETE /keys/{id}`](/api/keys#delete-an-api-key)                          |
| `Leads.ListLeadsAsync(int? page = 1, int? limit = 10, string domain = null)`         | [`GET /leads`](/api/leads#retrieve-leads)                                   |
| `Leads.GetLeadAsync(string id)`                                                      | [`GET /leads/{id}`](/api/leads#retrieve-a-single-lead)                      |
| `Leads.CreateLeadAsync(Dictionary<string, object> data)`                             | [`POST /leads`](/api/leads#create-a-lead)                                   |
| `Leads.UpdateLeadAsync(string id, Dictionary<string, object> data)`                  | [`PUT /leads/{id}`](/api/leads#update-a-lead)                               |
| `Leads.DeleteLeadAsync(string id)`                                                   | [`DELETE /leads/{id}`](/api/leads#delete-a-lead)                            |
| `LeadsLists.GetLists()`                                                              | [`GET /leads_lists`](/api/lead-lists#retrieve-leads-lists)                  |
| `LeadsLists.DeleteListId(string id)`                                                 | [`DELETE /leads_lists/{id}`](/api/lead-lists#delete-leads-list)             |
| `Bulk.ListAsync(string type, Dictionary<string, object> parameters = null)`          | [`GET /bulk/{type}`](/api/bulks)                                            |
| `Bulk.CreateAsync(string type, Dictionary<string, object> data)`                     | [`POST /bulk/{type}`](/api/bulks)                                           |
| `Bulk.GetAsync(string type, string id)`                                              | [`GET /bulk/{type}/{id}`](/api/bulks)                                       |
| `Bulk.ProgressAsync(string type, string id)`                                         | [`GET /bulk/{type}/{id}/progress`](/api/bulks)                              |
| `Bulk.DownloadAsync(string type, string id)`                                         | [`GET /bulk/{type}/{id}/download`](/api/bulks)                              |
| `Bulk.RenameAsync(string type, string id, string name)`                              | [`PUT /bulk/{type}/{id}/rename`](/api/bulks)                                |

`type` è il segmento `{type}` elencato in [Tipi di job in blocco](/bulks#bulk-types). `CreateAsync` invia un corpo JSON, quindi non può creare job che richiedono il caricamento di un file CSV, come i job [Email Finder](/bulks#email-finder).

## Codice sorgente

[github.com/tomba-io/csharp](https://github.com/tomba-io/csharp) (Apache-2.0). Le note di rilascio sono in [CHANGELOG.md](https://github.com/tomba-io/csharp/blob/master/CHANGELOG.md).
