summaryrefslogtreecommitdiffstats
path: root/examples/demo.c
blob: d3109a61596fec171e8732e99a544642b45ccc65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Small command-line client, mostly useful to confirm quickly that an
 * instance answers:
 *
 *     cepis-demo 01001000
 *     cepis-demo --base-url http://localhost:8080 01001000 20040020
 *     cepis-demo --health
 */

#include <stdio.h>
#include <string.h>

#include "cepis.h"

static void print_address(const cepis_address *a)
{
    char masked[10];

    cepis_address_formatted_cep(a, masked);
    printf("%s  %s, %s - %s/%s\n", masked, a->logradouro, a->bairro,
           a->localidade, a->uf);
    if (a->geo != NULL)
        printf("        geo %.6f,%.6f raio %ldm (%s)\n", a->geo->lat,
               a->geo->lon, a->geo->raio_m, a->geo->precisao);
}

int main(int argc, char **argv)
{
    cepis_config cfg;
    cepis_client *client = NULL;
    cepis_error err;
    int want_health = 0;
    int i;
    int rc = 0;

    memset(&cfg, 0, sizeof cfg);
    for (i = 1; i < argc; i++) {
        if (strcmp(argv[i], "--base-url") == 0 && i + 1 < argc)
            cfg.base_url = argv[++i];
        else if (strcmp(argv[i], "--health") == 0)
            want_health = 1;
        else
            break;
    }

    if (cepis_client_new(&cfg, &client) != CEPIS_OK) {
        fprintf(stderr, "cannot build a client\n");
        return 1;
    }

    if (want_health) {
        struct cepis_health *health = NULL;
        cepis_status st = cepis_health(client, &health, &err);

        if (st != CEPIS_OK) {
            fprintf(stderr, "health: %s\n", err.message);
            rc = 1;
        } else {
            printf("status %s  dne %s  ceps %lld  geo %lld  gerada %s\n",
                   health->status, health->dne, health->ceps, health->geo,
                   health->base_gerada_em);
            cepis_health_free(health);
        }
    }

    for (; i < argc; i++) {
        cepis_address *address = NULL;
        cepis_status st = cepis_lookup(client, argv[i], &address, &err);

        if (st != CEPIS_OK) {
            fprintf(stderr, "%s: %s (%s)\n", argv[i], err.message,
                    cepis_strerror(st));
            rc = 1;
            continue;
        }
        print_address(address);
        cepis_address_free(address);
    }

    cepis_client_free(client);
    cepis_global_cleanup();
    return rc;
}