emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel] Parse another code block's output then use captured data in another code block
@ 2014-03-07 15:23 Rehan Iftikhar
  2014-03-07 15:35 ` Eric Schulte
  2014-03-07 15:38 ` Andreas Leha
  0 siblings, 2 replies; 3+ messages in thread
From: Rehan Iftikhar @ 2014-03-07 15:23 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 543 bytes --]

Hello

I have a .org file where I am using sh code blocks to interact with a REST
API via curl. My first call is to authenticate with the REST API which
returns a token in the HTTP response. I would like to parse that HTTP
response (ie. with elisp via a subsequent code block) and use the token in
subsequent sh code blocks.

Right now I am doing this via a manual step where I step where I run the
first sh code block, set a :var PROPERTY, and then run the rest of the sh
code blocks. I would like to automate it if possible.

Thanks,
-Rehan

[-- Attachment #2: Type: text/html, Size: 654 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [babel] Parse another code block's output then use captured data in another code block
  2014-03-07 15:23 [babel] Parse another code block's output then use captured data in another code block Rehan Iftikhar
@ 2014-03-07 15:35 ` Eric Schulte
  2014-03-07 15:38 ` Andreas Leha
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Schulte @ 2014-03-07 15:35 UTC (permalink / raw)
  To: Rehan Iftikhar; +Cc: emacs-orgmode

Would something like the following work?

Rehan Iftikhar <rehan.iftikhar@gmail.com> writes:

> Hello
>
> I have a .org file where I am using sh code blocks to interact with a REST
> API via curl.

#+name: downloader
#+begin_src sh
  curl something
#+end_src

> My first call is to authenticate with the REST API which returns a
> token in the HTTP response. I would like to parse that HTTP response
> (ie. with elisp via a subsequent code block)

#+name: parser
#+begin_src emacs-lisp :var input=foo
;; do parsing
#+end_src

> and use the token in subsequent sh code blocks.
>

#+begin_src sh :var data=parser(downloader)
echo "$data"
#+end_src

>
> Right now I am doing this via a manual step where I step where I run
> the first sh code block, set a :var PROPERTY, and then run the rest of
> the sh code blocks. I would like to automate it if possible.
>

It is possible to chain code blocks in variable lines as shown, see the
manual pages on the :var and potentially also the :post header
arguments.  Also, the following has multiple examples demonstrating this
sort of usage.

http://www.jstatsoft.org/v46/i03/

Best,

>
> Thanks,
> -Rehan

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [babel] Parse another code block's output then use captured data in another code block
  2014-03-07 15:23 [babel] Parse another code block's output then use captured data in another code block Rehan Iftikhar
  2014-03-07 15:35 ` Eric Schulte
@ 2014-03-07 15:38 ` Andreas Leha
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Leha @ 2014-03-07 15:38 UTC (permalink / raw)
  To: emacs-orgmode

Hi Rehan,

Rehan Iftikhar <rehan.iftikhar@gmail.com> writes:

> Hello
>
> I have a .org file where I am using sh code blocks to interact with a
> REST API via curl. My first call is to authenticate with the REST API
> which returns a token in the HTTP response. I would like to parse that
> HTTP response (ie. with elisp via a subsequent code block) and use the
> token in subsequent sh code blocks. 
>
> Right now I am doing this via a manual step where I step where I run
> the first sh code block, set a :var PROPERTY, and then run the rest of
> the sh code blocks. I would like to automate it if possible.
>
> Thanks,
> -Rehan 


I am not sure, if I understand your question correctly.  But passing the
token to another code block is easy.

There are two straight forwards ways:

1. As a variable

--8<---------------cut here---------------start------------->8---
#+name: authenticate
#+begin_src sh
  ## do sth to generate a token
  token="uagpb"
  echo "$token"
#+end_src

#+name: usethetoken
#+header: :var token=authenticate()
#+begin_src sh
  echo "${token}.ext"
#+end_src

#+results: usethetoken
: uagpb.ext
--8<---------------cut here---------------end--------------->8---

2. Using noweb

--8<---------------cut here---------------start------------->8---
#+name: authenticate
#+begin_src sh
  ## do sth to generate a token
  token="uagpb"
  echo "$token"
#+end_src

#+name: usethetokenvianoweb
#+begin_src sh :noweb yes
  token=<<authenticate()>>
  echo "${token}.ext"
#+end_src

#+results: usethetokenvianoweb
: uagpb.ext
--8<---------------cut here---------------end--------------->8---

HTH,
Andreas

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-03-07 15:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-07 15:23 [babel] Parse another code block's output then use captured data in another code block Rehan Iftikhar
2014-03-07 15:35 ` Eric Schulte
2014-03-07 15:38 ` Andreas Leha

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).