git.iko.soy/rowtype-yoga/royo
on main help sign in
rowtype-yoga/royo/src/Yoga/APIClient.purs
102 lines 3.0 kB history raw
1module Yoga.APIClient where
3import Prelude
4import Affjax as AX
5import Affjax.RequestBody as RequestBody
6import Affjax.RequestHeader (RequestHeader(..))
7import Affjax.ResponseFormat as ResponseFormat
8import Affjax.StatusCode (StatusCode(..))
9import Data.Argonaut.Core (Json)
10import Data.Argonaut.Decode (class DecodeJson, decodeJson, printJsonDecodeError)
11import Data.Argonaut.Decode.Class (class DecodeJson)
12import Data.Argonaut.Encode (encodeJson)
13import Data.Either (Either(..), either)
14import Data.HTTP.Method (Method(..))
15import Data.Maybe (Maybe(..))
16import Data.MediaType.Common (applicationJSON)
17import Effect.Aff (Aff, error, throwError)
19type CompileRequest =
20 { code ∷ String }
22newtype YogaToken = YogaToken String
24compileAndRun ∷ YogaToken -> CompileRequest -> Aff (Either CompileResult RunResult)
25compileAndRun (YogaToken bearerToken) compileRequest = do
26 response <-
27 AX.request
28 ( AX.defaultRequest
29 { url = "https://rowtype.yoga/api/compileAndRun"
30 , method = Left POST
31 , content = Just (RequestBody.Json (encodeJson compileRequest))
32 , responseFormat = ResponseFormat.json
33 , headers =
34 [ RequestHeader "Authorization" ("Bearer " <> bearerToken)
35 , ContentType applicationJSON
36 ]
37 }
38 )
39 case response of
40 Left (l ∷ AX.Error) ->
41 pure
42 ( Left
43 { resultType: ""
44 , result:
45 [ { allSpans: []
46 , errorCode: ""
47 , errorLink: ""
48 , filename: ""
49 , message: AX.printError l
50 , moduleName: Nothing
51 , position:
52 { endColumn: 0
53 , endLine: 0
54 , startColumn: 0
55 , startLine: 0
56 }
57 , suggestion: Nothing
58 }
59 ]
60 }
61 )
62 Right { status, body } -> case status of
63 StatusCode 200 -> Right <$> decodeAff body
64 StatusCode 422 -> Left <$> decodeAff body
65 code -> throwError (error $ "Unexpected response code " <> show code)
67decodeAff ∷ ∀ a. DecodeJson a => Json -> Aff a
68decodeAff body = either (throwError <<< error <<< printJsonDecodeError) pure (decodeJson body)
69type CompileResult =
70 { result ∷ Array ErrorOrWarning
71 , resultType ∷ String
72 }
74type RunResult =
75 { code ∷ Maybe Int, stdout ∷ String, stderr ∷ String }
77type Suggestion =
78 { replaceRange ∷ Position, replacement ∷ String }
80type Span =
81 { end ∷ Array Int
82 , name ∷ String
83 , start ∷ Array Int
84 }
86type Position =
87 { endColumn ∷ Int
88 , endLine ∷ Int
89 , startColumn ∷ Int
90 , startLine ∷ Int
91 }
93type ErrorOrWarning =
94 { allSpans ∷ Array Span
95 , errorCode ∷ String
96 , errorLink ∷ String
97 , filename ∷ String
98 , message ∷ String
99 , moduleName ∷ Maybe String
100 , position ∷ Position
101 , suggestion ∷ Maybe Suggestion
102 }