Using OCaml cohttp Client.post method - http-post

I'm trying to use the OCaml cohttp package to send a POST request using the Client.post method. I looked at the example in ocaml-cohttp/lib_test/test_net_lwt_client_and_server.ml to use the method, here's a code snippet from the example which uses the function.
Client.post ~body:(Cohttp_lwt_body.of_string "barfoo") url
I'm using the function exactly the same way in my own code:
Client.post ~body:(Cohttp_lwt_body.of_string bodyString) (Uri.of_string stringURI) >>= function
| Some (_, body) -> Cohttp_lwt_body.string_of_body body
| None -> return ""
But I get the error message:
Error: This pattern matches values of type 'a option
but a pattern was expected which matches values of type
Cohttp.Response.t * Cohttp_lwt_body.t
I looked at https://github.com/mirage/ocaml-cohttp/issues/64 which suggested changing the ~body label to ?body but then I got a different error:
Error: This expression has type Cohttp_lwt_body.t
but an expression was expected of type Cohttp_lwt_body.t option
Could someone please explain how to use this function correctly?

The error message indicates that this is a typing issue:
Error: This pattern matches values of type 'a option
but a pattern was expected which matches values of type
Cohttp.Response.t * Cohttp_lwt_body.t
Your function body to the right of the bind (>>=) should be rewritten to handle the tuple returned by Client.post rather than an option type. For example:
Client.post
~body:(Cohttp_lwt_body.of_string bodyString)
(Uri.of_string stringURI)
>>= fun (response, body) ->
match response with
| { Cohttp.Response.status = `OK; _ } -> ok_response_action body
| { Cohttp.Response.status; _ } -> other_response_action status body
cohttp does not, unfortunately, currently have easily accessible documentation. You would need to reference the .mli files from the source directly. For example, see here for information on the Cohttp.Response.t type's structure.

Related

phoenix translate msg without put_locale

I have an array of users, each with its own locale.
I need to get a translation of the message into the language of each user.
...
Enum.map(user.tokens, fn(t) -> t.token end)
|> Sender.send(translated_msg(user.locale, msg))
...
defp translated_msg(locale, msg) do
message = Gettext.with_locale MyApp.Gettext, locale, fn ->
MyApp.Gettext.gettext(msg.body, msg.bindings)
end
message
end
where
msg = %{body: "%{login} added a new operation", bindings: %{login: current_user.login}}
But such code is not compiled
== Compilation error on file web/helpers/sender_helper.ex ==
** (ArgumentError) *gettext macros expect translation keys (msgid and msgid_plural) and
domains to expand to strings at compile-time, but the given msgid
doesn't.
Dynamic translations should be avoided as they limit gettext's
ability to extract translations from your source code. If you are
sure you need dynamic lookup, you can use the functions in the Gettext
module:
string = "hello world"
Gettext.gettext(MyApp.Gettext, string)
(gettext) lib/gettext/compiler.ex:196: anonymous fn/2 in Gettext.Compiler.expand_to_binary/4
expanding macro: MyApp.Gettext.dgettext_noop/2
web/helpers/push_helper.ex:23: MyApp.SenderHelper.translated_msg/2
expanding macro: MyApp.Gettext.dgettext/3
web/helpers/push_helper.ex:23: MyApp.SenderHelper.translated_msg/2
expanding macro: MyApp.Gettext.gettext/1
web/helpers/push_helper.ex:23: MyApp.SenderHelper.translated_msg/2
(elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
How can I manually translate a message into another language?
Just like the error message says, if the string to translate is dynamic, you need to use Gettext.gettext/{2,3} passing in your app's Gettext module as the first argument. Since you're passing bindings as well, you need to use Gettext.gettext/3:
Gettext.gettext(MyApp.Gettext, msg.body, msg.bindings)

puppet stdlib 'member" function not working

Trying to use the member function of the puppet stdlib module:
effectively:
$myvariable = 'FOO'
then when using the member function:
member(['FOO','BAR'], $myvariable)
I keep getting the error message:
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Function 'member' must be the value of a statement at /etc/puppet/modules/mymodule/manifests/init.pp:###
Looking at the stdlib documentation for member, we see that member is an rvalue. This means in this context that you need to have its output assigned. This is what the error message of must be the value of a statement is hinting at. Note a helpful wikipedia article on l-values and r-values https://en.wikipedia.org/wiki/Value_(computer_science)#lrvalue.
Your code will work, for example, if you assign the output of member(['FOO','BAR'], $myvariable) to a variable or a resource attribute.
For example:
$myvariable = 'FOO'
$variable = member(['FOO','BAR'], $myvariable)
notify { $variable: }
will result in a notify 'true' during compilation.

What's the difference between 'Array<_>' & 'Array'

I'm getting this error when trying to build the app:
<unknown>:0: error: cannot assign value of type 'Array<_>' to type 'Array'
but Xcode is not indicating a specific line or class for the failure.
If I could understand the difference between
Array<_>
&
Array
it may help me locate the issue.
When you app is crashing, than you can turn on All Exceptions for Debug breakpoint. This should stop on the line where the crash appears.
You find this in Xcode on the Left Panel-> BreakPoint Navigator.
Than press the + in the bottome left corner and Add Exception Breakpoint.
It looks like that you overwrite an array with an array that has a specific value definition. Good Luck :)
A generic argument clause is enclosed in angle brackets ( < > )
< generic argument list >
You can replace a type parameter with a type argument that is itself a specialized version of a generic type.For example, you can replace the type parameter T in Array < T > with a specialized version of an array, Array< Int > , Array< String >, to form an array whose elements are themselves array of integers / Strings
Regarding xour code Snippet the fix is:
u should define it with the right value ... userTweetsArray : [String] = String . or remove the : [String] because setting the value defines already the object and type :)
I don't like answering my own questions but for the sake of closure
I had this line of code
var userTweetsArray : Array = [String]()
I never actually used it. Once I removed that line the error had gone.
The error was caused by assigning an array of a type in this case String to an Array of no type.
So difference is Array<_> is an array of a type and Array is not.

NSLocalizedString should be used directly for exporting XLIFF?

I used to use NSLocalizedString by custom function.
For example, to access Profile.strings, I define this function:
func LocalizedProfile(key: String, comment: String?) {
NSLocalizedString(key, tableName: "Profile", comment: comment ?? "")
}
And, called like this:
let localized = LocalizedProfile("Submit", comment: "For registration")
This method works fine except for exporting XLIFF.
On the Xcode 6.3.2, executting Export for localizationthrows error:
To get error information, I executed via command line:
xcodebuild -exportLocalizations -localizationPath ./xliff -project MyApp.xcodeproj -exportLanguage ja
And, I got this error:
Bad entry in file /Users/mono/Documents/Git/MyApp/Localization.swift (line = 29): Argument is not a literal string.
Defining custom localization method is very useful for me, but I also want to use exporting XLIFF feature.
Are there any methods to resolve this demands?
Export For Localization and xcodebuild -exportLocalizations both generate strings files by looking for invocations of NSLocalizedString(_:tableName:bundle:value:comment:) in code and uses the static values passed into the parameters to create the appropriate strings files.
This means that the only values you can pass into key, comment, value, and tableName are string literals.
Since you're using a wrapper function around NSLocalizedString(_:comment:) to localize your strings, the only time Xcode sees you calling NSLocalizedString(_:comment:) is in that one wrapper function with non-string-literal values, which is invalid.
What you really want to do instead is call NSLocalizedString(_:tableName:comment:) directly.
Alternatively, you can call Bundle.localizedString(forKey:value:table:) in your wrapper function, but then you have to manually create your own strings files for those key-value pairs.
/// - parameter comment: This value is ignored, but should be provided for
/// context at the call site about the requested localized string.
func LocalizedProfile(key: String, comment: String?) -> String {
return Bundle.main.localizedString(forKey: key, value: nil, table: "Profile")
}

Passing Field Symbol value to Return parameter of method

I have the below code which uses a method. When I try to assign the Field Symbol value [Type ANY] to the return parameter RO_TAB [Type Ref to Data], I am getting an error message OBJECTS_MOVE_NOT SUPPORTED [Conversion of type "l" to type "g" not supported.].
The issue is happening after a BW system upgrade along with which we also moved to ABAP objects. The code executes perfectly in the older version of ABAP.
The dump occurs in the below line:
RO_TAB = <lf_storage>.
I have no idea why.
method GET_LU_STORAGE_FOR_ODS.
* IMPORTS
* IF_ODS TYPE RSODSTECH
* IF_ODS_TABLE_TYPE TYPE ZODS_TAB_TYPE
* RETURNS
* RO_TAB TYPE REF TO DATA
FIELD-SYMBOLS:
<lf_storage> TYPE ANY.
DATA:
lf_index TYPE SY-TABIX,
lf_sindex TYPE STRING,
lf_name TYPE STRING.
lf_index = GET_LU_STORAGE_INDEX(
IF_ODS = IF_ODS
IF_ODS_TABLE_TYPE = IF_ODS_TABLE_TYPE ).
lf_sindex = lf_index.
CONCATENATE
'MO_LU_DATA_'
lf_sindex
INTO lf_name.
ASSIGN lf_name TO <lf_storage>.
RO_TAB = <lf_storage>.
endmethod.
You need to create a data object first, using the CREATE DATA statement. Then you can ASSIGN a field symbol to work with the dynamically created data object. There's an example in the online manual. A field symbol is not a reference, it simply places the variable assigned to it in its position. You're effectively trying to move a string (which is what lf_name is) to a reference variable, and that won't work.
You cannot assign a variable of type STRING to a variable of type REF TO DATA.
The following code snippet shows how it should be done.
DATA: lf_name TYPE string.
DATA: lo_tab TYPE REF TO DATA.
FIELD-SYMBOLS: <lf_name> TYPE string.
lf_name = 'test'.
GET REFERENCE OF lf_name INTO lo_tab.
*lf_name = lo_tab. "this is not allowed
ASSIGN lo_tab->* TO <lf_name>.
So in your case it would be sufficient to define a field symbol.
FIELD-SYMBOLS: <lf_name> TYPE STRING.
then assign the contents referenced by RO_TAB to this field symbol.
ASSIGN ro_tab->* TO <lf_name>.
and finally do the concatenation.
CONCATENATE
'MO_LU_DATA_'
lf_index
INTO <lf_name>.
That's all! No further assignments should be required.
How about just this?
lf_sindex = lf_index.
CONCATENATE
'MO_LU_DATA_'
lf_sindex
INTO RO_TAB.

Resources