Skip to content

Commit

Permalink
Ability to parse float/double from request data.
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-crowhurst committed May 13, 2017
1 parent f52fd3c commit 35a89ed
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 16 deletions.
128 changes: 126 additions & 2 deletions source/corvusoft/restbed/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
//System Namespaces
using std::map;
using std::pair;
using std::stof;
using std::stod;
using std::string;
using std::function;
using std::multimap;
using std::make_pair;
using std::unique_ptr;
using std::shared_ptr;
using std::make_shared;
using std::out_of_range;
using std::invalid_argument;

//Project Namespaces
using restbed::Common;
Expand Down Expand Up @@ -127,6 +131,46 @@ namespace restbed
body = ( transform == nullptr ) ? String::to_string( m_pimpl->m_body ) : transform( m_pimpl->m_body );
}

float Request::get_header( const string& name, const float default_value ) const
{
float header = 0;

try
{
header = stof( get_header( name ) );
}
catch ( const out_of_range )
{
header = default_value;
}
catch ( const invalid_argument )
{
header = default_value;
}

return header;
}

double Request::get_header( const string& name, const double default_value ) const
{
double header = 0;

try
{
header = stod( get_header( name ) );
}
catch ( const out_of_range )
{
header = default_value;
}
catch ( const invalid_argument )
{
header = default_value;
}

return header;
}

string Request::get_header( const string& name, const string& default_value ) const
{
if ( name.empty( ) )
Expand Down Expand Up @@ -156,6 +200,46 @@ namespace restbed
return Common::get_parameters( name, m_pimpl->m_headers );
}

float Request::get_query_parameter( const string& name, const float default_value ) const
{
float parameter = 0;

try
{
parameter = stof( get_query_parameter( name ) );
}
catch ( const out_of_range )
{
parameter = default_value;
}
catch ( const invalid_argument )
{
parameter = default_value;
}

return parameter;
}

double Request::get_query_parameter( const string& name, const double default_value ) const
{
double parameter = 0;

try
{
parameter = stod( get_query_parameter( name ) );
}
catch ( const out_of_range )
{
parameter = default_value;
}
catch ( const invalid_argument )
{
parameter = default_value;
}

return parameter;
}

string Request::get_query_parameter( const string& name, const string& default_value ) const
{
if ( name.empty( ) )
Expand Down Expand Up @@ -185,6 +269,46 @@ namespace restbed
return Common::get_parameters( name, m_pimpl->m_query_parameters );
}

float Request::get_path_parameter( const string& name, const float default_value ) const
{
float parameter = 0;

try
{
parameter = stof( get_path_parameter( name ) );
}
catch ( const out_of_range )
{
parameter = default_value;
}
catch ( const invalid_argument )
{
parameter = default_value;
}

return parameter;
}

double Request::get_path_parameter( const string& name, const double default_value ) const
{
double parameter = 0;

try
{
parameter = stod( get_path_parameter( name ) );
}
catch ( const out_of_range )
{
parameter = default_value;
}
catch ( const invalid_argument )
{
parameter = default_value;
}

return parameter;
}

string Request::get_path_parameter( const string& name, const string& default_value ) const
{
if ( name.empty( ) )
Expand Down Expand Up @@ -263,12 +387,12 @@ namespace restbed
{
m_pimpl->m_protocol = value;
}

void Request::add_header( const string& name, const string& value )
{
m_pimpl->m_headers.insert( make_pair( name, value ) );
}

void Request::set_header( const string& name, const string& value )
{
m_pimpl->m_headers.erase( name );
Expand Down
36 changes: 24 additions & 12 deletions source/corvusoft/restbed/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,40 +83,52 @@ namespace restbed

void get_body( std::string& body, const std::function< std::string ( const Bytes& ) >& transform = nullptr ) const;

float get_header( const std::string& name, const float default_value ) const;

double get_header( const std::string& name, const double default_value ) const;

std::string get_header( const std::string& name, const std::string& default_value = "" ) const;

std::string get_header( const std::string& name, const std::function< std::string ( const std::string& ) >& transform ) const;

template< typename Type, typename std::enable_if< std::is_arithmetic< Type >::value, Type >::type = 0 >
Type get_header( const std::string& name, const Type default_value ) const
{
return Common::parse_parameter( get_header( name ), default_value );
}

std::string get_header( const std::string& name, const std::string& default_value ) const;
std::multimap< std::string, std::string > get_headers( const std::string& name = "" ) const;

std::string get_header( const std::string& name, const std::function< std::string ( const std::string& ) >& transform = nullptr ) const;
float get_query_parameter( const std::string& name, const float default_value ) const;

std::multimap< std::string, std::string > get_headers( const std::string& name = "" ) const;
double get_query_parameter( const std::string& name, const double default_value ) const;

std::string get_query_parameter( const std::string& name, const std::string& default_value = "" ) const;

std::string get_query_parameter( const std::string& name, const std::function< std::string ( const std::string& ) >& transform ) const;

template< typename Type, typename std::enable_if< std::is_arithmetic< Type >::value, Type >::type = 0 >
Type get_query_parameter( const std::string& name, const Type default_value ) const
{
return Common::parse_parameter( get_query_parameter( name ), default_value );
}

std::string get_query_parameter( const std::string& name, const std::string& default_value ) const;
std::multimap< std::string, std::string > get_query_parameters( const std::string& name = "" ) const;

float get_path_parameter( const std::string& name, const float default_value ) const;

std::string get_query_parameter( const std::string& name, const std::function< std::string ( const std::string& ) >& transform = nullptr ) const;
double get_path_parameter( const std::string& name, const double default_value ) const;

std::multimap< std::string, std::string > get_query_parameters( const std::string& name = "" ) const;
std::string get_path_parameter( const std::string& name, const std::string& default_value = "" ) const;

std::string get_path_parameter( const std::string& name, const std::function< std::string ( const std::string& ) >& transform ) const;

template< typename Type, typename std::enable_if< std::is_arithmetic< Type >::value, Type >::type = 0 >
Type get_path_parameter( const std::string& name, const Type default_value ) const
{
return Common::parse_parameter( get_path_parameter( name ), default_value );
}

std::string get_path_parameter( const std::string& name, const std::string& default_value ) const;

std::string get_path_parameter( const std::string& name, const std::function< std::string ( const std::string& ) >& transform = nullptr ) const;

std::map< std::string, std::string > get_path_parameters( const std::string& name = "" ) const;

//Setters
Expand All @@ -135,9 +147,9 @@ namespace restbed
void set_method( const std::string& value );

void set_protocol( const std::string& value );

void add_header( const std::string& name, const std::string& value );

void set_header( const std::string& name, const std::string& value );

void set_headers( const std::multimap< std::string, std::string >& values );
Expand Down
10 changes: 8 additions & 2 deletions test/unit/source/request_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,16 @@ TEST_CASE( "validate getter default value", "[request]" )
REQUIRE( value == -6 );
}

SECTION( "float" )
{
float value = request.get_header( "Var", 3.14 );
REQUIRE( value == 3.14f );
}

SECTION( "double" )
{
double value = request.get_header( "Var", 34443 );
REQUIRE( value == 34443 );
double value = request.get_header( "Var", 34.999443 );
REQUIRE( value == 34.999443 );
}

SECTION( "string" )
Expand Down

0 comments on commit 35a89ed

Please sign in to comment.