diff options
Diffstat (limited to 'examples/demo.c')
| -rw-r--r-- | examples/demo.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/examples/demo.c b/examples/demo.c new file mode 100644 index 0000000..d3109a6 --- /dev/null +++ b/examples/demo.c @@ -0,0 +1,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; +} |