The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

WebService::Soundcloud - Thin wrapper around Soundcloud RESTful API!

VERSION

Version 0.01

SYNOPSIS

    #!/usr/bin/perl
    use WebService::Soundcloud;
    
    my $scloud = WebService::Soundcloud->new($client_id, $client_secret, 
                           { redirect_uri => 'http://mydomain.com/callback' }
                         );
    
    # Now get authorization url
    my $authorization_url = $scloud->get_authorization_url();
    
    # Redirect the user to authorization url
    use CGI;
    my $q = new CGI;
    $q->redirect($authorization_url);
    
    # In your '/callback' handler capture code params
    # Check for error
    if ($q->param(error)) {
        die "Authorization Failed: ". $q->param('error');
    }
    # Get authorization code
    my $code = $q->param('code');
    
    # Get Access Token
    my $access_token = $scloud->get_access_token($code);
    
    # Save access_token and refresh_token, expires_in, scope for future use
    my $oauth_token = $access_token->{access_token};
    
    # OAuth Dance is completed :-) Have fun now.

    # Default request and response formats are 'json'
    
    # a GET request '/me' - gets users details
    my $user = $scloud->get('/me');
    
    # a PUT request '/me' - updated users details
    my $user = $scloud->put('/me', encode_json(
                { 'user' => {
                  'description' => 'Have fun with Perl wrapper to Soundcloud API'
                } } ) );
                
    # Comment on a Track POSt request usage
    my $comment = $scloud->post('/tracks/<track_id>/comments', 
                            { body => 'I love this hip-hop track' } );
    
    # Delete a track
    my $track = $scloud->delete('/tracks/{id}');
    
    # Download a track
    my $file_path = $scloud->download('<track_id>', $dest_file);

The above code is a basic usage of WebService::Soundcloud usage in a web environment. See sections mentioned below to get more insight into its feature set.

DESCRIPTION

This module provides a wrapper around Soundcloud RESTful API to work with different kinds of soundcloud resources. It contains many functions for convenient use rather than standard Soundcloud RESTful API.

Following functions are provided:

EXPORT

no functions will be exported by default

SUBROUTINES/METHODS

PACKAGE->new(<CLIENT_ID>, <CLIENT_SECRET>, <HASHREF>)

Returns a newly created WebService::Soundcloud object. The first arguement is client_id, the second arguement is client_secret.Soundcloud will provide you with client_id, client_secret when you register your application with them. The third optional arguement is an anonymous hash contains key value pairs that can be used across created object.

$OBJ->get_authorization_url()

This method is used to get authorization url, user should be redirected for authenticate from soundcloud. This will return URL to which user should be redirected.

$OBJ->get_access_token(<CODE>)

This method is used to receive access_token, refresh_token, scope, expires_in details from soundcloud once user is authenticated. access_token, refresh_token should be stored as it should be sent along with every request to access private resources on the user behalf.

$OBJ->get_access_token_refresh(<REFRESH_TOKEN>)

This method is used to get new access_token by exchanging refresh_token once the earlier access_token is expired. You will receive access_token, refresh_token, scope, expires_in details from soundcloud. access_token, refresh_token should be stored as it should be sent along with every request to access private resources on the user behalf.

$OBJ->request(<METHOD>, <URL>, <HEADERS>, <CONTENT>)

This method is used to dispatch any type of HTTP requests by mentioning GET, POST, PUT, DELETE for METOD parameter on the given <URL>(second arguement). The third optional arguement(<HEADERS>) is used to send headers. Forth optional arguement is used to send content to the <URL>. This method will return HTTP::Response object

$OBJ->get(<URL>, <PARAMS>, <HEADERS>)

This method is used to dispatch GET request on the give URL(first arguement). second arguement is an anonymous hash request parameters to be send along with GET request. The third optional arguement(<HEADERS>) is used to send headers. This method will return HTTP::Response object

$OBJ->post(<URL>, <CONTENT>, <HEADERS>)

This method is used to dispatch POST request on the give URL(first arguement). second arguement is the content to be posted to URL. The third optional arguement(<HEADERS>) is used to send headers. This method will return HTTP::Response object

$OBJ->put(<URL>, <CONTENT>, <HEADERS>)

This method is used to dispatch PUT request on the give URL(first arguement). second arguement is the content to be sent to URL. The third optional arguement(<HEADERS>) is used to send headers. This method will return HTTP::Response object

$OBJ->delete(<URL>, <PARAMS>, <HEADERS>)

This method is used to dispatch DELETE request on the give URL(first arguement). second optional arguement is an anonymous hash request parameters to be send along with DELETE request. The third optional arguement(<HEADERS>) is used to send headers. This method will return HTTP::Response object

$OBJ->download(<TRACK_ID>, <DEST_FILE>)

This method is used to download a particular track id given as first arguement. second arguement is name of the destination path where the downloaded track will be saved to. This method will return the file path of downloaded track.

$OBJ->request_format(<TYPE>)

This method is used to set or get request format for current/future requests being sent to soundcloud. Basically when you are setting request_format it will set 'Content-Type' HTTP request header.

$OBJ->response_format(<TYPE>)

This method is used to set or get response format for current/future requests being sent to soundcloud. Basically when you are setting response_format it will set 'Accept' HTTP request header.

INTERNAL SUBROUTINES/METHODS

Please do not use these internal methods directly. They are internal to WebService::Soundcloud module itself. These can be renamed/deleted/updated at any point of time in future.

$OBJ->_access_token(<PARAMS>)

This method is used to get access_token from soundcloud. This will be called from get_access_token and get_access_token_refresh methods.

$OBJ->_access_token_url(<PARAMS>)

This method is used to get access_token_url of soundcloud RESTful API. This will be called from _access_token method.

$OBJ->_build_url(<PATH>, PARAMS>)

This method is used to prepare absolute URL for a given path and request parameters.

$OBJ->_build_headers(<HEADERS>)

This method is used to set extra headers to the current HTTP Request.

$OBJ->log(<MSG>)

This method is used to write some text to STDERR.

AUTHOR

Mohan Prasad Gutta, <mohanprasadgutta at gmail.com>

BUGS

Please report any bugs or feature requests to bug-net-soundcloud at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-Soundcloud. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command. perldoc WebService::Soundcloud You can also look for information at:

LICENSE AND COPYRIGHT

Copyright 2011 Mohan Prasad Gutta. This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information.