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})


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

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

.bearer_token_headerObject



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

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

.decode_basic_authObject



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

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



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

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


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

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

.header_builderHash{Symbol=>String}

Parameters:

  • Array (Hash)

Returns:

  • (Hash{Symbol=>String})


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

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

.json_body_fnObject



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

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

.postObject

Client interface



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

def post
  -> correlation, service, resource, opts, hdrs, enc, body_fn, body {
    HttpPort.post.(correlation, service, resource, opts, 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



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

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