Class: FoucaultHttp::Net

Inherits:
Object
  • Object
show all
Extended by:
Dry::Monads::Try::Mixin
Defined in:
lib/foucault_http/net.rb

Class Method Summary collapse

Class Method Details

.basic_auth_headerHash{Symbol=>String}

(a -> a) -> Hash

Parameters:

  • c (String)

    : Client or user

  • s (String)

    : secret or password

Returns:

  • (Hash{Symbol=>String})


60
61
62
63
64
# File 'lib/foucault_http/net.rb', line 60

def basic_auth_header
  -> c, s {
    { authorization: ("Basic " + Base64::strict_encode64("#{c}:#{s}")).chomp }
  }.curry
end

.bearer_token_headerObject



51
52
53
54
55
# File 'lib/foucault_http/net.rb', line 51

def bearer_token_header
  -> token {
    { authorization: "Bearer #{token}"}
  }
end

.decode_basic_authObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/foucault_http/net.rb', line 66

def decode_basic_auth
  -> encoded_auth {
    result = Try { Base64::strict_decode64(encoded_auth.split(/\s+/).last) }
    case result.success?
    when true
      Try { result.value_or.split(":") }
    else
      result
    end
  }
end

.deleteObject



18
19
20
21
22
# File 'lib/foucault_http/net.rb', line 18

def delete
  -> correlation, service, resource, hdrs {
    HttpPort.delete.(correlation, service, resource, hdrs)
  }.curry
end

.getObject

Example > get.(@env, “/userinfo”, {authorization: “Bearer <token> }, :url_encoded, {} )

Parameters:

  • service

    String

  • resource

    String

  • hdrs
  • enc

    String

  • query


32
33
34
35
36
# File 'lib/foucault_http/net.rb', line 32

def get
  -> correlation, service, resource, hdrs, enc, query {
      HttpPort.get.(correlation, service, resource, hdrs, enc, query)
  }.curry
end

.header_builderHash{Symbol=>String}

Parameters:

  • Array (Hash)

Returns:

  • (Hash{Symbol=>String})


80
81
82
# File 'lib/foucault_http/net.rb', line 80

def header_builder
  -> *hdrs { Fn.inject.({}).(Fn.merge).(hdrs) }
end

.json_body_fnObject



84
85
86
# File 'lib/foucault_http/net.rb', line 84

def json_body_fn
  -> body { JSON.generate(body) }
end

.postObject

Client interface



12
13
14
15
16
# File 'lib/foucault_http/net.rb', line 12

def post
  -> correlation, service, resource, hdrs, enc, body_fn, body {
    HttpPort.post.(correlation, service, resource, hdrs, body_fn, enc, body)
  }.curry
end

.retryerObject

That is, not a circuit breaker

Parameters:

  • fn (Llambda)

    : A partially applied fn

  • args

    : The function’s arguments as either an array or hash

  • retries (Integer)

    : The max number of retries



42
43
44
45
46
47
48
49
# File 'lib/foucault_http/net.rb', line 42

def retryer
  -> fn, args, retries {
    result = fn.(*args)
    return result if result.success?
    return result if retries == 0
    retryer.(fn, args, retries - 1)
  }.curry
end