account.c

00001 /*
00002  * account.c
00003  */
00004 
00005 #define LIBNFSNAPI_BUILDING_LIB
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include "account.h"
00011 #include "error.h"
00012 #include "mgr.h"
00013 
00014 
00015 libnfsnapi_account_t *libnfsnapi_account_create(libnfsnapi_mgr_t *mgr,
00016                                                 const char *account_id)
00017 {
00018         libnfsnapi_account_t *account;
00019 
00020         account = (libnfsnapi_account_t *) malloc(sizeof(libnfsnapi_account_t));
00021         if (!account) {
00022                 mgr->error = LIBNFSNAPI_ERROR_MEMORY;
00023                 return NULL;
00024         }
00025 
00026         account->mgr = mgr;
00027 
00028         /* TODO: check format of Account ID */
00029 
00030         account->account_id = strdup(account_id);
00031         if (!account->account_id) {
00032                 mgr->error = LIBNFSNAPI_ERROR_MEMORY;
00033                 free(account);
00034                 return NULL;
00035         }
00036 
00037         return account;
00038 }
00039 
00040 void libnfsnapi_account_destroy(libnfsnapi_account_t *account)
00041 {
00042         free(account->account_id);
00043         free(account);
00044 }
00045 
00046 /* Returns balance in cents, or a negative number on error. */
00047 int libnfsnapi_account_balance(libnfsnapi_account_t *account,
00048                                 libnfsnapi_account_balance_t type)
00049 {
00050         const char *prop;
00051         char *request_uri;
00052         int err;
00053 
00054         switch (type) {
00055 #define B(x)    case LIBNFSNAPI_BALANCE_##x
00056                 B(ALL):         prop = "balance"; break;
00057                 B(CASH):        prop = "balanceCash"; break;
00058                 B(CREDIT):      prop = "balanceCredit"; break;
00059                 B(HIGH):        prop = "balanceHigh"; break;
00060                 default:
00061                         account->mgr->error = LIBNFSNAPI_ERROR_ARGUMENT;
00062                         return -1;
00063         }
00064 
00065         asprintf(&request_uri, "/account/%s/%s", account->account_id, prop);
00066         if (!request_uri) {
00067                 account->mgr->error = LIBNFSNAPI_ERROR_MEMORY;
00068                 return -1;
00069         }
00070         err = libnfsnapi_mgr_do(account->mgr, LIBNFSNAPI_MGR_HTTP_GET,
00071                                 request_uri, NULL);
00072         free(request_uri);
00073 
00074         if (err)
00075                 return -1;
00076 
00077         /* TODO: retrieve response string and parse more carefully */
00078         float bal = libnfsnapi_mgr_result_float(account->mgr) * 100;
00079         int bal_i = bal;
00080 
00081         return bal_i;
00082 }
00083 
00084 char *libnfsnapi_account_friendlyName(libnfsnapi_account_t *account)
00085 {
00086         char *request_uri;
00087         int err;
00088 
00089         asprintf(&request_uri, "/account/%s/friendlyName", account->account_id);
00090         if (!request_uri) {
00091                 account->mgr->error = LIBNFSNAPI_ERROR_MEMORY;
00092                 return NULL;
00093         }
00094         err = libnfsnapi_mgr_do(account->mgr, LIBNFSNAPI_MGR_HTTP_GET,
00095                         request_uri, NULL);
00096         free(request_uri);
00097 
00098         if (err)
00099                 return NULL;
00100 
00101         /* TODO: retrieve response string and parse more carefully */
00102         return libnfsnapi_mgr_result_string(account->mgr);
00103 }

Generated on Sat Jan 13 17:42:28 2007 for libNFSNapi by  doxygen 1.4.6