Custom path separator

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom path separator

Frédéric Menou
Hi guys,

I'm writing a client to some Google APIs, including Google Sheets API.

This action here is causing me some troubles:

POST https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}:batchUpdate

As you can see, they capture the id of the object in the URL as usual, but the custom action batchUpdate is specified after a semi-colon that I can't modelize properly with the :> combinator.

As was thinking of either :
  • writing my custom variant of the :> for this case
  • write a custom type to wrap the id and let it be suffixed by some keyword, in this case ':batchUpdate'
Do you have any advice ?

Frédéric

--
Reply | Threaded
Open this post in threaded view
|

Re: Custom path separator

Alp Mestanogullari
Hello Frédéric,

I would probably pick the second option, it's the simplest one by far and will get the job done without (to me) feeling like a hack. In case you need to support different "keywords" (i.e not just `batchUpdate`), you could even have something like:

newtype Spreadsheet (action :: Symbol) a = Spreadsheet a

where the 'action' bit would be "batchUpdate" or any other string that you need it to be. This might be overkill for now though if you only need to handle "batchUpdate". Then the logic of extracting the spreadsheetId and checking that it is followed by ":batchUpdate" or more generally by ":" and the keyword specified by "action" with my newtype above, could all be handled as part of the instance for FromHttpApiData

On Mon, May 22, 2017 at 10:14 AM, Frédéric Menou <[hidden email]> wrote:
Hi guys,

I'm writing a client to some Google APIs, including Google Sheets API.

This action here is causing me some troubles:
As you can see, they capture the id of the object in the URL as usual, but the custom action batchUpdate is specified after a semi-colon that I can't modelize properly with the :> combinator.

As was thinking of either :
  • writing my custom variant of the :> for this case
  • write a custom type to wrap the id and let it be suffixed by some keyword, in this case ':batchUpdate'
Do you have any advice ?

Frédéric

--



--
Alp Mestanogullari

--
Reply | Threaded
Open this post in threaded view
|

Re: Custom path separator

Frédéric Menou
OK, thank you,

here what I ended with :

data BatchUpdateCommand = BatchUpdateCommand SpreadsheetId

instance ToHttpApiData BatchUpdateCommand where
  toUrlPiece (BUC sid) = pack sid <> ":batchUpdate"

type API = "spreadsheets"
        :> Capture "spreadsheetId" BatchUpdateCommand
        :> ReqBody '[JSON] BatchUpdate
        :> Post    '[JSON] BatchUpdateResult

batchUpdate' :: BatchUpdateCommand -> BatchUpdate -> ClientM BatchUpdateResult
batchUpdate' = client api

batchUpdate :: SpreadsheetId -> BatchUpdate -> ClientM BatchUpdateResult
batchUpdate sid = batchUpdate' (BUC sid)


Le lundi 22 mai 2017 10:23:39 UTC+2, Alp Mestanogullari a écrit :
Hello Frédéric,

I would probably pick the second option, it's the simplest one by far and will get the job done without (to me) feeling like a hack. In case you need to support different "keywords" (i.e not just `batchUpdate`), you could even have something like:

newtype Spreadsheet (action :: Symbol) a = Spreadsheet a

where the 'action' bit would be "batchUpdate" or any other string that you need it to be. This might be overkill for now though if you only need to handle "batchUpdate". Then the logic of extracting the spreadsheetId and checking that it is followed by ":batchUpdate" or more generally by ":" and the keyword specified by "action" with my newtype above, could all be handled as part of the instance for FromHttpApiData

On Mon, May 22, 2017 at 10:14 AM, Frédéric Menou <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="5hauEpZaAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">frederi...@...> wrote:
Hi guys,

I'm writing a client to some Google APIs, including Google Sheets API.

This action here is causing me some troubles:

POST <a href="https://sheets.googleapis.com/v4/spreadsheets/%7BspreadsheetId%7D:batchUpdate" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://sheets.googleapis.com/v4/spreadsheets/%7BspreadsheetId%7D:batchUpdate&#39;;return true;" onclick="this.href=&#39;https://sheets.googleapis.com/v4/spreadsheets/%7BspreadsheetId%7D:batchUpdate&#39;;return true;">https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}:batchUpdate

As you can see, they capture the id of the object in the URL as usual, but the custom action batchUpdate is specified after a semi-colon that I can't modelize properly with the :> combinator.

As was thinking of either :
  • writing my custom variant of the :> for this case
  • write a custom type to wrap the id and let it be suffixed by some keyword, in this case ':batchUpdate'
Do you have any advice ?

Frédéric

--



--
Alp Mestanogullari

--
Reply | Threaded
Open this post in threaded view
|

Re: Custom path separator

Alp Mestanogullari
Oh right, you only need to ToHttpApiData direction. Looks good to me. I'd probably make BatchUpdateCommand a newtype, but that's just nitpicking :)

On Mon, May 22, 2017 at 11:07 AM, Frédéric Menou <[hidden email]> wrote:
OK, thank you,

here what I ended with :

data BatchUpdateCommand = BatchUpdateCommand SpreadsheetId

instance ToHttpApiData BatchUpdateCommand where
  toUrlPiece (BUC sid) = pack sid <> ":batchUpdate"

type API = "spreadsheets"
        :> Capture "spreadsheetId" BatchUpdateCommand
        :> ReqBody '[JSON] BatchUpdate
        :> Post    '[JSON] BatchUpdateResult

batchUpdate' :: BatchUpdateCommand -> BatchUpdate -> ClientM BatchUpdateResult
batchUpdate' = client api

batchUpdate :: SpreadsheetId -> BatchUpdate -> ClientM BatchUpdateResult
batchUpdate sid = batchUpdate' (BUC sid)


Le lundi 22 mai 2017 10:23:39 UTC+2, Alp Mestanogullari a écrit :
Hello Frédéric,

I would probably pick the second option, it's the simplest one by far and will get the job done without (to me) feeling like a hack. In case you need to support different "keywords" (i.e not just `batchUpdate`), you could even have something like:

newtype Spreadsheet (action :: Symbol) a = Spreadsheet a

where the 'action' bit would be "batchUpdate" or any other string that you need it to be. This might be overkill for now though if you only need to handle "batchUpdate". Then the logic of extracting the spreadsheetId and checking that it is followed by ":batchUpdate" or more generally by ":" and the keyword specified by "action" with my newtype above, could all be handled as part of the instance for FromHttpApiData

On Mon, May 22, 2017 at 10:14 AM, Frédéric Menou <[hidden email]> wrote:
Hi guys,

I'm writing a client to some Google APIs, including Google Sheets API.

This action here is causing me some troubles:
As you can see, they capture the id of the object in the URL as usual, but the custom action batchUpdate is specified after a semi-colon that I can't modelize properly with the :> combinator.

As was thinking of either :
  • writing my custom variant of the :> for this case
  • write a custom type to wrap the id and let it be suffixed by some keyword, in this case ':batchUpdate'
Do you have any advice ?

Frédéric

--



--
Alp Mestanogullari

--



--
Alp Mestanogullari

--
Reply | Threaded
Open this post in threaded view
|

Re: Custom path separator

Frédéric Menou
Indeed.

Thank you :)

Le lundi 22 mai 2017 11:22:51 UTC+2, Alp Mestanogullari a écrit :
Oh right, you only need to ToHttpApiData direction. Looks good to me. I'd probably make BatchUpdateCommand a newtype, but that's just nitpicking :)

On Mon, May 22, 2017 at 11:07 AM, Frédéric Menou <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="cMEpDdFdAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">frederi...@...> wrote:
OK, thank you,

here what I ended with :

data BatchUpdateCommand = BatchUpdateCommand SpreadsheetId

instance ToHttpApiData BatchUpdateCommand where
  toUrlPiece (BUC sid) = pack sid <> ":batchUpdate"

type API = "spreadsheets"
        :> Capture "spreadsheetId" BatchUpdateCommand
        :> ReqBody '[JSON] BatchUpdate
        :> Post    '[JSON] BatchUpdateResult

batchUpdate' :: BatchUpdateCommand -> BatchUpdate -> ClientM BatchUpdateResult
batchUpdate' = client api

batchUpdate :: SpreadsheetId -> BatchUpdate -> ClientM BatchUpdateResult
batchUpdate sid = batchUpdate' (BUC sid)


Le lundi 22 mai 2017 10:23:39 UTC+2, Alp Mestanogullari a écrit :
Hello Frédéric,

I would probably pick the second option, it's the simplest one by far and will get the job done without (to me) feeling like a hack. In case you need to support different "keywords" (i.e not just `batchUpdate`), you could even have something like:

newtype Spreadsheet (action :: Symbol) a = Spreadsheet a

where the 'action' bit would be "batchUpdate" or any other string that you need it to be. This might be overkill for now though if you only need to handle "batchUpdate". Then the logic of extracting the spreadsheetId and checking that it is followed by ":batchUpdate" or more generally by ":" and the keyword specified by "action" with my newtype above, could all be handled as part of the instance for FromHttpApiData

On Mon, May 22, 2017 at 10:14 AM, Frédéric Menou <[hidden email]> wrote:
Hi guys,

I'm writing a client to some Google APIs, including Google Sheets API.

This action here is causing me some troubles:

POST <a href="https://sheets.googleapis.com/v4/spreadsheets/%7BspreadsheetId%7D:batchUpdate" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://sheets.googleapis.com/v4/spreadsheets/%7BspreadsheetId%7D:batchUpdate&#39;;return true;" onclick="this.href=&#39;https://sheets.googleapis.com/v4/spreadsheets/%7BspreadsheetId%7D:batchUpdate&#39;;return true;">https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}:batchUpdate

As you can see, they capture the id of the object in the URL as usual, but the custom action batchUpdate is specified after a semi-colon that I can't modelize properly with the :> combinator.

As was thinking of either :
  • writing my custom variant of the :> for this case
  • write a custom type to wrap the id and let it be suffixed by some keyword, in this case ':batchUpdate'
Do you have any advice ?

Frédéric

--



--
Alp Mestanogullari

--



--
Alp Mestanogullari

--