[spotify/jsonapi] Expose access token and user country in JSON API

spoitfy endpoint
This commit is contained in:
chme 2018-03-17 12:21:24 +01:00
parent d562cb9b6b
commit 7bab990eb3
3 changed files with 40 additions and 2 deletions

View File

@ -627,6 +627,7 @@ jsonapi_reply_spotify(struct httpd_request *hreq)
char *oauth_uri;
struct spotify_status_info info;
struct spotifywebapi_status_info webapi_info;
struct spotifywebapi_access_token webapi_token;
json_object_object_add(jreply, "enabled", json_object_new_boolean(true));
@ -647,11 +648,16 @@ jsonapi_reply_spotify(struct httpd_request *hreq)
spotify_status_info_get(&info);
json_object_object_add(jreply, "libspotify_installed", json_object_new_boolean(info.libspotify_installed));
json_object_object_add(jreply, "libspotify_logged_in", json_object_new_boolean(info.libspotify_logged_in));
json_object_object_add(jreply, "libspotify_user", json_object_new_string(info.libspotify_user));
safe_json_add_string(jreply, "libspotify_user", info.libspotify_user);
spotifywebapi_status_info_get(&webapi_info);
json_object_object_add(jreply, "webapi_token_valid", json_object_new_boolean(webapi_info.token_valid));
json_object_object_add(jreply, "webapi_user", json_object_new_string(webapi_info.user));
safe_json_add_string(jreply, "webapi_user", webapi_info.user);
safe_json_add_string(jreply, "webapi_country", webapi_info.country);
spotifywebapi_access_token_get(&webapi_token);
safe_json_add_string(jreply, "webapi_token", webapi_token.token);
json_object_object_add(jreply, "webapi_token_expires_in", json_object_new_int(webapi_token.expires_in));
#else
json_object_object_add(jreply, "enabled", json_object_new_boolean(false));

View File

@ -1780,6 +1780,29 @@ spotifywebapi_status_info_get(struct spotifywebapi_status_info *info)
{
memcpy(info->user, spotify_user, (sizeof(info->user) - 1));
}
if (spotify_user_country)
{
memcpy(info->country, spotify_user_country, (sizeof(info->country) - 1));
}
CHECK_ERR(L_SPOTIFY, pthread_mutex_unlock(&token_lck));
}
void
spotifywebapi_access_token_get(struct spotifywebapi_access_token *info)
{
token_refresh();
memset(info, 0, sizeof(struct spotifywebapi_access_token));
CHECK_ERR(L_SPOTIFY, pthread_mutex_lock(&token_lck));
if (token_requested > 0)
info->expires_in = expires_in - difftime(time(NULL), token_requested);
else
info->expires_in = 0;
info->token = safe_strdup(spotify_access_token);
CHECK_ERR(L_SPOTIFY, pthread_mutex_unlock(&token_lck));
}

View File

@ -30,6 +30,13 @@ struct spotifywebapi_status_info
{
bool token_valid;
char user[100];
char country[3]; // ISO 3166-1 alpha-2 country code
};
struct spotifywebapi_access_token
{
int expires_in;
char *token;
};
@ -49,5 +56,7 @@ spotifywebapi_pl_remove(const char *uri);
void
spotifywebapi_status_info_get(struct spotifywebapi_status_info *info);
void
spotifywebapi_access_token_get(struct spotifywebapi_access_token *info);
#endif /* SRC_SPOTIFY_WEBAPI_H_ */