Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom types in API #1296

Closed
ip1981 opened this issue May 3, 2020 · 3 comments
Closed

Custom types in API #1296

ip1981 opened this issue May 3, 2020 · 3 comments

Comments

@ip1981
Copy link

ip1981 commented May 3, 2020

I could not find anything related, except maybe the #766.

So here is what I'd like to have (at least for the client):

data MyType = Foo Int | Bar String | Zzzz Int Int
-- and the URLs like "/example?foo=1" or "/example?bar=qwerty" or "/example?z1=1&z2=2"
type API = "example" :> MyType :> Get '[ JSON] Int
...

clientFunction :: MyType ->  ClientM Int
...

What classes/types families should I implement?

@ericnething
Copy link

You'll want to define instances for ToHttpApiData and FromHttpApiData. https://hackage.haskell.org/package/servant-0.17/docs/Servant-API.html#t:ToHttpApiData

@dmjio
Copy link
Member

dmjio commented May 24, 2020

@ip1981 you'll need to implement HasClient api => HasClient (MyType :> api), you can most likely copy the existing code for QueryParam and modify it slightly for your data type. See https://github.com/haskell-servant/servant/blob/master/servant-client-core/src/Servant/Client/Core/HasClient.hs#L432

@dmjio dmjio closed this as completed May 27, 2020
@ip1981
Copy link
Author

ip1981 commented Jun 28, 2020

Yes, that was it. For future references, there is similar question: #884

And my solution: ip1981/openweathermap@27b2970

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants