[akka-user] Re: [akka-http] Custom Directives - Scala

2015-08-30 Thread tdrozdowski
Thanks Tomasz - this helps me understand a lot.  Exactly what I need!

On Thursday, August 27, 2015 at 6:47:02 AM UTC-7, Tomasz Kogut wrote:
>
> You are trying to create route at one place (reject part) and directive in 
> another (provide).
>
> Directives can pass and reject stuff eventually they will produce a Route.
>
> Maybe this is more or less you want to achive? Notice the predicate part 
> added. This will be a simple function composition now.
> https://gist.github.com/almendar/416f3bd0f8ef30505166
>
> As for using directives to reject stuff:
>
> in source you can find sth like this:
>
> private val _rejectEmptyResponse: Directive0 =
>   mapRouteResult {
> case Complete(response) if response.entity.isKnownEmpty ⇒ Rejected(Nil)
> case x ⇒ x
>   }
>
>
> this basically shows how to change already created routest.
>
>
> W dniu wtorek, 25 sierpnia 2015 09:23:54 UTC+3 użytkownik 
> tdroz...@gmail.com napisał:
>>
>> I'm new to working with akka-http and have a question that I hope someone 
>> can provide some guidance with.
>>
>> I've been trying to build my own custom directive - that would take the 
>> results of an existing directive - perform some logic and then move onto 
>> the next directive, or reject route. 
>>
>> For example...
>>
>> Let's say I want to take the extractClientIp directive, use the 
>> RemoteAddress value to perform some logic and either continue on with 
>> further directives or reject the request.  I don't intend to complete the 
>> request here.
>>
>> Is this possible?  To make this re-usable is a directive what I'd want to 
>> do here?  Or am I over thinking this?
>>
>> Any help is appreciated!
>>
>> -t
>>
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: [akka-http] Custom Directives - Scala

2015-08-27 Thread Johannes Rudolph
Hi,

use the combinators of the Directive class to make custom directives. E.g.

extractClientIp.flatMap { ip =>
  val cond = // something
  if (cond) pass else reject(...)
}

or if you want to return a value, i.e. create a Directive1[T], use 
`provide` instead of `pass`.

HTH
Johannes

On Tuesday, August 25, 2015 at 8:23:54 AM UTC+2, tdroz...@gmail.com wrote:
>
> I'm new to working with akka-http and have a question that I hope someone 
> can provide some guidance with.
>
> I've been trying to build my own custom directive - that would take the 
> results of an existing directive - perform some logic and then move onto 
> the next directive, or reject route. 
>
> For example...
>
> Let's say I want to take the extractClientIp directive, use the 
> RemoteAddress value to perform some logic and either continue on with 
> further directives or reject the request.  I don't intend to complete the 
> request here.
>
> Is this possible?  To make this re-usable is a directive what I'd want to 
> do here?  Or am I over thinking this?
>
> Any help is appreciated!
>
> -t
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: [akka-http] Custom Directives - Scala

2015-08-27 Thread Tomasz Kogut
You are trying to create route at one place (reject part) and directive in 
another (provide).

Directives can pass and reject stuff eventually they will produce a Route.

Maybe this is more or less you want to achive? Notice the predicate part 
added. This will be a simple function composition now.
https://gist.github.com/almendar/416f3bd0f8ef30505166

As for using directives to reject stuff:

in source you can find sth like this:

private val _rejectEmptyResponse: Directive0 =
  mapRouteResult {
case Complete(response) if response.entity.isKnownEmpty ⇒ Rejected(Nil)
case x ⇒ x
  }


this basically shows how to change already created routest.


W dniu wtorek, 25 sierpnia 2015 09:23:54 UTC+3 użytkownik 
tdroz...@gmail.com napisał:
>
> I'm new to working with akka-http and have a question that I hope someone 
> can provide some guidance with.
>
> I've been trying to build my own custom directive - that would take the 
> results of an existing directive - perform some logic and then move onto 
> the next directive, or reject route. 
>
> For example...
>
> Let's say I want to take the extractClientIp directive, use the 
> RemoteAddress value to perform some logic and either continue on with 
> further directives or reject the request.  I don't intend to complete the 
> request here.
>
> Is this possible?  To make this re-usable is a directive what I'd want to 
> do here?  Or am I over thinking this?
>
> Any help is appreciated!
>
> -t
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: [akka-http] Custom Directives - Scala

2015-08-26 Thread tdrozdowski
Tomas,

Thanks for the response.

So I get how to do a basic custom directive.  Where I'm having difficulty 
is if I try to reference another directive within my custom directive.

Here's what I'm trying to do:

https://gist.github.com/tdrozdowski/5efce0c186f2ae5ea194

Here's the compile error I receive:

Error:(74, 22) type mismatch;
 found   : akka.http.scaladsl.server.Directive1[java.net.InetAddress]
(which expands to) 
 akka.http.scaladsl.server.Directive[(java.net.InetAddress,)]
 required: akka.http.scaladsl.server.StandardRoute
  provide(address)
 ^

To me it appears that the reject properly resolves as a Directive.  But I'm 
not ready to complete the route here - this requires an inner route to do 
something with the provided address.

I think perhaps I'm missing something in regards to how Directives compose. 
 I've been reviewing all the existing routes, and it looks like this should 
work - but its not.

-t

On Wednesday, August 26, 2015 at 7:24:01 AM UTC-7, Tomasz Kogut wrote:
>
> A directive is basically a function:
>
> type Route = RequestContext ⇒ Future[RouteResult]
>
>
> val route: Route = { (ctx: RequestContext) => 
>   ctx.complete("yeah")
> }
>
>
> so just see what you have available in the `ctx` object. You can call 
> ctx.reject when you don't want to pass it further.
>
>
> If your directive needs to take parameters just change val to def and add 
> whatever you need.
>
>
> Best way to learn different neat things is to look at already created 
> directives.
>
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: [akka-http] Custom Directives - Scala

2015-08-26 Thread Tomasz Kogut
A directive is basically a function:

type Route = RequestContext ⇒ Future[RouteResult]


val route: Route = { (ctx: RequestContext) => 
  ctx.complete("yeah")
}


so just see what you have available in the `ctx` object. You can call 
ctx.reject when you don't want to pass it further.


If your directive needs to take parameters just change val to def and add 
whatever you need.


Best way to learn different neat things is to look at already created 
directives.

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: akka-http custom directives

2015-02-27 Thread Tim Pigden
I'll contribute a code example of my own then. Not particularly elegant but 
it might help someone else. 
In essence I create a directive with the path, then map and filter it to 
insert the required rejections then map to get rid of my option. 

// My route already knows the user and the role required. It needs to check 
the user has the right role within the client

def validateRoles(user: User, role: Role)(optClient: Tuple1[Option[Client]]) = {
  (for {
client <- optClient._1

// returns Int bit map of roles for this (client, user) as option

roles <- 
authenticationHolder.model.clientUsers.get(ClientUserNames(client.name, 
user.name)) } yield {
logger.debug(s"client is $client roles $roles looking for $role")
(role & roles) != 0
}).getOrElse(false)
}

// simply checks the client exists. Note the mucking around with Tuple1 - 
this seems to be necessary as it's tuples all the way
val clientOf : (Tuple1[String]) => Option[Client] = { ts => 
authenticationHolder.model.clients.get(ts._1) }

def clientRole(user: User, role: Role) = path( Segment ) // match a string 
for client name
.tmap[Option[Client]](clientOf) // get the client
.tfilter(validateRoles(user, role)(_), AuthorizationFailedRejection) // 
filter out non-existent client or no authorisation
.tmap[Client](_._1.get) // turns it into the client itself. Couldn't figure 
out if I could combine filter and map more elegantly


// not pretty but all I've got time for today

On Friday, February 27, 2015 at 9:56:18 AM UTC, Tim Pigden wrote:
>
> Hi - is there any documentation (even in draft form) about custom 
> directives for akka-http? Or has anyone any code examples?
> Thanks
> Tim
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: akka-http custom directives

2015-02-27 Thread Giovanni Alberto Caporaletti
Hi,
I just started to write one to check basic authentication with an external 
service, because the current implementation forces you to know the 
cleartext password to compare it with the provided one.

This is a basic test I made for the time being.  I was wondering if there 
is a better way of integrating existing flows with the directives, instead 
of sub-materializing them as I did in this example.


class AuthenticationDirectives(userService: UserService) {

  val failure = AuthenticationResult.failWithChallenge(HttpChallenge(scheme = 
"Basic", realm = "test"))

  def authenticated: AuthenticationDirective[User] =
extractFlowMaterializer.flatMap { implicit mat =>
  authenticateOrRejectWithChallenge[BasicHttpCredentials, User] {

case Some(BasicHttpCredentials(username, password)) =>
  userService.authenticate(username, password)
.map {
  case None => failure
  case Some(user) => AuthenticationResult.success(user)
}
.runWith(Sink.head)

case None => Future.successful(failure)

  }
}

}


On Friday, 27 February 2015 09:56:18 UTC, Tim Pigden wrote:
>
> Hi - is there any documentation (even in draft form) about custom 
> directives for akka-http? Or has anyone any code examples?
> Thanks
> Tim
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.