{"size": 1651, "ext": "scrbl", "max_stars_repo_path": "scribblings/import.scrbl", "max_stars_repo_name": "jessealama/riposte", "max_stars_repo_stars_event_min_datetime": "2018-11-08T19:12:04.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-11T16:09:15.000Z", "max_issues_repo_path": "scribblings/import.scrbl", "max_issues_repo_name": "jessealama/riposte", "max_issues_repo_issues_event_min_datetime": "2018-10-25T08:57:28.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-12T12:38:06.000Z", "max_forks_repo_path": "scribblings/import.scrbl", "max_forks_repo_name": "jessealama/riposte", "max_forks_repo_forks_event_min_datetime": "2018-11-15T08:33:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-05T21:07:15.000Z"}
{"max_stars_count": 37, "max_issues_count": 12, "max_forks_count": 5, "avg_line_length": 28.9649122807, "max_line_length": 351, "alphanum_fraction": 0.7135069655}
#lang scribble/manual

@title{Imports}

Imports are a way of putting some Riposte code into one file and using it in another. It gives a way to pass variables from one script to another. They allow you to carry out some requests, gathering a bit of state, and then using that state in another file.

Here's an example of this. Let's call this file @tt{headers.rip}:

@codeblock[#:keep-lang-line? #f]|{
#lang riposte
^Content-Type := "application/json"
^Cache-Control := "no-cache"
}|

Consider setting up a base URI in a separate file, @tt{parameters.rip}:

@codeblock[#:keep-lang-line? #f]|{
#lang riposte
%base := https://cool.test/api/v2/
%timeout := 30
}|

Then, in another Riposte script, @tt{login.rip}, we can import these two to set up a kind of basis:

@codeblock[#:keep-lang-line? #f]|{
#lang riposte
import parameters.rip
import headers.rip

$payload := {
  "username": @USERNAME with fallback "jesse",
  "password": @PASSWORD with fallback "password"
}

POST $payload to login responds with 200

$key := /apikey
^APIKey := $key
}|

The @tt{POST} command here gets executed here with the parameters and headers set up in the other files. Upon executing @tt{login.rip}, a header, @tt{APIKey}, gets set. @tt{login.rip} also ``exports'' a variable, @tt{$key}. If you were to keep going an import @tt{login.rip}, you'd have that variable, and the header assignment would also be in place:

@codeblock[#:keep-lang-line? #f]|{
#lang riposte
import login.rip

# You can refer to $key

$load := {
  "key": $key, # comes from login.rip
  "zoom": "boom"
}

# APIKey request header will be present here:
PATCH $load to dump/truck responds with 204
}|
