🍴 Meu Garfo é uma visualização em grafo dos CNPJs cuducos.tngl.io/meu-garfo
1

Configure Feed

Select the types of activity you want to include in your feed.

at main 3.6 kB View raw
1module Api exposing (fetchCompanyName, queryEntity) 2 3import Http 4import Json.Decode as Decode exposing (Decoder, field, string) 5import Types exposing (..) 6 7 8fetchCompanyName : String -> String -> Cmd Msg 9fetchCompanyName jsonApi cnpj = 10 let 11 url = 12 if String.right 1 jsonApi == "/" then 13 jsonApi ++ cnpj 14 15 else 16 jsonApi ++ "/" ++ cnpj 17 in 18 Http.get 19 { url = url 20 , expect = 21 Http.expectStringResponse (GotCompanyName cnpj url) 22 (\response -> 23 case response of 24 Http.BadStatus_ meta _ -> 25 Err (Http.BadStatus meta.statusCode) 26 27 Http.GoodStatus_ _ body -> 28 case Decode.decodeString companyInfoDecoder body of 29 Ok info -> 30 Ok info 31 32 Err _ -> 33 Err (Http.BadBody "Could not parse company info") 34 35 Http.BadUrl_ url_ -> 36 Err (Http.BadUrl url_) 37 38 Http.Timeout_ -> 39 Err Http.Timeout 40 41 Http.NetworkError_ -> 42 Err Http.NetworkError 43 ) 44 } 45 46 47companyInfoDecoder : Decoder CompanyInfo 48companyInfoDecoder = 49 Decode.map2 CompanyInfo 50 (field "razao_social" string) 51 (Decode.maybe (field "situacao_cadastral" Decode.int)) 52 53 54queryEntity : String -> QueryType -> Int -> Cmd Msg 55queryEntity graphApi qType depth = 56 let 57 base = 58 if String.right 1 graphApi == "/" then 59 graphApi 60 61 else 62 graphApi ++ "/" 63 64 ( id, url ) = 65 case qType of 66 EntityQuery term -> 67 ( term, base ++ "relacoes/" ++ term ) 68 69 ConnectionQuery id1 id2 -> 70 ( id1 ++ ";" ++ id2, base ++ "conexao/" ++ id1 ++ "/" ++ id2 ) 71 in 72 Http.get 73 { url = url 74 , expect = Http.expectStringResponse (GotResponse id url depth) toApiResult 75 } 76 77 78toApiResult : Http.Response String -> Result ApiError ApiResponse 79toApiResult response = 80 case response of 81 Http.BadUrl_ u -> 82 Err (HttpError (Http.BadUrl u)) 83 84 Http.Timeout_ -> 85 Err (HttpError Http.Timeout) 86 87 Http.NetworkError_ -> 88 Err (HttpError Http.NetworkError) 89 90 Http.BadStatus_ meta body -> 91 if meta.statusCode == 404 then 92 Err NotFound 93 94 else if meta.statusCode == 400 then 95 case Decode.decodeString (field "message" string) body of 96 Ok msg -> 97 Err (BadRequest msg) 98 99 Err _ -> 100 Err (HttpError (Http.BadStatus meta.statusCode)) 101 102 else 103 Err (HttpError (Http.BadStatus meta.statusCode)) 104 105 Http.GoodStatus_ _ body -> 106 case Decode.decodeString (Decode.list relationDecoder) body of 107 Ok value -> 108 Ok value 109 110 Err err -> 111 Err (HttpError (Http.BadBody (Decode.errorToString err))) 112 113 114relationDecoder : Decoder Relation 115relationDecoder = 116 Decode.map5 Relation 117 (field "cnpj" string) 118 (field "razao_social" string) 119 (field "id" string) 120 (Decode.maybe (field "nome" string)) 121 (Decode.maybe (field "cpf" string))